-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from sandstorm/feature/12-persistent-resource-…
…fixtures add table printer / BDD step for creating persistent resource / asset fixtures
- Loading branch information
Showing
8 changed files
with
507 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Sandstorm\E2ETestTools\StepGenerator; | ||
|
||
use Neos\Media\Domain\Model\ImageInterface; | ||
|
||
/** | ||
* Builder for printing a persistent resource fixture gherkin table. | ||
*/ | ||
class ImageTable | ||
{ | ||
|
||
private static array $IMAGE_DEFAULT_HEADER = [ | ||
'Image ID', | ||
'Width', | ||
'Height' | ||
]; | ||
|
||
private GherkinTable $gherkinTable; | ||
private PersistentResourceFixtures $persistentResourceFixtures; | ||
|
||
private array $defaultProperties; | ||
|
||
public function __construct(PersistentResourceFixtures $persistentResourceFixtures, array $defaultProperties = []) | ||
{ | ||
$this->persistentResourceFixtures = $persistentResourceFixtures; | ||
$this->defaultProperties = $defaultProperties; | ||
$this->gherkinTable = new GherkinTable( | ||
array_merge( | ||
self::$IMAGE_DEFAULT_HEADER, | ||
array_keys($defaultProperties), | ||
PersistentResourceFixtures::$PERSISTENT_RESOURCE_DEFAULT_HEADER | ||
) | ||
); | ||
} | ||
|
||
public function addImage(string $objectIdentifier, ImageInterface $image): void | ||
{ | ||
$persistentResourceCells = $this->persistentResourceFixtures->addPersistentResource($image->getResource()); | ||
$imageCells = array_merge( | ||
$this->defaultProperties, | ||
[ | ||
'Image ID' => $objectIdentifier, | ||
'Width' => $image->getWidth(), | ||
'Height' => $image->getHeight() | ||
], | ||
$persistentResourceCells | ||
); | ||
$this->gherkinTable->addRow($imageCells); | ||
} | ||
|
||
public function print(): void | ||
{ | ||
if ($this->gherkinTable->isEmpty()) { | ||
echo '# I have no images'; | ||
echo "\n"; | ||
return; | ||
} | ||
echo 'Given I have the following images:'; | ||
echo "\n"; | ||
$this->gherkinTable->print(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Sandstorm\E2ETestTools\StepGenerator; | ||
|
||
use Neos\Flow\Package\PackageManager; | ||
|
||
/** | ||
* Public builder API to configure and create a NodeTable to use in your step generator command controller. | ||
*/ | ||
class NodeTableBuilder | ||
{ | ||
|
||
private PackageManager $packageManager; | ||
private array $defaultNodeProperties = []; | ||
private array $defaultPersistentResourceProperties = []; | ||
private array $defaultImageProperties = []; | ||
private ?string $fixtureBasePath = null; | ||
|
||
public function __construct(PackageManager $packageManager) | ||
{ | ||
$this->packageManager = $packageManager; | ||
} | ||
|
||
public function withDefaultNodeProperties(array $defaultNodeProperties): NodeTableBuilder | ||
{ | ||
$this->defaultNodeProperties = $defaultNodeProperties; | ||
return $this; | ||
} | ||
|
||
public function withDefaultPersistentResourceProperties(array $defaultPersistentResourceProperties): NodeTableBuilder | ||
{ | ||
$this->defaultPersistentResourceProperties = $defaultPersistentResourceProperties; | ||
return $this; | ||
} | ||
|
||
public function withDefaultImageProperties(array $defaultNodeProperties): NodeTableBuilder | ||
{ | ||
$this->defaultNodeProperties = $defaultNodeProperties; | ||
return $this; | ||
} | ||
|
||
public function withFixturesBaseDirectory(string $packageKey, string $subPath): NodeTableBuilder | ||
{ | ||
$package = $this->packageManager->getPackage($packageKey); | ||
$this->fixtureBasePath = $package->getPackagePath() . $subPath; | ||
return $this; | ||
} | ||
|
||
public function build(): NodeTable | ||
{ | ||
return new NodeTable( | ||
$this->defaultNodeProperties, | ||
$this->fixtureBasePath, | ||
$this->defaultPersistentResourceProperties, | ||
$this->defaultImageProperties | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Sandstorm\E2ETestTools\StepGenerator; | ||
|
||
use Neos\Flow\Package\PackageManager; | ||
use Neos\Flow\Annotations as Flow; | ||
|
||
/** | ||
* Public builder API to configure and create a NodeTable to use in your step generator command controller. | ||
* | ||
* @Flow\Scope("singleton") | ||
*/ | ||
class NodeTableBuilderService | ||
{ | ||
|
||
/** | ||
* @Flow\Inject | ||
*/ | ||
protected PackageManager $packageManager; | ||
|
||
public function nodeTable(): NodeTableBuilder | ||
{ | ||
return new NodeTableBuilder($this->packageManager); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Sandstorm\E2ETestTools\StepGenerator; | ||
|
||
use Neos\Flow\ResourceManagement\PersistentResource; | ||
|
||
/** | ||
* Model for persistent resources fixtures collected in a NodeTable. | ||
*/ | ||
class PersistentResourceFixtures | ||
{ | ||
|
||
public static array $PERSISTENT_RESOURCE_DEFAULT_HEADER = [ | ||
'Filename', | ||
'Collection', | ||
'Relative Publication Path', | ||
'Path' | ||
]; | ||
|
||
private ?string $fixtureBasePath; | ||
|
||
private array $defaultProperties = []; | ||
private array $fixtureResources = []; | ||
|
||
public function __construct(?string $fixtureBasePath, array $defaultProperties = []) | ||
{ | ||
if ($fixtureBasePath !== null) { | ||
$this->fixtureBasePath = $fixtureBasePath . (str_ends_with($fixtureBasePath, '/') ? '' : '/'); | ||
if (!str_starts_with($fixtureBasePath, FLOW_PATH_PACKAGES)) { | ||
throw new \Exception("fixtureBasePath must be sub dir of Flow packages directory '" . FLOW_PATH_PACKAGES . "'; but was: " . $fixtureBasePath); | ||
} | ||
} else { | ||
$this->fixtureBasePath = null; | ||
} | ||
$this->defaultProperties = $defaultProperties; | ||
} | ||
|
||
/** | ||
* Adds a persistent resource to the fixtures for later storing. | ||
* Returns the gherkin table cells of the PersistentResource to be printed in a Media fixture table row. | ||
* | ||
* @param PersistentResource $persistentResource | ||
* @return array the PersistentResource table cells | ||
*/ | ||
public function addPersistentResource(PersistentResource $persistentResource): array | ||
{ | ||
if ($this->fixtureBasePath === null) { | ||
throw new \Exception("No fixture base path given for NodeTable"); | ||
} | ||
$fixturePath = $this->fixtureBasePath . $persistentResource->getSha1() . '.' . $persistentResource->getFileExtension(); | ||
if (!array_key_exists($persistentResource->getSha1(), $this->fixtureResources)) { | ||
$this->fixtureResources[$persistentResource->getSha1()] = [ | ||
'object' => $persistentResource, | ||
'path' => $fixturePath | ||
]; | ||
} | ||
$fixturePathRelativeToFlowRoot = substr($fixturePath, strlen(FLOW_PATH_PACKAGES)); | ||
return array_merge($this->defaultProperties, [ | ||
'Filename' => $persistentResource->getFilename(), | ||
'Collection' => $persistentResource->getCollectionName(), | ||
'Relative Publication Path' => $persistentResource->getRelativePublicationPath(), | ||
'Path' => $fixturePathRelativeToFlowRoot | ||
]); | ||
} | ||
|
||
public function storeFixtures(): void | ||
{ | ||
if (count($this->fixtureResources) === 0) { | ||
return; | ||
} | ||
if (!file_exists($this->fixtureBasePath)) { | ||
mkdir($this->fixtureBasePath, 0777, true); | ||
} | ||
foreach ($this->fixtureResources as $sha1 => $persistentResourceObjectAndPath) { | ||
$path = $persistentResourceObjectAndPath['path']; | ||
if (!file_exists($path)) { | ||
/** | ||
* @var PersistentResource $persistentResource | ||
*/ | ||
$persistentResource = $persistentResourceObjectAndPath['object']; | ||
file_put_contents($path, $persistentResource->getStream()); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.