From 3a90253c23fea948de082d81a560703af0a2382b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 27 Jul 2016 19:15:17 +0100 Subject: [PATCH] Implement Populator (#450) --- .../Alice/Definition/Fixture/DummyFixture.php | 15 +- .../Alice/Definition/Fixture/FakeFixture.php | 8 - .../Alice/Generator/Hydrator/FakeHydrator.php | 30 +++ .../Definition/Fixture/FixtureWithFlags.php | 8 - .../Definition/Fixture/SimpleFixture.php | 18 +- .../Alice/Definition/Fixture/Templating.php | 2 +- .../Definition/Fixture/TemplatingFixture.php | 8 - .../Alice/Definition/Flag/ExtendFlag.php | 2 +- .../MethodCall/MethodCallWithReference.php | 2 +- .../Alice/Definition/Object/SimpleObject.php | 14 +- src/Nelmio/Alice/Definition/Property.php | 10 + src/Nelmio/Alice/Definition/PropertyBag.php | 12 +- .../ServiceReference/FixtureReference.php | 43 +--- .../InstantiatedReference.php | 2 +- .../ServiceReference/StaticReference.php | 2 +- .../Definition/ServiceReferenceInterface.php | 2 +- .../Instantiator/InstantiationException.php | 2 +- .../InstantiatorNotFoundException.php | 2 +- src/Nelmio/Alice/FixtureInterface.php | 8 +- .../Hydrator/PropertyAccessorHydrator.php | 16 +- .../Alice/Generator/HydratorInterface.php | 9 +- .../AbstractChainableInstantiator.php | 2 +- .../Chainable/FactoryInstantiator.php | 2 +- .../Chainable/NoConstructorInstantiator.php | 4 +- .../Generator/Populator/DummyPopulator.php | 27 --- .../Generator/Populator/SimplePopulator.php | 13 +- .../Fixture/TemplateFixtureResolver.php | 22 +-- src/Nelmio/Alice/Loader/NativeLoader.php | 21 +- src/Nelmio/Alice/ObjectBag.php | 10 +- .../Fixture/FixtureWithFlagsTest.php | 8 +- .../Definition/Fixture/SimpleFixtureTest.php | 3 +- .../Fixture/TemplatingFixtureTest.php | 12 +- .../Definition/Fixture/TemplatingTest.php | 8 +- .../Alice/Definition/PropertyBagTest.php | 24 +++ .../Nelmio/Alice/Definition/PropertyTest.php | 17 ++ .../ServiceReference/FixtureReferenceTest.php | 51 +---- .../InstantiatedReferenceTest.php | 2 +- .../ServiceReference/StaticReferenceTest.php | 2 +- .../Hydrator/PropertyAccessorHydratorTest.php | 11 +- .../Instantiator/InstantiatorRegistryTest.php | 11 +- .../SimpleObjectGeneratorTest.php | 3 +- .../Populator/SimplePopulatorTest.php | 184 ++++++++++++++++++ .../TemplateFixtureBagResolverTest.php | 2 +- .../Fixture/TemplatingFixtureBagTest.php | 16 +- .../DynamicArrayValueResolverTest.php | 2 +- .../Resolver/Value/ResolverRegistryTest.php | 9 +- .../Alice/Loader/LoaderIntegrationTest.php | 4 +- tests/Nelmio/Alice/ObjectBagTest.php | 2 +- 48 files changed, 407 insertions(+), 280 deletions(-) create mode 100644 fixtures/Nelmio/Alice/Generator/Hydrator/FakeHydrator.php delete mode 100644 src/Nelmio/Alice/Generator/Populator/DummyPopulator.php create mode 100644 tests/Nelmio/Alice/Generator/Populator/SimplePopulatorTest.php diff --git a/fixtures/Nelmio/Alice/Definition/Fixture/DummyFixture.php b/fixtures/Nelmio/Alice/Definition/Fixture/DummyFixture.php index bfa92e3e5..ef59b0f76 100644 --- a/fixtures/Nelmio/Alice/Definition/Fixture/DummyFixture.php +++ b/fixtures/Nelmio/Alice/Definition/Fixture/DummyFixture.php @@ -22,12 +22,11 @@ class DummyFixture implements FixtureInterface /** * @var string */ - private $reference; + private $id; - public function __construct(string $reference) + public function __construct(string $id) { - $this->id = __CLASS__.'#'.$reference; - $this->reference = $reference; + $this->id = $id; } /** @@ -38,14 +37,6 @@ public function getId(): string return $this->id; } - /** - * @inheritdoc - */ - public function getReference(): string - { - return $this->reference; - } - /** * @inheritdoc */ diff --git a/fixtures/Nelmio/Alice/Definition/Fixture/FakeFixture.php b/fixtures/Nelmio/Alice/Definition/Fixture/FakeFixture.php index 855a50ccb..6d0fd1a57 100644 --- a/fixtures/Nelmio/Alice/Definition/Fixture/FakeFixture.php +++ b/fixtures/Nelmio/Alice/Definition/Fixture/FakeFixture.php @@ -27,14 +27,6 @@ public function getId(): string $this->__call(); } - /** - * @inheritdoc - */ - public function getReference(): string - { - $this->__call(); - } - /** * @inheritdoc */ diff --git a/fixtures/Nelmio/Alice/Generator/Hydrator/FakeHydrator.php b/fixtures/Nelmio/Alice/Generator/Hydrator/FakeHydrator.php new file mode 100644 index 000000000..64cfd777d --- /dev/null +++ b/fixtures/Nelmio/Alice/Generator/Hydrator/FakeHydrator.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Nelmio\Alice\Generator\Hydrator; + +use Nelmio\Alice\Definition\Property; +use Nelmio\Alice\Generator\HydratorInterface; +use Nelmio\Alice\NotCallableTrait; +use Nelmio\Alice\ObjectInterface; + +class FakeHydrator implements HydratorInterface +{ + use NotCallableTrait; + + /** + * @inheritdoc + */ + public function hydrate(ObjectInterface $object, Property $property): ObjectInterface + { + $this->__call(); + } +} diff --git a/src/Nelmio/Alice/Definition/Fixture/FixtureWithFlags.php b/src/Nelmio/Alice/Definition/Fixture/FixtureWithFlags.php index 863c08ee9..1a8772e21 100644 --- a/src/Nelmio/Alice/Definition/Fixture/FixtureWithFlags.php +++ b/src/Nelmio/Alice/Definition/Fixture/FixtureWithFlags.php @@ -43,14 +43,6 @@ public function getId(): string { return $this->fixture->getId(); } - - /** - * @inheritdoc - */ - public function getReference(): string - { - return $this->fixture->getReference(); - } /** * @inheritdoc diff --git a/src/Nelmio/Alice/Definition/Fixture/SimpleFixture.php b/src/Nelmio/Alice/Definition/Fixture/SimpleFixture.php index 4bf5539ca..5a780c438 100644 --- a/src/Nelmio/Alice/Definition/Fixture/SimpleFixture.php +++ b/src/Nelmio/Alice/Definition/Fixture/SimpleFixture.php @@ -24,11 +24,6 @@ final class SimpleFixture implements FixtureInterface */ private $id; - /** - * @var string - */ - private $reference; - /** * @var string */ @@ -39,11 +34,10 @@ final class SimpleFixture implements FixtureInterface */ private $specs; - public function __construct(string $reference, string $className, SpecificationBag $specs) + public function __construct(string $id, string $className, SpecificationBag $specs) { - $this->id = $className.'#'.$reference; + $this->id = $id; $this->className = $className; - $this->reference = $reference; $this->specs = $specs; } @@ -55,14 +49,6 @@ public function getId(): string return $this->id; } - /** - * @inheritdoc - */ - public function getReference(): string - { - return $this->reference; - } - /** * @inheritdoc */ diff --git a/src/Nelmio/Alice/Definition/Fixture/Templating.php b/src/Nelmio/Alice/Definition/Fixture/Templating.php index fd7eacb90..b58d4eb4b 100644 --- a/src/Nelmio/Alice/Definition/Fixture/Templating.php +++ b/src/Nelmio/Alice/Definition/Fixture/Templating.php @@ -45,7 +45,7 @@ public function __construct(FixtureWithFlags $fixture) if ($flag instanceof ExtendFlag) { // Potential flag duplication is handled at the flagbag level - $this->extends[] = $flag->getExtendedFixture()->createAbsoluteFrom($fixture); + $this->extends[] = $flag->getExtendedFixture(); } } } diff --git a/src/Nelmio/Alice/Definition/Fixture/TemplatingFixture.php b/src/Nelmio/Alice/Definition/Fixture/TemplatingFixture.php index 1a8a9ca41..99f0a2937 100644 --- a/src/Nelmio/Alice/Definition/Fixture/TemplatingFixture.php +++ b/src/Nelmio/Alice/Definition/Fixture/TemplatingFixture.php @@ -47,14 +47,6 @@ public function getId(): string return $this->fixture->getId(); } - /** - * @inheritdoc - */ - public function getReference(): string - { - return $this->fixture->getReference(); - } - /** * @inheritdoc */ diff --git a/src/Nelmio/Alice/Definition/Flag/ExtendFlag.php b/src/Nelmio/Alice/Definition/Flag/ExtendFlag.php index c30bf84d8..a37ddd827 100644 --- a/src/Nelmio/Alice/Definition/Flag/ExtendFlag.php +++ b/src/Nelmio/Alice/Definition/Flag/ExtendFlag.php @@ -38,7 +38,7 @@ final class ExtendFlag implements FlagInterface public function __construct(FixtureReference $extendedFixture) { $this->extendedFixture = $extendedFixture; - $this->stringValue = 'extends '.$extendedFixture->getReference(); + $this->stringValue = 'extends '.$extendedFixture->getId(); } public function getExtendedFixture(): FixtureReference diff --git a/src/Nelmio/Alice/Definition/MethodCall/MethodCallWithReference.php b/src/Nelmio/Alice/Definition/MethodCall/MethodCallWithReference.php index 9b17a62b5..4684afb0f 100644 --- a/src/Nelmio/Alice/Definition/MethodCall/MethodCallWithReference.php +++ b/src/Nelmio/Alice/Definition/MethodCall/MethodCallWithReference.php @@ -53,7 +53,7 @@ public function __construct(ServiceReferenceInterface $caller, string $method, a $this->caller = $caller; $this->method = $method; $this->arguments = $arguments; - $this->stringValue = $caller->getReference().$method; + $this->stringValue = $caller->getId().$method; } /** diff --git a/src/Nelmio/Alice/Definition/Object/SimpleObject.php b/src/Nelmio/Alice/Definition/Object/SimpleObject.php index 7a05ef8ed..231b25893 100644 --- a/src/Nelmio/Alice/Definition/Object/SimpleObject.php +++ b/src/Nelmio/Alice/Definition/Object/SimpleObject.php @@ -43,7 +43,17 @@ public function __construct(string $reference, $instance) $this->reference = $reference; $this->instance = $instance; } - + + /** + * @param \object $newInstance + * + * @return SimpleObject + */ + public function withInstance($newInstance): self + { + return new self($this->reference, $newInstance); + } + public function getReference(): string { return $this->reference; @@ -56,7 +66,7 @@ public function getInstance() { return clone $this->instance; } - + public function __clone() { $this->instance = clone $this->instance; diff --git a/src/Nelmio/Alice/Definition/Property.php b/src/Nelmio/Alice/Definition/Property.php index bd5ac58aa..96e15dcd2 100644 --- a/src/Nelmio/Alice/Definition/Property.php +++ b/src/Nelmio/Alice/Definition/Property.php @@ -40,6 +40,16 @@ public function __construct(string $name, $value) $this->value = $value; } + /** + * param ValueInterface|mixed $value + * + * @return self + */ + public function withValue($value): self + { + return new self($this->name, $value); + } + public function getName(): string { return $this->name; diff --git a/src/Nelmio/Alice/Definition/PropertyBag.php b/src/Nelmio/Alice/Definition/PropertyBag.php index 9218db7fa..dc94442dc 100644 --- a/src/Nelmio/Alice/Definition/PropertyBag.php +++ b/src/Nelmio/Alice/Definition/PropertyBag.php @@ -11,7 +11,9 @@ namespace Nelmio\Alice\Definition; -final class PropertyBag +use Traversable; + +final class PropertyBag implements \IteratorAggregate { /** * @var Property[] @@ -43,4 +45,12 @@ public function mergeWith(self $propertyBag): self return $clone; } + + /** + * @inheritdoc + */ + public function getIterator() + { + return new \ArrayIterator(array_values($this->properties)); + } } diff --git a/src/Nelmio/Alice/Definition/ServiceReference/FixtureReference.php b/src/Nelmio/Alice/Definition/ServiceReference/FixtureReference.php index 2ad70cd98..33c16132f 100644 --- a/src/Nelmio/Alice/Definition/ServiceReference/FixtureReference.php +++ b/src/Nelmio/Alice/Definition/ServiceReference/FixtureReference.php @@ -12,56 +12,27 @@ namespace Nelmio\Alice\Definition\ServiceReference; use Nelmio\Alice\Definition\ServiceReferenceInterface; -use Nelmio\Alice\FixtureInterface; /** - * Value object to point to refer to a fixture. The reference can be relative, e.g. 'user_base' (fixture reference) or - * absolute e.g. 'Nelmio\Alice\User#user_base' (fixture ID). + * Value object to point to refer to a fixture. */ final class FixtureReference implements ServiceReferenceInterface { /** * @var string */ - private $reference; + private $id; /** - * @param string $reference + * @param string $fixtureId */ - public function __construct(string $reference) + public function __construct(string $fixtureId) { - $this->reference = $reference; + $this->id = $fixtureId; } - /** - * A fixture reference may be relative, e.g. 'user_base' (fixture reference). By giving it a fixture, this method - * creates a new reference which will be absolute, e.g. 'Nelmio\Alice\User#user_base' (fixture ID. - * - * @param FixtureInterface $fixture - * - * @return self - */ - public function createAbsoluteFrom(FixtureInterface $fixture): self - { - if (false !== strpos($this->reference, '#')) { - throw new \BadMethodCallException( - sprintf( - 'Attempted to make the reference "%s" absolute from the fixture of ID "%s", however the reference '. - 'is already absolute.', - $this->reference, - $fixture->getId() - ) - ); - } - - $clone = clone $this; - $clone->reference = $fixture->getClassName().'#'.$this->reference; - - return $clone; - } - - public function getReference(): string + public function getId(): string { - return $this->reference; + return $this->id; } } diff --git a/src/Nelmio/Alice/Definition/ServiceReference/InstantiatedReference.php b/src/Nelmio/Alice/Definition/ServiceReference/InstantiatedReference.php index abcd12c57..50a7978b2 100644 --- a/src/Nelmio/Alice/Definition/ServiceReference/InstantiatedReference.php +++ b/src/Nelmio/Alice/Definition/ServiceReference/InstantiatedReference.php @@ -31,7 +31,7 @@ public function __construct(string $reference) $this->reference = $reference; } - public function getReference(): string + public function getId(): string { return $this->reference; } diff --git a/src/Nelmio/Alice/Definition/ServiceReference/StaticReference.php b/src/Nelmio/Alice/Definition/ServiceReference/StaticReference.php index cae177683..0c7914f6a 100644 --- a/src/Nelmio/Alice/Definition/ServiceReference/StaticReference.php +++ b/src/Nelmio/Alice/Definition/ServiceReference/StaticReference.php @@ -31,7 +31,7 @@ public function __construct(string $reference) $this->reference = $reference; } - public function getReference(): string + public function getId(): string { return $this->reference; } diff --git a/src/Nelmio/Alice/Definition/ServiceReferenceInterface.php b/src/Nelmio/Alice/Definition/ServiceReferenceInterface.php index 9d6fa3ad9..a760e6890 100644 --- a/src/Nelmio/Alice/Definition/ServiceReferenceInterface.php +++ b/src/Nelmio/Alice/Definition/ServiceReferenceInterface.php @@ -17,5 +17,5 @@ */ interface ServiceReferenceInterface { - public function getReference(): string; + public function getId(): string; } diff --git a/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiationException.php b/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiationException.php index ce0702575..ad41733ac 100644 --- a/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiationException.php +++ b/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiationException.php @@ -21,7 +21,7 @@ public static function create(FixtureInterface $fixture, \Throwable $previous) return new static( sprintf( 'Could no instantiate fixture "%s".', - $fixture->getReference() + $fixture->getId() ), 0, $previous diff --git a/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiatorNotFoundException.php b/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiatorNotFoundException.php index 30b72f8fd..e405cef25 100644 --- a/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiatorNotFoundException.php +++ b/src/Nelmio/Alice/Exception/Generator/Instantiator/InstantiatorNotFoundException.php @@ -21,7 +21,7 @@ public static function create(FixtureInterface $fixture): self return new static( sprintf( 'No suitable instantiator found for the fixture "%s".', - $fixture->getReference() + $fixture->getId() ) ); } diff --git a/src/Nelmio/Alice/FixtureInterface.php b/src/Nelmio/Alice/FixtureInterface.php index d553c7a6c..b1d95d3f7 100644 --- a/src/Nelmio/Alice/FixtureInterface.php +++ b/src/Nelmio/Alice/FixtureInterface.php @@ -18,16 +18,10 @@ */ interface FixtureInterface { - /** - * @return string Unique across a whole fixture set, mainly used to build unique values. By default is - * 'className#reference' - */ - public function getId(): string; - /** * @return string e.g. 'dummy0'. May contain flags depending of the implementation. */ - public function getReference(): string; + public function getId(): string; /** * @return string FQCN. May contain flags depending of the implementation. diff --git a/src/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydrator.php b/src/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydrator.php index ce4482f8d..2755eba49 100644 --- a/src/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydrator.php +++ b/src/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydrator.php @@ -11,9 +11,11 @@ namespace Nelmio\Alice\Generator\Hydrator; +use Nelmio\Alice\Definition\Object\SimpleObject; use Nelmio\Alice\Definition\Property; use Nelmio\Alice\Generator\HydratorInterface; use Nelmio\Alice\NotClonableTrait; +use Nelmio\Alice\ObjectInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; final class PropertyAccessorHydrator implements HydratorInterface @@ -31,17 +33,13 @@ public function __construct(PropertyAccessorInterface $propertyAccess) } /** - * Hydrate the object with the provided. - * - * @param \object $object - * @param Property $property - * - * @return \object + * @inheritdoc */ - public function hydrate($object, Property $property) + public function hydrate(ObjectInterface $object, Property $property): ObjectInterface { - $this->propertyAccess->setValue($object, $property->getName(), $property->getValue()); + $instance = $object->getInstance(); + $this->propertyAccess->setValue($instance, $property->getName(), $property->getValue()); - return $object; + return new SimpleObject($object->getReference(), $instance); } } diff --git a/src/Nelmio/Alice/Generator/HydratorInterface.php b/src/Nelmio/Alice/Generator/HydratorInterface.php index bc6095be9..dbce0aae2 100644 --- a/src/Nelmio/Alice/Generator/HydratorInterface.php +++ b/src/Nelmio/Alice/Generator/HydratorInterface.php @@ -12,16 +12,17 @@ namespace Nelmio\Alice\Generator; use Nelmio\Alice\Definition\Property; +use Nelmio\Alice\ObjectInterface; interface HydratorInterface { /** * Hydrate the object with the provided. * - * @param \object $object - * @param Property $property + * @param ObjectInterface $object + * @param Property $property * - * @return \object + * @return ObjectInterface */ - public function hydrate($object, Property $property); + public function hydrate(ObjectInterface $object, Property $property): ObjectInterface; } diff --git a/src/Nelmio/Alice/Generator/Instantiator/Chainable/AbstractChainableInstantiator.php b/src/Nelmio/Alice/Generator/Instantiator/Chainable/AbstractChainableInstantiator.php index b79518a52..8e41a3c88 100644 --- a/src/Nelmio/Alice/Generator/Instantiator/Chainable/AbstractChainableInstantiator.php +++ b/src/Nelmio/Alice/Generator/Instantiator/Chainable/AbstractChainableInstantiator.php @@ -26,7 +26,7 @@ public function instantiate(FixtureInterface $fixture, ResolvedFixtureSet $fixtu $instance = $this->createInstance($fixture); $objects = $fixtureSet->getObjects()->with( new SimpleObject( - $fixture->getReference(), + $fixture->getId(), $instance ) ); diff --git a/src/Nelmio/Alice/Generator/Instantiator/Chainable/FactoryInstantiator.php b/src/Nelmio/Alice/Generator/Instantiator/Chainable/FactoryInstantiator.php index 4fae643d4..43c52509f 100644 --- a/src/Nelmio/Alice/Generator/Instantiator/Chainable/FactoryInstantiator.php +++ b/src/Nelmio/Alice/Generator/Instantiator/Chainable/FactoryInstantiator.php @@ -35,7 +35,7 @@ protected function createInstance(FixtureInterface $fixture) { $constructor = $fixture->getSpecs()->getConstructor(); list($factory, $method, $arguments) = [ - $constructor->getCaller()->getReference(), + $constructor->getCaller()->getId(), $constructor->getMethod(), $constructor->getArguments() ]; diff --git a/src/Nelmio/Alice/Generator/Instantiator/Chainable/NoConstructorInstantiator.php b/src/Nelmio/Alice/Generator/Instantiator/Chainable/NoConstructorInstantiator.php index 8a963984a..8d890f9a7 100644 --- a/src/Nelmio/Alice/Generator/Instantiator/Chainable/NoConstructorInstantiator.php +++ b/src/Nelmio/Alice/Generator/Instantiator/Chainable/NoConstructorInstantiator.php @@ -34,7 +34,7 @@ protected function createInstance(FixtureInterface $fixture) throw new InstantiationException( sprintf( 'Could not instantiate "%s", constructor is not public.', - $fixture->getReference() + $fixture->getId() ) ); } @@ -46,7 +46,7 @@ protected function createInstance(FixtureInterface $fixture) throw new InstantiationException( sprintf( 'Could not instantiate "%s", constructor has mandatory parameters but no parameters has been given.', - $fixture->getReference() + $fixture->getId() ) ); } catch (\ReflectionException $exception) { diff --git a/src/Nelmio/Alice/Generator/Populator/DummyPopulator.php b/src/Nelmio/Alice/Generator/Populator/DummyPopulator.php deleted file mode 100644 index 27161691b..000000000 --- a/src/Nelmio/Alice/Generator/Populator/DummyPopulator.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nelmio\Alice\Generator\Populator; - -use Nelmio\Alice\Generator\PopulatorInterface; -use Nelmio\Alice\Generator\ResolvedFixtureSet; -use Nelmio\Alice\ObjectInterface; - -final class DummyPopulator implements PopulatorInterface -{ - /** - * @inheritdoc - */ - public function populate(ObjectInterface $object, ResolvedFixtureSet $fixtureSet): ResolvedFixtureSet - { - return $fixtureSet; - } -} diff --git a/src/Nelmio/Alice/Generator/Populator/SimplePopulator.php b/src/Nelmio/Alice/Generator/Populator/SimplePopulator.php index cde9ffeb3..b103764f1 100644 --- a/src/Nelmio/Alice/Generator/Populator/SimplePopulator.php +++ b/src/Nelmio/Alice/Generator/Populator/SimplePopulator.php @@ -12,6 +12,7 @@ namespace Nelmio\Alice\Generator\Populator; use Nelmio\Alice\Definition\Property; +use Nelmio\Alice\Definition\ValueInterface; use Nelmio\Alice\Generator\HydratorInterface; use Nelmio\Alice\Generator\PopulatorInterface; use Nelmio\Alice\Generator\ResolvedFixtureSet; @@ -49,11 +50,15 @@ public function populate(ObjectInterface $object, ResolvedFixtureSet $fixtureSet $scope = []; foreach ($properties as $property) { - /** @var Property $property */ - $resolvedValue = $this->resolver->resolve($property->getValue(), $fixture, $fixtureSet, $scope); - $scope[$property->getName()] = $resolvedValue; + $propertyValue = $property->getValue(); + if ($propertyValue instanceof ValueInterface) { + $result = $this->resolver->resolve($propertyValue, $fixture, $fixtureSet, $scope); + list($propertyValue, $fixtureSet) = [$result->getValue(), $result->getSet()]; + $property = $property->withValue($propertyValue); + } + $scope[$property->getName()] = $propertyValue; - $object = $this->hydrator->hydrate($object, $property->withValue($resolvedValue)); + $object = $this->hydrator->hydrate($object, $property); } return new ResolvedFixtureSet( diff --git a/src/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureResolver.php b/src/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureResolver.php index 4694aae78..3860010c5 100644 --- a/src/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureResolver.php +++ b/src/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureResolver.php @@ -69,7 +69,7 @@ public function resolve( } /** - * @param FixtureReference[] $fixturesReferences + * @param FixtureReference[] $extendedFixtureReferences * @param FixtureBag $unresolvedFixtures * @param TemplatingFixtureBag $resolvedFixtures * @param ResolvingContext $context @@ -79,38 +79,38 @@ public function resolve( * @return array */ private function resolveExtendedFixtures( - array $fixturesReferences, + array $extendedFixtureReferences, FixtureBag $unresolvedFixtures, TemplatingFixtureBag $resolvedFixtures, ResolvingContext $context ): array { $fixtures = new FixtureBag(); - foreach ($fixturesReferences as $reference) { - $id = $reference->getReference(); - $context = $context->with($id); + foreach ($extendedFixtureReferences as $reference) { + $fixtureId = $reference->getId(); + $context = $context->with($fixtureId); - if (false === $unresolvedFixtures->has($id)) { - throw FixtureNotFoundException::create($id); + if (false === $unresolvedFixtures->has($fixtureId)) { + throw FixtureNotFoundException::create($fixtureId); } - if ($resolvedFixtures->has($id)) { + if ($resolvedFixtures->has($fixtureId)) { $fixtures = $fixtures->with( - $resolvedFixtures->get($id) + $resolvedFixtures->get($fixtureId) ); continue; } $resolvedFixtures = $this->resolve( - $unresolvedFixtures->get($id), + $unresolvedFixtures->get($fixtureId), $unresolvedFixtures, $resolvedFixtures, $context ); $fixtures = $fixtures->with( - $resolvedFixtures->get($id) + $resolvedFixtures->get($fixtureId) ); } diff --git a/src/Nelmio/Alice/Loader/NativeLoader.php b/src/Nelmio/Alice/Loader/NativeLoader.php index 784f30330..97828f8c6 100644 --- a/src/Nelmio/Alice/Loader/NativeLoader.php +++ b/src/Nelmio/Alice/Loader/NativeLoader.php @@ -55,8 +55,9 @@ use Nelmio\Alice\FixtureBuilder\DenormalizerInterface; use Nelmio\Alice\FixtureBuilder\SimpleBuilder; use Nelmio\Alice\Generator\Caller\DummyCaller; -use Nelmio\Alice\Generator\Caller\FakeCaller; use Nelmio\Alice\Generator\CallerInterface; +use Nelmio\Alice\Generator\Hydrator\PropertyAccessorHydrator; +use Nelmio\Alice\Generator\HydratorInterface; use Nelmio\Alice\Generator\Instantiator\Chainable\NoCallerMethodCallInstantiator; use Nelmio\Alice\Generator\Instantiator\Chainable\NoConstructorInstantiator; use Nelmio\Alice\Generator\Instantiator\Chainable\FactoryInstantiator; @@ -64,16 +65,13 @@ use Nelmio\Alice\Generator\Instantiator\InstantiatorResolver; use Nelmio\Alice\Generator\InstantiatorInterface; use Nelmio\Alice\Generator\ObjectGenerator\SimpleObjectGenerator; -use Nelmio\Alice\Generator\Populator\DummyPopulator; -use Nelmio\Alice\Generator\Populator\FakePopulator; +use Nelmio\Alice\Generator\Populator\SimplePopulator; use Nelmio\Alice\Generator\PopulatorInterface; use Nelmio\Alice\Generator\Resolver\Fixture\TemplateFixtureBagResolver; -use Nelmio\Alice\Generator\Resolver\Instantiator\FakeInstantiator; use Nelmio\Alice\Generator\Resolver\SimpleFixtureSetResolver; use Nelmio\Alice\Generator\Resolver\UniqueValuesPool; use Nelmio\Alice\Generator\Resolver\Value\Chainable\DynamicArrayValueResolver; use Nelmio\Alice\Generator\Resolver\Value\Chainable\UniqueValueResolver; -use Nelmio\Alice\Generator\Resolver\Value\FakeValueResolver; use Nelmio\Alice\Generator\Resolver\Value\ResolverRegistry; use Nelmio\Alice\Generator\ValueResolverInterface; use Nelmio\Alice\Parser\Chainable\PhpParser; @@ -97,6 +95,7 @@ use Nelmio\Alice\NotClonableTrait; use Nelmio\Alice\ObjectSet; use Nelmio\Alice\Generator\Resolver\ParameterBagResolverInterface; +use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\Yaml\Parser as SymfonyYamlParser; /** @@ -261,7 +260,10 @@ public function getBuiltInInstantiator(): InstantiatorInterface public function getBuiltInPopulator(): PopulatorInterface { - return new DummyPopulator(); + return new SimplePopulator( + $this->getBuiltInValueResolver(), + $this->getBuiltInHydrator() + ); } public function getBuiltInCaller(): CallerInterface @@ -279,6 +281,13 @@ public function getBuiltInValueResolver(): ValueResolverInterface ]); } + public function getBuiltInHydrator(): HydratorInterface + { + return new PropertyAccessorHydrator( + new PropertyAccessor() + ); + } + public function getBuiltInUniqueValuesPool(): UniqueValuesPool { return new UniqueValuesPool(); diff --git a/src/Nelmio/Alice/ObjectBag.php b/src/Nelmio/Alice/ObjectBag.php index 59e8d08b7..fd4c2dedc 100644 --- a/src/Nelmio/Alice/ObjectBag.php +++ b/src/Nelmio/Alice/ObjectBag.php @@ -27,6 +27,10 @@ final class ObjectBag implements \IteratorAggregate, \Countable public function __construct(array $objects = []) { foreach ($objects as $reference => $object) { + if ($object instanceof ObjectInterface) { + $object = $object->getInstance(); + } + $this->objects[$reference] = new SimpleObject($reference, $object); } } @@ -67,7 +71,7 @@ public function mergeWith(self $objects): self public function has(FixtureInterface $fixture): bool { - return isset($this->objects[$fixture->getReference()]); + return isset($this->objects[$fixture->getId()]); } /** @@ -80,10 +84,10 @@ public function has(FixtureInterface $fixture): bool public function get(FixtureInterface $fixture): ObjectInterface { if ($this->has($fixture)) { - return clone $this->objects[$fixture->getReference()]; + return clone $this->objects[$fixture->getId()]; } - throw ObjectNotFoundException::create($fixture->getReference(), $fixture->getClassName()); + throw ObjectNotFoundException::create($fixture->getId(), $fixture->getClassName()); } /** diff --git a/tests/Nelmio/Alice/Definition/Fixture/FixtureWithFlagsTest.php b/tests/Nelmio/Alice/Definition/Fixture/FixtureWithFlagsTest.php index 5d80677a0..4be312aea 100644 --- a/tests/Nelmio/Alice/Definition/Fixture/FixtureWithFlagsTest.php +++ b/tests/Nelmio/Alice/Definition/Fixture/FixtureWithFlagsTest.php @@ -30,14 +30,12 @@ public function testIsAFixture() public function testAccessors() { - $id = 'Nelmio\Entity\User#user0'; $reference = 'user0'; $className = 'Nelmio\Entity\User'; $specs = new SpecificationBag(null, new PropertyBag(), new MethodCallBag()); $decoratedFixtureProphecy = $this->prophesize(FixtureInterface::class); - $decoratedFixtureProphecy->getId()->willReturn($id); - $decoratedFixtureProphecy->getReference()->willReturn($reference); + $decoratedFixtureProphecy->getId()->willReturn($reference); $decoratedFixtureProphecy->getClassName()->willReturn($className); $decoratedFixtureProphecy->getSpecs()->willReturn($specs); /** @var FixtureInterface $decoratedFixture */ @@ -47,14 +45,12 @@ public function testAccessors() $fixture = new FixtureWithFlags($decoratedFixture, $flags); - $this->assertEquals($id, $fixture->getId()); - $this->assertEquals($reference, $fixture->getReference()); + $this->assertEquals($reference, $fixture->getId()); $this->assertEquals($className, $fixture->getClassName()); $this->assertEquals($specs, $fixture->getSpecs()); $this->assertEquals($flags, $fixture->getFlags()); $decoratedFixtureProphecy->getId()->shouldHaveBeenCalledTimes(1); - $decoratedFixtureProphecy->getReference()->shouldHaveBeenCalledTimes(1); $decoratedFixtureProphecy->getClassName()->shouldHaveBeenCalledTimes(1); $decoratedFixtureProphecy->getSpecs()->shouldHaveBeenCalledTimes(1); } diff --git a/tests/Nelmio/Alice/Definition/Fixture/SimpleFixtureTest.php b/tests/Nelmio/Alice/Definition/Fixture/SimpleFixtureTest.php index 3213eb2d7..ac85a4cfa 100644 --- a/tests/Nelmio/Alice/Definition/Fixture/SimpleFixtureTest.php +++ b/tests/Nelmio/Alice/Definition/Fixture/SimpleFixtureTest.php @@ -35,8 +35,7 @@ public function testAccessors() $fixture = new SimpleFixture($reference, $className, $specs); - $this->assertEquals('Nelmio\Entity\User#user0', $fixture->getId()); - $this->assertEquals($reference, $fixture->getReference()); + $this->assertEquals($reference, $fixture->getId()); $this->assertEquals($className, $fixture->getClassName()); $this->assertEquals($specs, $fixture->getSpecs()); } diff --git a/tests/Nelmio/Alice/Definition/Fixture/TemplatingFixtureTest.php b/tests/Nelmio/Alice/Definition/Fixture/TemplatingFixtureTest.php index c972b1d2e..3d17c7924 100644 --- a/tests/Nelmio/Alice/Definition/Fixture/TemplatingFixtureTest.php +++ b/tests/Nelmio/Alice/Definition/Fixture/TemplatingFixtureTest.php @@ -34,14 +34,12 @@ public function testIsAFixture() public function testAccessors() { - $id = 'Nelmio\Entity\User#user0'; $reference = 'user0'; $className = 'Nelmio\Entity\User'; $specs = new SpecificationBag(null, new PropertyBag(), new MethodCallBag()); $decoratedFixtureProphecy = $this->prophesize(FixtureInterface::class); - $decoratedFixtureProphecy->getId()->willReturn($id); - $decoratedFixtureProphecy->getReference()->willReturn($reference); + $decoratedFixtureProphecy->getId()->willReturn($reference); $decoratedFixtureProphecy->getClassName()->willReturn($className); $decoratedFixtureProphecy->getSpecs()->willReturn($specs); /** @var FixtureInterface $decoratedFixture */ @@ -59,17 +57,15 @@ public function testAccessors() $fixtureWithFlags = new FixtureWithFlags($decoratedFixture, $flags); $fixture = new TemplatingFixture($fixtureWithFlags); - $this->assertEquals($id, $fixture->getId()); - $this->assertEquals($reference, $fixture->getReference()); + $this->assertEquals($reference, $fixture->getId()); $this->assertEquals($className, $fixture->getClassName()); $this->assertEquals($specs, $fixture->getSpecs()); $this->assertTrue($fixture->isATemplate()); $this->assertTrue($fixture->extendsFixtures()); - $this->assertEquals([new FixtureReference('Nelmio\Entity\User#user_base')], $fixture->getExtendedFixturesReferences()); + $this->assertEquals([new FixtureReference('user_base')], $fixture->getExtendedFixturesReferences()); $decoratedFixtureProphecy->getId()->shouldHaveBeenCalledTimes(1); - $decoratedFixtureProphecy->getReference()->shouldHaveBeenCalledTimes(1); - $decoratedFixtureProphecy->getClassName()->shouldHaveBeenCalledTimes(2); + $decoratedFixtureProphecy->getClassName()->shouldHaveBeenCalledTimes(1); $decoratedFixtureProphecy->getSpecs()->shouldHaveBeenCalledTimes(1); } diff --git a/tests/Nelmio/Alice/Definition/Fixture/TemplatingTest.php b/tests/Nelmio/Alice/Definition/Fixture/TemplatingTest.php index bcb12fced..b83e80db9 100644 --- a/tests/Nelmio/Alice/Definition/Fixture/TemplatingTest.php +++ b/tests/Nelmio/Alice/Definition/Fixture/TemplatingTest.php @@ -64,8 +64,8 @@ public function provideFlags() false, true, [ - new FixtureReference('Nelmio\Entity\User#user_base0'), - new FixtureReference('Nelmio\Entity\User#user_base1'), + new FixtureReference('user_base0'), + new FixtureReference('user_base1'), ], ]; @@ -80,8 +80,8 @@ public function provideFlags() true, true, [ - new FixtureReference('Nelmio\Entity\User#user_base0'), - new FixtureReference('Nelmio\Entity\User#user_base1'), + new FixtureReference('user_base0'), + new FixtureReference('user_base1'), ], ]; } diff --git a/tests/Nelmio/Alice/Definition/PropertyBagTest.php b/tests/Nelmio/Alice/Definition/PropertyBagTest.php index cb5ffa5df..be2bd4ede 100644 --- a/tests/Nelmio/Alice/Definition/PropertyBagTest.php +++ b/tests/Nelmio/Alice/Definition/PropertyBagTest.php @@ -89,4 +89,28 @@ public function testMergeTwoBags() $this->propRefl->getValue($bag) ); } + + public function testIsIterable() + { + $property1 = new Property('username', 'alice'); + $property2 = new Property('owner', 'bob'); + + $bag = (new PropertyBag()) + ->with($property1) + ->with($property2) + ; + + $array = []; + foreach ($bag as $index => $property) { + $array[$index] = $property; + } + + $this->assertSame( + [ + $property1, + $property2, + ], + $array + ); + } } diff --git a/tests/Nelmio/Alice/Definition/PropertyTest.php b/tests/Nelmio/Alice/Definition/PropertyTest.php index 1bb0ee39f..c9194ab14 100644 --- a/tests/Nelmio/Alice/Definition/PropertyTest.php +++ b/tests/Nelmio/Alice/Definition/PropertyTest.php @@ -39,6 +39,23 @@ public function testIsImmutable() $this->assertNotSame($definition->getValue(), $definition->getValue()); } + public function testImmutableFactories() + { + $property = 'username'; + $value = new \stdClass(); + $newValue = clone $value; + $newValue->foo = 'bar'; + + $definition = new Property($property, $value); + $newDefinition = $definition->withValue($newValue); + + $this->assertInstanceOf(Property::class, $newDefinition); + $this->assertEquals($property, $definition->getName()); + $this->assertEquals($property, $newDefinition->getName()); + $this->assertEquals($value, $definition->getValue()); + $this->assertEquals($newValue, $newDefinition->getValue()); + } + /** * @expectedException \DomainException */ diff --git a/tests/Nelmio/Alice/Definition/ServiceReference/FixtureReferenceTest.php b/tests/Nelmio/Alice/Definition/ServiceReference/FixtureReferenceTest.php index 66ee10173..75d542324 100644 --- a/tests/Nelmio/Alice/Definition/ServiceReference/FixtureReferenceTest.php +++ b/tests/Nelmio/Alice/Definition/ServiceReference/FixtureReferenceTest.php @@ -12,7 +12,6 @@ namespace Nelmio\Alice\Definition\ServiceReference; use Nelmio\Alice\Definition\ServiceReferenceInterface; -use Nelmio\Alice\FixtureInterface; /** * @covers Nelmio\Alice\Definition\ServiceReference\FixtureReference @@ -23,54 +22,12 @@ public function testIsAReference() { $this->assertTrue(is_a(FixtureReference::class, ServiceReferenceInterface::class, true)); } - - public function testAccessors() - { - $reference = 'Nelmio\Entity\User#user_base'; - $definition = new FixtureReference($reference); - - $this->assertEquals($reference, $definition->getReference()); - } - - public function testCreateAbsoluteReference() - { - $reference = 'user_base'; - $definition = new FixtureReference($reference); - - $fixtureProphecy = $this->prophesize(FixtureInterface::class); - $fixtureProphecy->getClassName()->willReturn('Nelmio\Entity\User'); - /** @var FixtureInterface $fixture */ - $fixture = $fixtureProphecy->reveal(); - - $absoluteDefinition = $definition->createAbsoluteFrom($fixture); - $this->assertEquals($reference, $definition->getReference()); - $this->assertEquals('Nelmio\Entity\User#user_base', $absoluteDefinition->getReference()); - - $fixtureProphecy->getClassName()->shouldHaveBeenCalledTimes(1); - } - - public function testCreateAbsoluteReferenceFromAbsoluteReference() + public function testAccessors() { - $reference = 'Nelmio\Entity\User#user_base'; - $definition = new FixtureReference($reference); - - $fixtureProphecy = $this->prophesize(FixtureInterface::class); - $fixtureProphecy->getId()->willReturn('Nelmio\Entity\User#user0'); - /** @var FixtureInterface $fixture */ - $fixture = $fixtureProphecy->reveal(); - - try { - $definition->createAbsoluteFrom($fixture); - $this->fail('Expected exception to be thrown.'); - } catch (\BadMethodCallException $exception) { - $this->assertEquals( - 'Attempted to make the reference "Nelmio\Entity\User#user_base" absolute from the fixture of ID ' - .'"Nelmio\Entity\User#user0", however the reference is already absolute.', - $exception->getMessage() - ); - } + $id = 'user_base'; + $definition = new FixtureReference($id); - $fixtureProphecy->getId()->shouldHaveBeenCalledTimes(1); + $this->assertEquals($id, $definition->getId()); } } diff --git a/tests/Nelmio/Alice/Definition/ServiceReference/InstantiatedReferenceTest.php b/tests/Nelmio/Alice/Definition/ServiceReference/InstantiatedReferenceTest.php index a5a777f3e..a33c35a23 100644 --- a/tests/Nelmio/Alice/Definition/ServiceReference/InstantiatedReferenceTest.php +++ b/tests/Nelmio/Alice/Definition/ServiceReference/InstantiatedReferenceTest.php @@ -28,6 +28,6 @@ public function testAccessors() $reference = 'dummy*'; $definition = new InstantiatedReference($reference); - $this->assertEquals($reference, $definition->getReference()); + $this->assertEquals($reference, $definition->getId()); } } diff --git a/tests/Nelmio/Alice/Definition/ServiceReference/StaticReferenceTest.php b/tests/Nelmio/Alice/Definition/ServiceReference/StaticReferenceTest.php index a5483b455..77c7437c1 100644 --- a/tests/Nelmio/Alice/Definition/ServiceReference/StaticReferenceTest.php +++ b/tests/Nelmio/Alice/Definition/ServiceReference/StaticReferenceTest.php @@ -28,6 +28,6 @@ public function testAccessors() $reference = 'dummy*'; $definition = new StaticReference($reference); - $this->assertEquals($reference, $definition->getReference()); + $this->assertEquals($reference, $definition->getId()); } } diff --git a/tests/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydratorTest.php b/tests/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydratorTest.php index 252e7e414..1940d87f4 100644 --- a/tests/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydratorTest.php +++ b/tests/Nelmio/Alice/Generator/Hydrator/PropertyAccessorHydratorTest.php @@ -11,6 +11,7 @@ namespace Nelmio\Alice\Generator\Hydrator; +use Nelmio\Alice\Definition\Object\SimpleObject; use Nelmio\Alice\Definition\Property; use Nelmio\Alice\Generator\HydratorInterface; use Symfony\Component\PropertyAccess\PropertyAccessor; @@ -45,7 +46,8 @@ public function testIsAnHydrator() public function testItReturnsModifiedObject() { $property = new Property('username', 'bob'); - $object = new \stdClass(); + $instance = new \stdClass(); + $object = new SimpleObject('dummy', $instance); $accessorProphecy = $this->prophesize(PropertyAccessorInterface::class); /** @var PropertyAccessorInterface $accessor */ @@ -54,7 +56,7 @@ public function testItReturnsModifiedObject() $hydrator = new PropertyAccessorHydrator($accessor); $result = $hydrator->hydrate($object, $property); - $this->assertSame($object, $result); + $this->assertEquals($object, $result); } /** @@ -62,11 +64,12 @@ public function testItReturnsModifiedObject() */ public function testSetValue(Property $property) { - $object = new Dummy(); + $instance = new Dummy(); + $object = new SimpleObject('dummy', $instance); $hydratedObject = $this->hydrator->hydrate($object , $property); $expected = $property->getValue(); - $actual = $this->propertyAccessor->getValue($hydratedObject, $property->getName()); + $actual = $this->propertyAccessor->getValue($hydratedObject->getInstance(), $property->getName()); $this->assertSame($expected, $actual); } diff --git a/tests/Nelmio/Alice/Generator/Instantiator/InstantiatorRegistryTest.php b/tests/Nelmio/Alice/Generator/Instantiator/InstantiatorRegistryTest.php index cc7a8f2cc..fec79bc8b 100644 --- a/tests/Nelmio/Alice/Generator/Instantiator/InstantiatorRegistryTest.php +++ b/tests/Nelmio/Alice/Generator/Instantiator/InstantiatorRegistryTest.php @@ -11,6 +11,8 @@ namespace Nelmio\Alice\Generator\Instantiator; +use Nelmio\Alice\Definition\Fixture\DummyFixture; +use Nelmio\Alice\Definition\Fixture\FakeFixture; use Nelmio\Alice\Definition\Object\SimpleObject; use Nelmio\Alice\FixtureBag; use Nelmio\Alice\FixtureInterface; @@ -54,9 +56,7 @@ public function testIsNotClonable() public function testIterateOverEveryInstantiatorAndUseTheFirstValidOne() { - $fixtureProphecy = $this->prophesize(FixtureInterface::class); - /** @var FixtureInterface $fixture */ - $fixture = $fixtureProphecy->reveal(); + $fixture = new FakeFixture(); $set = new ResolvedFixtureSet(new ParameterBag(), new FixtureBag(), new ObjectBag()); $expected = new ResolvedFixtureSet(new ParameterBag(), new FixtureBag(), (new ObjectBag())->with(new SimpleObject('dummy', new \stdClass()))); @@ -96,10 +96,7 @@ public function testIterateOverEveryInstantiatorAndUseTheFirstValidOne() */ public function testThrowExceptionIfNoSuitableParserIsFound() { - $fixtureProphecy = $this->prophesize(FixtureInterface::class); - $fixtureProphecy->getReference()->willReturn('dummy'); - /** @var FixtureInterface $fixture */ - $fixture = $fixtureProphecy->reveal(); + $fixture = new DummyFixture('dummy'); $set = new ResolvedFixtureSet(new ParameterBag(), new FixtureBag(), new ObjectBag()); diff --git a/tests/Nelmio/Alice/Generator/ObjectGenerator/SimpleObjectGeneratorTest.php b/tests/Nelmio/Alice/Generator/ObjectGenerator/SimpleObjectGeneratorTest.php index 1af046cf1..cd12897cc 100644 --- a/tests/Nelmio/Alice/Generator/ObjectGenerator/SimpleObjectGeneratorTest.php +++ b/tests/Nelmio/Alice/Generator/ObjectGenerator/SimpleObjectGeneratorTest.php @@ -60,9 +60,8 @@ public function testIsNotClonable() public function testGeneratorObject() { $fixtureProphecy = $this->prophesize(FixtureInterface::class); - $fixtureProphecy->getId()->willReturn('stdClass#dummy'); + $fixtureProphecy->getId()->willReturn('dummy'); $fixtureProphecy->getClassName()->willReturn('stdClass'); - $fixtureProphecy->getReference()->willReturn('dummy'); /** @var FixtureInterface $fixture */ $fixture = $fixtureProphecy->reveal(); diff --git a/tests/Nelmio/Alice/Generator/Populator/SimplePopulatorTest.php b/tests/Nelmio/Alice/Generator/Populator/SimplePopulatorTest.php new file mode 100644 index 000000000..7995daa33 --- /dev/null +++ b/tests/Nelmio/Alice/Generator/Populator/SimplePopulatorTest.php @@ -0,0 +1,184 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Nelmio\Alice\Generator\Populator; + +use Nelmio\Alice\Definition\Fixture\SimpleFixture; +use Nelmio\Alice\Definition\MethodCallBag; +use Nelmio\Alice\Definition\Object\SimpleObject; +use Nelmio\Alice\Definition\Property; +use Nelmio\Alice\Definition\PropertyBag; +use Nelmio\Alice\Definition\SpecificationBag; +use Nelmio\Alice\Definition\Value\FakeValue; +use Nelmio\Alice\FixtureBag; +use Nelmio\Alice\Generator\Hydrator\FakeHydrator; +use Nelmio\Alice\Generator\HydratorInterface; +use Nelmio\Alice\Generator\PopulatorInterface; +use Nelmio\Alice\Generator\ResolvedFixtureSet; +use Nelmio\Alice\Generator\ResolvedFixtureSetFactory; +use Nelmio\Alice\Generator\ResolvedValueWithFixtureSet; +use Nelmio\Alice\Generator\Resolver\Value\FakeValueResolver; +use Nelmio\Alice\Generator\ValueResolverInterface; +use Nelmio\Alice\ObjectBag; +use Nelmio\Alice\ParameterBag; +use Prophecy\Argument; + +/** + * @covers Nelmio\Alice\Generator\Populator\SimplePopulator + */ +class SimplePopulatorTest extends \PHPUnit_Framework_TestCase +{ + public function testIsAPopulator() + { + $this->assertTrue(is_a(SimplePopulator::class, PopulatorInterface::class, true)); + } + + public function testAddsObjectToFixtureSet() + { + $object = new SimpleObject('dummy', new \stdClass()); + $set = ResolvedFixtureSetFactory::create( + null, + $fixtures = (new FixtureBag())->with( + new SimpleFixture( + 'dummy', + \stdClass::class, + new SpecificationBag( + null, + new PropertyBag(), + new MethodCallBag() + ) + ) + ) + ); + $expected = new ResolvedFixtureSet( + new ParameterBag(), + $fixtures, + new ObjectBag(['dummy' => $object]) + ); + + $populator = new SimplePopulator(new FakeValueResolver(), new FakeHydrator()); + $actual = $populator->populate($object, $set); + + $this->assertEquals($expected, $actual); + } + + public function testHydratesObjectWithTheGivenProperties() + { + $object = new SimpleObject('dummy', new \stdClass()); + $set = ResolvedFixtureSetFactory::create( + null, + $fixtures = (new FixtureBag())->with( + new SimpleFixture( + 'dummy', + \stdClass::class, + new SpecificationBag( + null, + (new PropertyBag()) + ->with($username = new Property('username', 'Bob')) + ->with($group = new Property('group', 'Badass')), + new MethodCallBag() + ) + ) + ) + ); + + $hydratorProphecy = $this->prophesize(HydratorInterface::class); + $newInstance = new \stdClass(); + $newInstance->username = 'Bob'; + $newObject = $object->withInstance($newInstance); + $hydratorProphecy->hydrate($object, $username)->willReturn($newObject); + + $secondNewInstance = clone $newInstance; + $secondNewInstance->group = 'Badass'; + $secondNewObject = $object->withInstance($secondNewInstance); + $hydratorProphecy->hydrate($newObject, $group)->willReturn($secondNewObject); + /** @var HydratorInterface $hydrator */ + $hydrator = $hydratorProphecy->reveal(); + + $expected = new ResolvedFixtureSet( + new ParameterBag(), + $fixtures, + new ObjectBag(['dummy' => $secondNewObject]) + ); + + $populator = new SimplePopulator(new FakeValueResolver(), $hydrator); + $actual = $populator->populate($object, $set); + + $this->assertEquals($expected, $actual); + + $hydratorProphecy->hydrate(Argument::cetera())->shouldHaveBeenCalledTimes(2); + } + + public function testResolvesAllPropertyValues() + { + $object = new SimpleObject('dummy', new \stdClass()); + $set = ResolvedFixtureSetFactory::create( + null, + $fixtures = (new FixtureBag())->with( + $fixture = new SimpleFixture( + 'dummy', + \stdClass::class, + new SpecificationBag( + null, + (new PropertyBag()) + ->with($username = new Property('username', $usernameValue = new FakeValue())) + ->with($group = new Property('group', $groupValue = new FakeValue())) + , + new MethodCallBag() + ) + ) + ) + ); + + $resolverProphecy = $this->prophesize(ValueResolverInterface::class); + $setAfterFirstResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 1]), $fixtures); + $resolverProphecy + ->resolve($usernameValue, $fixture, $set, []) + ->willReturn( + new ResolvedValueWithFixtureSet('Bob', $setAfterFirstResolution) + ) + ; + + $setAfterSecondResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 2]), $fixtures); + $resolverProphecy + ->resolve($groupValue, $fixture, $setAfterFirstResolution, ['username' => 'Bob']) + ->willReturn( + new ResolvedValueWithFixtureSet('Badass', $setAfterSecondResolution) + ) + ; + /** @var ValueResolverInterface $resolver */ + $resolver = $resolverProphecy->reveal(); + + $hydratorProphecy = $this->prophesize(HydratorInterface::class); + $newInstance = new \stdClass(); + $newInstance->username = 'Bob'; + $newObject = $object->withInstance($newInstance); + $hydratorProphecy->hydrate($object, $username->withValue('Bob'))->willReturn($newObject); + + $secondNewInstance = clone $newInstance; + $secondNewInstance->group = 'Badass'; + $secondNewObject = $object->withInstance($secondNewInstance); + $hydratorProphecy->hydrate($newObject, $group->withValue('Badass'))->willReturn($secondNewObject); + /** @var HydratorInterface $hydrator */ + $hydrator = $hydratorProphecy->reveal(); + + $expected = new ResolvedFixtureSet( + new ParameterBag(['iteration' => 2]), + $fixtures, + new ObjectBag(['dummy' => $secondNewObject]) + ); + + $populator = new SimplePopulator($resolver, $hydrator); + $actual = $populator->populate($object, $set); + + $this->assertEquals($expected, $actual); + } +} diff --git a/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureBagResolverTest.php b/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureBagResolverTest.php index d53b2e36b..dc3002d1a 100644 --- a/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureBagResolverTest.php +++ b/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplateFixtureBagResolverTest.php @@ -375,7 +375,7 @@ public function testTheOrderOfFixturesGivesTheSameResult() /** * @expectedException \Nelmio\Alice\Exception\FixtureNotFoundException - * @expectedExceptionMessage Could not find the fixture "Nelmio\Entity\User#user_base". + * @expectedExceptionMessage Could not find the fixture "user_base". */ public function testThrowExceptionIfFixtureNotFound() { diff --git a/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplatingFixtureBagTest.php b/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplatingFixtureBagTest.php index 4e68c1bbc..017d6d101 100644 --- a/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplatingFixtureBagTest.php +++ b/tests/Nelmio/Alice/Generator/Resolver/Fixture/TemplatingFixtureBagTest.php @@ -11,6 +11,7 @@ namespace Nelmio\Alice\Generator\Resolver\Fixture; +use Nelmio\Alice\Definition\Fixture\DummyFixture; use Nelmio\Alice\Definition\Fixture\FixtureWithFlags; use Nelmio\Alice\Definition\Fixture\TemplatingFixture; use Nelmio\Alice\Definition\Flag\TemplateFlag; @@ -26,20 +27,13 @@ class TemplatingFixtureBagTest extends \PHPUnit_Framework_TestCase { public function testAccessors() { - $fixtureId = 'Nelmio\Entity\User#user0'; - $fixtureProphecy = $this->prophesize(FixtureInterface::class); - $fixtureProphecy->getId()->willReturn($fixtureId); - /** @var FixtureInterface $fixture */ - $fixture = $fixtureProphecy->reveal(); + $fixtureId = 'user0'; + $fixture = new DummyFixture($fixtureId); - $templateId = 'Nelmio\Entity\User#user_base'; - $templateProphecy = $this->prophesize(FixtureInterface::class); - $templateProphecy->getId()->willReturn($templateId); - /** @var FixtureInterface $template */ - $template = $templateProphecy->reveal(); + $templateId = 'user_base'; $template = new TemplatingFixture( new FixtureWithFlags( - $template, + new DummyFixture($templateId), (new FlagBag('user_base'))->with(new TemplateFlag()) ) ); diff --git a/tests/Nelmio/Alice/Generator/Resolver/Value/Chainable/DynamicArrayValueResolverTest.php b/tests/Nelmio/Alice/Generator/Resolver/Value/Chainable/DynamicArrayValueResolverTest.php index 0255925d7..229cfa87e 100644 --- a/tests/Nelmio/Alice/Generator/Resolver/Value/Chainable/DynamicArrayValueResolverTest.php +++ b/tests/Nelmio/Alice/Generator/Resolver/Value/Chainable/DynamicArrayValueResolverTest.php @@ -110,7 +110,7 @@ public function testResolvesQuantifierIfIsAValue() /** * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Expected quantifier to be an integer superior or equal to 2. Got "1" for "Nelmio\Alice\Definition\Fixture\DummyFixture#dummy", check you dynamic arrays declarations (e.g. "x @user*"). + * @expectedExceptionMessage Expected quantifier to be an integer superior or equal to 2. Got "1" for "dummy", check you dynamic arrays declarations (e.g. "x @user*"). */ public function testThrowsExceptionIfInvalidQuantifier() { diff --git a/tests/Nelmio/Alice/Generator/Resolver/Value/ResolverRegistryTest.php b/tests/Nelmio/Alice/Generator/Resolver/Value/ResolverRegistryTest.php index 919058f6b..d3a8b1afe 100644 --- a/tests/Nelmio/Alice/Generator/Resolver/Value/ResolverRegistryTest.php +++ b/tests/Nelmio/Alice/Generator/Resolver/Value/ResolverRegistryTest.php @@ -11,17 +11,15 @@ namespace Nelmio\Alice\Generator\Resolver\Value; +use Nelmio\Alice\Definition\Fixture\DummyFixture; use Nelmio\Alice\Definition\Fixture\FakeFixture; use Nelmio\Alice\Definition\Object\SimpleObject; use Nelmio\Alice\Definition\Value\FakeValue; -use Nelmio\Alice\FixtureBag; use Nelmio\Alice\FixtureInterface; -use Nelmio\Alice\Generator\ResolvedFixtureSet; use Nelmio\Alice\Generator\ResolvedFixtureSetFactory; use Nelmio\Alice\Generator\ResolvedValueWithFixtureSet; use Nelmio\Alice\Generator\ValueResolverInterface; use Nelmio\Alice\ObjectBag; -use Nelmio\Alice\ParameterBag; use Prophecy\Argument; /** @@ -102,10 +100,7 @@ public function testIterateOverEveryResolverAndUseTheFirstValidOne() */ public function testThrowExceptionIfNoSuitableParserIsFound() { - $fixtureProphecy = $this->prophesize(FixtureInterface::class); - $fixtureProphecy->getReference()->willReturn('dummy'); - /** @var FixtureInterface $fixture */ - $fixture = $fixtureProphecy->reveal(); + $fixture = new DummyFixture('dummy'); $set = ResolvedFixtureSetFactory::create(); diff --git a/tests/Nelmio/Alice/Loader/LoaderIntegrationTest.php b/tests/Nelmio/Alice/Loader/LoaderIntegrationTest.php index fe8cf81be..80c1df202 100644 --- a/tests/Nelmio/Alice/Loader/LoaderIntegrationTest.php +++ b/tests/Nelmio/Alice/Loader/LoaderIntegrationTest.php @@ -355,9 +355,7 @@ public function provideFixturesToInstantiate() [ DummyWithNamedPrivateConstructor::class => [ 'dummy' => [ - 'dummy' => [ - '__construct' => false, - ], + '__construct' => false, ], ], ], diff --git a/tests/Nelmio/Alice/ObjectBagTest.php b/tests/Nelmio/Alice/ObjectBagTest.php index c4571165a..e3eb675af 100644 --- a/tests/Nelmio/Alice/ObjectBagTest.php +++ b/tests/Nelmio/Alice/ObjectBagTest.php @@ -258,7 +258,7 @@ private function assertSameObjects(array $expected, ObjectBag $actual) private function createFixture(string $reference, string $className): FixtureInterface { $fixtureProphecy = $this->prophesize(FixtureInterface::class); - $fixtureProphecy->getReference()->willReturn($reference); + $fixtureProphecy->getId()->willReturn($reference); $fixtureProphecy->getClassName()->willReturn($className); return $fixtureProphecy->reveal();