From dd4467339f45e29d04963e992429ece7b91d9328 Mon Sep 17 00:00:00 2001 From: Simon Gadient Date: Thu, 12 Jan 2023 10:42:00 +0100 Subject: [PATCH] RFT: faker provider as prototype to support inheritance of faker Base class --- Classes/Context.php | 9 ++++----- Classes/Provider/CollectionFakerProvider.php | 13 +------------ Classes/Provider/FakerProviderFactory.php | 20 ++++++++++++++++---- Classes/Provider/FakerProviderInterface.php | 5 +---- Classes/Provider/ResourceFakerProvider.php | 7 ++++--- Classes/Provider/SecurityFakerProvider.php | 13 +------------ 6 files changed, 27 insertions(+), 40 deletions(-) diff --git a/Classes/Context.php b/Classes/Context.php index 4798cff..95a40eb 100644 --- a/Classes/Context.php +++ b/Classes/Context.php @@ -61,7 +61,7 @@ public function __construct(ObjectManagerInterface $objectManager, bool $persist foreach ($this->settings['fakerProviders'] as $fakerProviderSetting) { $options = $fakerProviderSetting['options'] ?? []; $options['persistenceEnabled'] = $persistenceEnabled; - $provider = $fakerProviderFactory->create($fakerProviderSetting['provider'], $options); + $provider = $fakerProviderFactory->create($fakerProviderSetting['provider'], $this->faker, $options); $this->faker->addProvider($provider); } } @@ -85,9 +85,9 @@ public function getFaker(): Faker * @return array The loaded objects with object ID as key * @throws Exception */ - public function loadFixture(string $fixtureName, string $fixtureSet = 'default', array $parameters = []): array + public function loadFixture(string $fixtureName, string $fixtureSet = 'default', array $parameters = [], array $objects = []): array { - $objects = $this->getFixtureObjects($fixtureName, $fixtureSet, $parameters); + $objects = $this->getFixtureObjects($fixtureName, $fixtureSet, $parameters, $objects); if ($this->persistenceEnabled) { $this->persist($objects); @@ -96,9 +96,8 @@ public function loadFixture(string $fixtureName, string $fixtureSet = 'default', return $objects; } - protected function getFixtureObjects(string $fixtureName, string $fixtureSet, array $parameters): array + protected function getFixtureObjects(string $fixtureName, string $fixtureSet, array $parameters, array $objects = []): array { - $objects = []; if (!isset($this->settings['fixtureSets'][$fixtureSet])) { throw new Exception(sprintf('No fixture set with name "%s" available.', $fixtureSet), 1614235658); } diff --git a/Classes/Provider/CollectionFakerProvider.php b/Classes/Provider/CollectionFakerProvider.php index eced93d..9c76b2a 100644 --- a/Classes/Provider/CollectionFakerProvider.php +++ b/Classes/Provider/CollectionFakerProvider.php @@ -7,21 +7,10 @@ use Neos\Flow\Annotations as Flow; /** - * @Flow\Scope("singleton") + * @Flow\Scope("prototype") */ class CollectionFakerProvider implements FakerProviderInterface { - - /** - * @var array - */ - protected array $options; - - public function setOptions(array $options): void - { - $this->options = $options; - } - /** * A doctrine array collection * diff --git a/Classes/Provider/FakerProviderFactory.php b/Classes/Provider/FakerProviderFactory.php index f3f18f1..ca2d8f7 100644 --- a/Classes/Provider/FakerProviderFactory.php +++ b/Classes/Provider/FakerProviderFactory.php @@ -3,8 +3,10 @@ namespace Swisscom\AliceConnector\Provider; +use Faker\Generator; use Neos\Flow\Annotations as Flow; use Neos\Flow\ObjectManagement\ObjectManagerInterface; +use Swisscom\AliceConnector\Exception; /** * @Flow\Scope("singleton") @@ -18,11 +20,21 @@ class FakerProviderFactory */ protected $objectManager; - public function create(string $providerClassName, array $options): FakerProviderInterface + /** + * Creator factory that passes the generator as well as possible options to the provider. This allows creation of + * extended "Base" fakers as well as own fakers with options defined in the Settings.yaml. + * + * @param array|null $options + */ + public function create(string $providerClassName, Generator $generator, ?array $options): FakerProviderInterface { - /** @var FakerProviderInterface $provider */ - $provider = $this->objectManager->get($providerClassName); - $provider->setOptions($options); + /** @phpstan-ignore-next-line */ + $provider = $this->objectManager->get($providerClassName, $generator, $options); + if (!$provider instanceof FakerProviderInterface) { + throw new Exception( + sprintf('Alice Connector faker providers should implement "%s"', FakerProviderInterface::class) + ); + } return $provider; } diff --git a/Classes/Provider/FakerProviderInterface.php b/Classes/Provider/FakerProviderInterface.php index 3a4841e..a74285b 100644 --- a/Classes/Provider/FakerProviderInterface.php +++ b/Classes/Provider/FakerProviderInterface.php @@ -5,7 +5,4 @@ interface FakerProviderInterface { - - public function setOptions(array $options): void; - -} \ No newline at end of file +} diff --git a/Classes/Provider/ResourceFakerProvider.php b/Classes/Provider/ResourceFakerProvider.php index d26cbee..5191f8a 100644 --- a/Classes/Provider/ResourceFakerProvider.php +++ b/Classes/Provider/ResourceFakerProvider.php @@ -3,6 +3,7 @@ namespace Swisscom\AliceConnector\Provider; +use Faker\Generator; use Neos\Flow\Annotations as Flow; use Neos\Flow\ResourceManagement\PersistentResource; use Neos\Flow\ResourceManagement\ResourceManager; @@ -10,13 +11,13 @@ use Neos\Media\Domain\Model\Image; /** - * @Flow\Scope("singleton") + * @Flow\Scope("prototype") */ class ResourceFakerProvider implements FakerProviderInterface { /** - * @var array + * @var array{fixturePath: string, persistenceEnabled: bool} */ protected array $options; @@ -26,7 +27,7 @@ class ResourceFakerProvider implements FakerProviderInterface */ protected $resourceManager; - public function setOptions(array $options): void + public function __construct(Generator $generator, array $options) { $this->options = $options; } diff --git a/Classes/Provider/SecurityFakerProvider.php b/Classes/Provider/SecurityFakerProvider.php index 03878da..0fcdf02 100644 --- a/Classes/Provider/SecurityFakerProvider.php +++ b/Classes/Provider/SecurityFakerProvider.php @@ -9,16 +9,10 @@ use Neos\Flow\Security\Policy\Role; /** - * @Flow\Scope("singleton") + * @Flow\Scope("prototype") */ class SecurityFakerProvider implements FakerProviderInterface { - - /** - * @var array - */ - protected array $options; - /** * @Flow\Inject * @var PolicyService @@ -31,11 +25,6 @@ class SecurityFakerProvider implements FakerProviderInterface */ protected $hashService; - public function setOptions(array $options): void - { - $this->options = $options; - } - /** * Password hash for the given password *