Esempio n. 1
0
 public function testPicksTheFirstSuitableResolverToResolveTheGivenValue()
 {
     $value = new FakeValue();
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create();
     $scope = ['scope' => 'epocs'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $expected = new ResolvedValueWithFixtureSet(10, ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with(new SimpleObject('dummy', new \stdClass()))));
     $instantiator1Prophecy = $this->prophesize(ChainableValueResolverInterface::class);
     $instantiator1Prophecy->canResolve($value)->willReturn(false);
     /* @var ChainableValueResolverInterface $instantiator1 */
     $instantiator1 = $instantiator1Prophecy->reveal();
     $instantiator2Prophecy = $this->prophesize(ChainableValueResolverInterface::class);
     $instantiator2Prophecy->canResolve($value)->willReturn(true);
     $instantiator2Prophecy->resolve($value, $fixture, $set, $scope, $context)->willReturn($expected);
     /* @var ChainableValueResolverInterface $instantiator2 */
     $instantiator2 = $instantiator2Prophecy->reveal();
     $instantiator3Prophecy = $this->prophesize(ChainableValueResolverInterface::class);
     $instantiator3Prophecy->canResolve(Argument::any())->shouldNotBeCalled();
     /* @var ChainableValueResolverInterface $instantiator3 */
     $instantiator3 = $instantiator3Prophecy->reveal();
     $registry = new ValueResolverRegistry([$instantiator1, $instantiator2, $instantiator3]);
     $actual = $registry->resolve($value, $fixture, $set, $scope, $context);
     $this->assertSame($expected, $actual);
     $instantiator1Prophecy->canResolve(Argument::any())->shouldHaveBeenCalledTimes(1);
     $instantiator2Prophecy->canResolve(Argument::any())->shouldHaveBeenCalledTimes(1);
     $instantiator2Prophecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
 public function testReturnsSetWithResolvedValue()
 {
     $value = new FixturePropertyValue($reference = new FakeValue(), $property = 'prop');
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar']));
     $scope = ['val' => 'scopie'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $valueResolverContext = new GenerationContext();
     $valueResolverContext->markIsResolvingFixture('foo');
     $valueResolverContext->markAsNeedsCompleteGeneration();
     $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $valueResolverProphecy->resolve($reference, $fixture, $set, $scope, $valueResolverContext)->willReturn(new ResolvedValueWithFixtureSet($instance = new \stdClass(), $newSet = ResolvedFixtureSetFactory::create(new ParameterBag(['ping' => 'pong']))));
     /** @var ValueResolverInterface $valueResolver */
     $valueResolver = $valueResolverProphecy->reveal();
     $propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
     $propertyAccessorProphecy->getValue($instance, $property)->willReturn('yo');
     /** @var PropertyAccessorInterface $propertyAccessor */
     $propertyAccessor = $propertyAccessorProphecy->reveal();
     $expected = new ResolvedValueWithFixtureSet('yo', $newSet);
     $resolver = new FixturePropertyReferenceResolver($propertyAccessor, $valueResolver);
     $actual = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
     $valueResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1);
     $propertyAccessorProphecy->getValue(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
Esempio n. 3
0
 /**
  * @testdox Do a instantiate-hydrate-calls cycle to generate the object described by the fixture.
  */
 public function testGenerate()
 {
     $this->markTestIncomplete('TODO');
     $fixture = new SimpleFixture('dummy', \stdClass::class, SpecificationBagFactory::create());
     $set = ResolvedFixtureSetFactory::create();
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $instance = new \stdClass();
     $instantiatedObject = new SimpleObject($fixture->getId(), $instance);
     $instantiatorProphecy = $this->prophesize(InstantiatorInterface::class);
     $instantiatorProphecy->instantiate($fixture, $set, $context)->willReturn($setWithInstantiatedObject = ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with($instantiatedObject)));
     /** @var InstantiatorInterface $instantiator */
     $instantiator = $instantiatorProphecy->reveal();
     $hydratedInstance = clone $instance;
     $hydratedInstance->hydrated = true;
     $hydratedObject = new SimpleObject($fixture->getId(), $hydratedInstance);
     $hydratorProphecy = $this->prophesize(HydratorInterface::class);
     $hydratorProphecy->hydrate($instantiatedObject, $setWithInstantiatedObject, $context)->willReturn($setWithHydratedObject = ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with($hydratedObject)));
     /** @var HydratorInterface $hydrator */
     $hydrator = $hydratorProphecy->reveal();
     $instanceAfterCalls = clone $hydratedInstance;
     $instanceAfterCalls->calls = true;
     $objectAfterCalls = new SimpleObject($fixture->getId(), $instanceAfterCalls);
     $callerProphecy = $this->prophesize(CallerInterface::class);
     $callerProphecy->doCallsOn($hydratedObject, $setWithHydratedObject)->willReturn($setWithObjectAfterCalls = ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with($objectAfterCalls)));
     /** @var CallerInterface $caller */
     $caller = $callerProphecy->reveal();
     $generator = new SimpleObjectGenerator(new FakeValueResolver(), $instantiator, $hydrator, $caller);
     $objects = $generator->generate($fixture, $set, $context);
     $this->assertEquals($setWithObjectAfterCalls->getObjects(), $objects);
     $instantiatorProphecy->instantiate(Argument::cetera())->shouldHaveBeenCalledTimes(1);
     $hydratorProphecy->hydrate(Argument::cetera())->shouldHaveBeenCalledTimes(1);
     $callerProphecy->doCallsOn(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
Esempio n. 4
0
 public function testIterateOverEveryInstantiatorAndUseTheFirstValidOne()
 {
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create();
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $expected = ResolvedFixtureSetFactory::create(null, null, (new ObjectBag())->with(new SimpleObject('dummy', new \stdClass())));
     $instantiator1Prophecy = $this->prophesize(ChainableInstantiatorInterface::class);
     $instantiator1Prophecy->canInstantiate($fixture)->willReturn(false);
     /* @var ChainableInstantiatorInterface $instantiator1 */
     $instantiator1 = $instantiator1Prophecy->reveal();
     $instantiator2Prophecy = $this->prophesize(ChainableInstantiatorInterface::class);
     $instantiator2Prophecy->canInstantiate($fixture)->willReturn(true);
     $instantiator2Prophecy->instantiate($fixture, $set, $context)->willReturn($expected);
     /* @var ChainableInstantiatorInterface $instantiator2 */
     $instantiator2 = $instantiator2Prophecy->reveal();
     $instantiator3Prophecy = $this->prophesize(ChainableInstantiatorInterface::class);
     $instantiator3Prophecy->canInstantiate(Argument::any())->shouldNotBeCalled();
     /* @var ChainableInstantiatorInterface $instantiator3 */
     $instantiator3 = $instantiator3Prophecy->reveal();
     $registry = new InstantiatorRegistry([$instantiator1, $instantiator2, $instantiator3]);
     $actual = $registry->instantiate($fixture, $set, $context);
     $this->assertSame($expected, $actual);
     $instantiator1Prophecy->canInstantiate(Argument::any())->shouldHaveBeenCalledTimes(1);
     $instantiator2Prophecy->canInstantiate(Argument::any())->shouldHaveBeenCalledTimes(1);
     $instantiator2Prophecy->instantiate(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
Esempio n. 5
0
 /**
  * @inheritdoc
  */
 public function generate(FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : ObjectBag
 {
     if ($context->isFirstPass()) {
         $fixtureSet = $this->instantiator->instantiate($fixture, $fixtureSet, $context)->getObjects();
         if (false === $context->needsCompleteGeneration()) {
             return $fixtureSet;
         }
     }
     $fixtureSet = $this->completeObject($fixture, $fixtureSet, $context);
     return $fixtureSet->getObjects();
 }
Esempio n. 6
0
 public function testCannotRetrieveAnInexistingValueFromCache()
 {
     $context = new GenerationContext();
     try {
         $context->getCachedValue('foo');
         $this->fail('Expected exception to be thrown.');
     } catch (CachedValueNotFound $exception) {
         $this->assertEquals('No value with the key "foo" was found in the cache.', $exception->getMessage());
         $this->assertEquals(0, $exception->getCode());
         $this->assertNull($exception->getPrevious());
     }
 }
Esempio n. 7
0
 public function testThrowsAnExceptionWhenACircularReferenceIsDetected()
 {
     $context = new GenerationContext();
     $context->markIsResolvingFixture('bar');
     $context->markIsResolvingFixture('foo');
     try {
         $context->markIsResolvingFixture('foo');
         $this->fail('Expected exception to be thrown.');
     } catch (CircularReferenceException $exception) {
         $this->assertEquals('Circular reference detected for the parameter "foo" while resolving ["bar", "foo"].', $exception->getMessage());
     }
 }
 public function testReturnsTheResultOfTheDecoratedInstantiatorIfTheFixtureHasNotBeenInstantiated()
 {
     $fixture = new DummyFixture('dummy');
     $set = ResolvedFixtureSetFactory::create();
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $decoratedInstantiatorProphecy = $this->prophesize(InstantiatorInterface::class);
     $decoratedInstantiatorProphecy->instantiate($fixture, $set, $context)->willReturn($expected = $set->withObjects((new ObjectBag())->with(new SimpleObject('dummy', new \stdClass()))));
     /** @var InstantiatorInterface $decoratedInstantiator */
     $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal();
     $instantiator = new ExistingInstanceInstantiator($decoratedInstantiator);
     $actual = $instantiator->instantiate($fixture, $set, $context);
     $this->assertSame($expected, $actual);
     $decoratedInstantiatorProphecy->instantiate(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
 public function testReturnsSetWithResolvedValue()
 {
     $value = new FunctionCallValue('foo');
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar']));
     $scope = ['val' => 'scopie'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $fakerGeneratorProphecy = $this->prophesize(FakerGenerator::class);
     $fakerGeneratorProphecy->format('foo', [])->willReturn('bar');
     /** @var FakerGenerator $fakerGenerator */
     $fakerGenerator = $fakerGeneratorProphecy->reveal();
     $expected = new ResolvedValueWithFixtureSet('bar', $set);
     $resolver = new FakerFunctionCallValueResolver($fakerGenerator);
     $actual = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
 }
Esempio n. 10
0
 public function testResolvesAllTheValuesInArrayBeforeImplodingIt()
 {
     $value = new ListValue(['a', new FakeValue(), 'c', new FakeValue()]);
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar']));
     $scope = ['scope' => 'epocs'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $valueResolverProphecy->resolve(new FakeValue(), $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet('b', $newSet = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'baz']))));
     $valueResolverProphecy->resolve(new FakeValue(), $fixture, $newSet, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet('d', $newSet2 = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'zab']))));
     /** @var ValueResolverInterface $valueResolver */
     $valueResolver = $valueResolverProphecy->reveal();
     $expected = new ResolvedValueWithFixtureSet('abcd', $newSet2);
     $resolver = new ListValueResolver($valueResolver);
     $actual = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
     $valueResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(2);
 }
 /**
  * {@inheritdoc}
  *
  * @param FixturePropertyValue $value
  *
  * @throws NoSuchPropertyException
  * @throws UnresolvableValueException
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $context->markAsNeedsCompleteGeneration();
     $fixtureReferenceResult = $this->resolver->resolve($value->getReference(), $fixture, $fixtureSet, $scope, $context);
     $context->unmarkAsNeedsCompleteGeneration();
     /** @var ResolvedFixtureSet $fixtureSet */
     list($instance, $fixtureSet) = [$fixtureReferenceResult->getValue(), $fixtureReferenceResult->getSet()];
     try {
         $propertyValue = $this->propertyAccessor->getValue($instance, $value->getProperty());
     } catch (SymfonyNoSuchPropertyException $exception) {
         throw NoSuchPropertyExceptionFactory::createForFixture($fixture, $value, 0, $exception);
     } catch (\Exception $exception) {
         throw UnresolvableValueExceptionFactory::create($value, 0, $exception);
     }
     return new ResolvedValueWithFixtureSet($propertyValue, $fixtureSet);
 }
 /**
  * Gets all the fixture IDs suitable for the given value.
  *
  * @param FixtureMatchReferenceValue $value
  * @param ResolvedFixtureSet         $fixtureSet
  * @param GenerationContext          $context
  *
  * @return string[]
  */
 private function getSuitableIds(FixtureMatchReferenceValue $value, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : array
 {
     $pattern = $value->getValue();
     try {
         $cache = $context->getCachedValue(self::IDS_BY_PATTERN_CACHE_KEY);
     } catch (CachedValueNotFound $exception) {
         $cache = [];
     }
     if (array_key_exists($pattern, $cache)) {
         return $cache[$pattern];
     }
     $suitableIds = $this->findSuitableIds($pattern, $fixtureSet);
     $cache[$pattern] = $suitableIds;
     $context->cacheValue(self::IDS_BY_PATTERN_CACHE_KEY, $cache);
     return $suitableIds;
 }
 public function testReturnsResultOfTheDecoratedResolverIfFunctionIsBlacklisted()
 {
     $value = new FunctionCallValue('strtolower', ['BAR']);
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar']));
     $scope = ['val' => 'scopie'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $decoratedResolverProphecy->resolve($value, $fixture, $set, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet('bar', ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar', 'ping' => 'pong']))));
     /** @var ValueResolverInterface $decoratedResolver */
     $decoratedResolver = $decoratedResolverProphecy->reveal();
     $resolver = new PhpFunctionCallValueResolver(['strtolower'], $decoratedResolver);
     $actual = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
     $decoratedResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
Esempio n. 14
0
 private function isObjectComplete(FixtureInterface $fixture, ObjectInterface $object, GenerationContext $context) : bool
 {
     return $object instanceof CompleteObject || $context->needsCompleteGeneration() || false === $context->isFirstPass() || false === $context->needsCompleteGeneration() && $fixture->getSpecs()->getProperties()->isEmpty() && $fixture->getSpecs()->getMethodCalls()->isEmpty();
 }
 public function testIfTheReferenceRefersToANonInstantiatedFixtureThenGenerateItBeforeReturningTheInstance()
 {
     $value = new FixtureReferenceValue('dummy');
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create(null, $fixtures = (new FixtureBag())->with($referredFixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create())), null);
     $scope = [];
     $context = new GenerationContext();
     $generatorContext = new GenerationContext();
     $generatorContext->markIsResolvingFixture('dummy');
     $generatorProphecy = $this->prophesize(ObjectGeneratorInterface::class);
     $generatorProphecy->generate($referredFixture, $set, $generatorContext)->willReturn($objects = new ObjectBag(['dummy' => $expectedInstance = new \stdClass()]));
     /** @var ObjectGeneratorInterface $generator */
     $generator = $generatorProphecy->reveal();
     $expected = new ResolvedValueWithFixtureSet($expectedInstance, ResolvedFixtureSetFactory::create(null, $fixtures, $objects));
     $resolver = new FixtureReferenceResolver($generator);
     $actual = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
     $this->assertEquals($context, $generatorContext);
     $generatorProphecy->generate(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
 public function testResolvesElementAsManyTimeAsNecessaryIfItIsAValue()
 {
     $quantifier = 2;
     $element = new FakeValue();
     $value = new DynamicArrayValue($quantifier, $element);
     $fixture = new DummyFixture('dummy');
     $set = ResolvedFixtureSetFactory::create();
     $scope = ['injected' => true];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('bar');
     $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $setAfterFirstResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 0]));
     $decoratedResolverProphecy->resolve($element, $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet(10, $setAfterFirstResolution));
     $setAfterSecondResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 1]));
     $decoratedResolverProphecy->resolve($element, $fixture, $setAfterFirstResolution, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet(100, $setAfterSecondResolution));
     /** @var ValueResolverInterface $decoratedResolver */
     $decoratedResolver = $decoratedResolverProphecy->reveal();
     $resolver = new DynamicArrayValueResolver($decoratedResolver);
     $result = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertSame([10, 100], $result->getValue());
     $this->assertEquals($setAfterSecondResolution, $result->getSet());
 }
 public function testResolvesReferenceBeforeHandingOverTheResolutionToTheDecoratedResolver()
 {
     $idValue = new FakeValue();
     $value = new FixtureReferenceValue($idValue);
     $expectedObject = new \stdClass();
     $expectedObject->foo = 'bar';
     $set = ResolvedFixtureSetFactory::create(null, $fixtureBag = (new FixtureBag())->with($dummyFixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()))->with($anotherDummyFixture = new SimpleFixture('another_dummy', 'Dummy', SpecificationBagFactory::create())), $objectBag = new ObjectBag(['dummy' => $expectedObject]));
     $scope = ['injected' => true];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('bar');
     $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $valueResolverProphecy->resolve($idValue, $dummyFixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet('alice', $newSet = ResolvedFixtureSetFactory::create(null, $fixtureBag->with(new SimpleFixture('value_resolver_fixture', 'Dummy', SpecificationBagFactory::create())), $newObjectBag = $objectBag->with(new SimpleObject('value_resolver_fixture', new \stdClass())))));
     /** @var ValueResolverInterface $valueResolver */
     $valueResolver = $valueResolverProphecy->reveal();
     $decoratedResolverProphecy = $this->prophesize(ChainableValueResolverInterface::class);
     $decoratedResolverProphecy->resolve(new FixtureReferenceValue('alice'), $dummyFixture, $newSet, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet($expectedObject, ResolvedFixtureSetFactory::create(null, $fixtureBag, $newObjectBag->with(new SimpleObject('alice', $expectedObject)))));
     /** @var ChainableValueResolverInterface $decoratedResolver */
     $decoratedResolver = $decoratedResolverProphecy->reveal();
     $resolver = new UnresolvedFixtureReferenceIdResolver($decoratedResolver, $valueResolver);
     $actual = $resolver->resolve($value, $dummyFixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
     $valueResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1);
     $decoratedResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
Esempio n. 18
0
 public function testThrowsIfLimitForGenerationOfUniqueValuesIsReached()
 {
     $uniqueId = 'uniqid';
     $realValue = new FakeValue();
     $value = new UniqueValue($uniqueId, $realValue);
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create();
     $scope = ['scope' => 'epocs'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $pool = new UniqueValuesPool();
     $pool->add(new UniqueValue($uniqueId, 10));
     $pool->add(new UniqueValue($uniqueId, 11));
     $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $decoratedResolverProphecy->resolve($realValue, $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet(10, $set));
     /** @var ValueResolverInterface $decoratedResolver */
     $decoratedResolver = $decoratedResolverProphecy->reveal();
     $resolver = new UniqueValueResolver($pool, $decoratedResolver);
     try {
         $resolver->resolve($value, $fixture, $set, $scope, $context);
         $this->fail('Expected exception to be thrown.');
     } catch (UniqueValueGenerationLimitReachedException $exception) {
         $decoratedResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(150);
     }
 }
Esempio n. 19
0
 /**
  * @param FixtureIdInterface|FixtureInterface $referredFixture
  * @param string                              $referredFixtureId
  * @param ResolvedFixtureSet                  $fixtureSet
  * @param GenerationContext                   $context
  *
  * @return ResolvedValueWithFixtureSet
  */
 private function resolveReferredFixture(FixtureIdInterface $referredFixture, string $referredFixtureId, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if ($fixtureSet->getObjects()->has($referredFixture)) {
         $referredObject = $fixtureSet->getObjects()->get($referredFixture);
         if ($referredObject instanceof CompleteObject || false === $context->needsCompleteGeneration()) {
             return new ResolvedValueWithFixtureSet($referredObject->getInstance(), $fixtureSet);
         }
     }
     // Object is either not completely generated or has not been generated at all yet
     if (false === $referredFixture instanceof FixtureInterface) {
         throw FixtureNotFoundExceptionFactory::create($referredFixtureId);
     }
     $context->markIsResolvingFixture($referredFixtureId);
     $objects = $this->generator->generate($referredFixture, $fixtureSet, $context);
     $fixtureSet = $fixtureSet->withObjects($objects);
     return new ResolvedValueWithFixtureSet($fixtureSet->getObjects()->get($referredFixture)->getInstance(), $fixtureSet);
 }
Esempio n. 20
0
 public function provideSets()
 {
     (yield 'decorated generator generates a complete object => complete object' => (function () {
         $fixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create(null, (new PropertyBag())->with(new Property('foo', 'bar'))));
         $context = new GenerationContext();
         $decoratedGeneratorProphecy = $this->prophesize(ObjectGeneratorInterface::class);
         $decoratedGeneratorProphecy->generate(Argument::cetera())->willReturn((new ObjectBag())->with(new CompleteObject(new SimpleObject('dummy', new \stdClass()))));
         /** @var ObjectGeneratorInterface $decoratedGenerator */
         $decoratedGenerator = $decoratedGeneratorProphecy->reveal();
         $expected = (new ObjectBag())->with(new CompleteObject(new CompleteObject(new SimpleObject('dummy', new \stdClass()))));
         return [$fixture, $context, $decoratedGenerator, $expected];
     })());
     (yield 'object has been generated during the second pass => complete object' => (function () {
         $fixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create(null, (new PropertyBag())->with(new Property('foo', 'bar'))));
         $context = new GenerationContext();
         $context->setToSecondPass();
         $decoratedGeneratorProphecy = $this->prophesize(ObjectGeneratorInterface::class);
         $decoratedGeneratorProphecy->generate(Argument::cetera())->willReturn((new ObjectBag())->with(new SimpleObject('dummy', new \stdClass())));
         /** @var ObjectGeneratorInterface $decoratedGenerator */
         $decoratedGenerator = $decoratedGeneratorProphecy->reveal();
         $expected = (new ObjectBag())->with(new CompleteObject(new SimpleObject('dummy', new \stdClass())));
         return [$fixture, $context, $decoratedGenerator, $expected];
     })());
     (yield 'object was generated with "complete object" generation context => complete object' => (function () {
         $fixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create(null, (new PropertyBag())->with(new Property('foo', 'bar'))));
         $context = new GenerationContext();
         $context->markAsNeedsCompleteGeneration();
         $decoratedGeneratorProphecy = $this->prophesize(ObjectGeneratorInterface::class);
         $decoratedGeneratorProphecy->generate(Argument::cetera())->willReturn((new ObjectBag())->with(new SimpleObject('dummy', new \stdClass())));
         /** @var ObjectGeneratorInterface $decoratedGenerator */
         $decoratedGenerator = $decoratedGeneratorProphecy->reveal();
         $expected = (new ObjectBag())->with(new CompleteObject(new SimpleObject('dummy', new \stdClass())));
         return [$fixture, $context, $decoratedGenerator, $expected];
     })());
     (yield 'object generated needed only instantiation => complete object' => (function () {
         $fixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create());
         $context = new GenerationContext();
         $decoratedGeneratorProphecy = $this->prophesize(ObjectGeneratorInterface::class);
         $decoratedGeneratorProphecy->generate(Argument::cetera())->willReturn((new ObjectBag())->with(new SimpleObject('dummy', new \stdClass())));
         /** @var ObjectGeneratorInterface $decoratedGenerator */
         $decoratedGenerator = $decoratedGeneratorProphecy->reveal();
         $expected = (new ObjectBag())->with(new CompleteObject(new SimpleObject('dummy', new \stdClass())));
         return [$fixture, $context, $decoratedGenerator, $expected];
     })());
     (yield 'object generated during first pass => unchanged' => (function () {
         $fixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create(null, (new PropertyBag())->with(new Property('foo', 'bar'))));
         $context = new GenerationContext();
         $decoratedGeneratorProphecy = $this->prophesize(ObjectGeneratorInterface::class);
         $decoratedGeneratorProphecy->generate(Argument::cetera())->willReturn($expected = (new ObjectBag())->with(new SimpleObject('dummy', new \stdClass())));
         /** @var ObjectGeneratorInterface $decoratedGenerator */
         $decoratedGenerator = $decoratedGeneratorProphecy->reveal();
         return [$fixture, $context, $decoratedGenerator, $expected];
     })());
 }
Esempio n. 21
0
 public function testDoesNotResolveArgumentsIfSpecifiedNoConstructor()
 {
     $specs = SpecificationBagFactory::create();
     $fixture = new SimpleFixture('dummy', 'stdClass', $specs);
     $set = ResolvedFixtureSetFactory::create();
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $expected = ResolvedFixtureSetFactory::create(null, (new FixtureBag())->with($fixture), new ObjectBag(['dummy' => new \stdClass()]));
     $resolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $resolverProphecy->resolve(Argument::cetera())->shouldNotBeCalled();
     /** @var ValueResolverInterface $resolver */
     $resolver = $resolverProphecy->reveal();
     $decoratedInstantiatorProphecy = $this->prophesize(InstantiatorInterface::class);
     $decoratedInstantiatorProphecy->instantiate($fixture, $set, $context)->willReturn($expected);
     /** @var InstantiatorInterface $decoratedInstantiator */
     $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal();
     $instantiator = new InstantiatorResolver($decoratedInstantiator, $resolver);
     $actual = $instantiator->instantiate($fixture, $set, $context);
     $this->assertSame($expected, $actual);
 }
 public function testResolvesAllArgumentsValuesBeforePassingThemToTheDecoratedResolver()
 {
     $value = new FunctionCallValue('foo', ['scalar', new FakeValue(), 'another scalar']);
     $fixture = new FakeFixture();
     $set = ResolvedFixtureSetFactory::create(new ParameterBag(['foo' => 'bar']));
     $scope = ['val' => 'scopie'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $argumentResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $argumentResolverProphecy->resolve(new FakeValue(), $fixture, $set, $scope, $context)->willReturn(new ResolvedValueWithFixtureSet($instance = new \stdClass(), $newSet = ResolvedFixtureSetFactory::create(new ParameterBag(['ping' => 'pong']))));
     /** @var ValueResolverInterface $argumentResolver */
     $argumentResolver = $argumentResolverProphecy->reveal();
     $decoratedResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $decoratedResolverProphecy->resolve(new FunctionCallValue('foo', ['scalar', $instance, 'another scalar']), $fixture, $newSet, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet('end', ResolvedFixtureSetFactory::create(new ParameterBag(['gnip' => 'gnop']))));
     /** @var ValueResolverInterface $decoratedResolver */
     $decoratedResolver = $decoratedResolverProphecy->reveal();
     $resolver = new FunctionCallArgumentResolver($decoratedResolver, $argumentResolver);
     $actual = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
 }
 public function testReturnsResultOfTheDecoratedResolverIfReferenceDoesNotMatchSelf()
 {
     $valueProphecy = $this->prophesize(ValueInterface::class);
     $valueProphecy->getValue()->willReturn('a_random_fixture_id');
     /** @var ValueInterface $value */
     $value = $valueProphecy->reveal();
     $expectedObject = new \stdClass();
     $expectedObject->foo = 'bar';
     $set = ResolvedFixtureSetFactory::create(null, $fixtureBag = (new FixtureBag())->with($dummyFixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()))->with($anotherDummyFixture = new SimpleFixture('another_dummy', 'Dummy', SpecificationBagFactory::create())), new ObjectBag(['dummy' => $expectedObject]));
     $scope = ['injected' => true];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('bar');
     $decoratedResolverProphecy = $this->prophesize(ChainableValueResolverInterface::class);
     $decoratedResolverProphecy->resolve($value, $dummyFixture, $set, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet($resolvedFixture = new SimpleFixture('resolved_fixture', 'Dummy', SpecificationBagFactory::create()), ResolvedFixtureSetFactory::create(null, $fixtureBag->with($resolvedFixture))));
     /** @var ChainableValueResolverInterface $decoratedResolver */
     $decoratedResolver = $decoratedResolverProphecy->reveal();
     $resolver = new SelfFixtureReferenceResolver($decoratedResolver);
     $actual = $resolver->resolve($value, $dummyFixture, $set, $scope, $context);
     $this->assertEquals($expected, $actual);
     $valueProphecy->getValue()->shouldHaveBeenCalledTimes(1);
     $decoratedResolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(1);
 }
Esempio n. 24
0
 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()))));
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $resolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $setAfterFirstResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 1]), $fixtures);
     $resolverProphecy->resolve($usernameValue, $fixture, $set, ['_instances' => $set->getObjects()->toArray()], $context)->willReturn(new ResolvedValueWithFixtureSet('Bob', $setAfterFirstResolution));
     $setAfterSecondResolution = ResolvedFixtureSetFactory::create(new ParameterBag(['iteration' => 2]), $fixtures);
     $resolverProphecy->resolve($groupValue, $fixture, $setAfterFirstResolution, ['_instances' => $set->getObjects()->toArray(), 'username' => 'Bob'], $context)->willReturn(new ResolvedValueWithFixtureSet('Badass', $setAfterSecondResolution));
     /** @var ValueResolverInterface $resolver */
     $resolver = $resolverProphecy->reveal();
     $hydratorProphecy = $this->prophesize(PropertyHydratorInterface::class);
     $newInstance = new \stdClass();
     $newInstance->username = '******';
     $newObject = $object->withInstance($newInstance);
     $hydratorProphecy->hydrate($object, $username->withValue('Bob'), $context)->willReturn($newObject);
     $secondNewInstance = clone $newInstance;
     $secondNewInstance->group = 'Badass';
     $secondNewObject = $object->withInstance($secondNewInstance);
     $hydratorProphecy->hydrate($newObject, $group->withValue('Badass'), $context)->willReturn($secondNewObject);
     /** @var PropertyHydratorInterface $hydrator */
     $hydrator = $hydratorProphecy->reveal();
     $expected = new ResolvedFixtureSet(new ParameterBag(['iteration' => 2]), $fixtures, new ObjectBag(['dummy' => $secondNewObject]));
     $hydrator = new SimpleHydrator($hydrator, $resolver);
     $actual = $hydrator->hydrate($object, $set, $context);
     $this->assertEquals($expected, $actual);
 }
 public function testTheMatchingFixtureCanBeFromLoadedFixtures()
 {
     $value = FixtureMatchReferenceValue::createWildcardReference('dummy');
     $fixture = new DummyFixture('injected_fixture');
     $set = ResolvedFixtureSetFactory::create(null, (new FixtureBag())->with(new DummyFixture('dummy')));
     $scope = ['foo' => 'bar'];
     $context = new GenerationContext();
     $context->markIsResolvingFixture('foo');
     $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class);
     $valueResolverProphecy->resolve(new FixtureReferenceValue('dummy'), $fixture, $set, $scope, $context)->willReturn($expected = new ResolvedValueWithFixtureSet('dummy', $newSet = ResolvedFixtureSetFactory::create(new ParameterBag(['ping' => 'pong']), (new FixtureBag())->with($fixture))));
     /** @var ValueResolverInterface $valueResolver */
     $valueResolver = $valueResolverProphecy->reveal();
     $resolver = new FixtureWildcardReferenceResolver($valueResolver);
     $actual = $resolver->resolve($value, $fixture, $set, $scope, $context);
     $this->assertSame($expected, $actual);
 }