Skip to content

Commit

Permalink
RFT: faker provider as prototype to support inheritance of faker Base…
Browse files Browse the repository at this point in the history
… class
  • Loading branch information
simstern committed Jan 12, 2023
1 parent feee98c commit dd44673
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 40 deletions.
9 changes: 4 additions & 5 deletions Classes/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
13 changes: 1 addition & 12 deletions Classes/Provider/CollectionFakerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
20 changes: 16 additions & 4 deletions Classes/Provider/FakerProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<string, mixed>|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;
}
Expand Down
5 changes: 1 addition & 4 deletions Classes/Provider/FakerProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@

interface FakerProviderInterface
{

public function setOptions(array $options): void;

}
}
7 changes: 4 additions & 3 deletions Classes/Provider/ResourceFakerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@

namespace Swisscom\AliceConnector\Provider;

use Faker\Generator;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ResourceManagement\PersistentResource;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\Document;
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;

Expand All @@ -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;
}
Expand Down
13 changes: 1 addition & 12 deletions Classes/Provider/SecurityFakerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down

0 comments on commit dd44673

Please sign in to comment.