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); }
public function testAccessors() { $context = new GenerationContext(); $this->assertTrue($context->isFirstPass()); $this->assertFalse($context->needsCompleteGeneration()); $context->setToSecondPass(); $this->assertFalse($context->isFirstPass()); $this->assertFalse($context->needsCompleteGeneration()); $context->markAsNeedsCompleteGeneration(); $this->assertFalse($context->isFirstPass()); $this->assertTrue($context->needsCompleteGeneration()); $context->unmarkAsNeedsCompleteGeneration(); $this->assertFalse($context->isFirstPass()); $this->assertFalse($context->needsCompleteGeneration()); }
/** * {@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); }
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]; })()); }
public function testIfTheReferenceRefersToAnInstantiatedFixtureAndRequiresToBeCompleteThenGenerateIt() { $value = new FixtureReferenceValue('dummy'); $fixture = new FakeFixture(); $set = ResolvedFixtureSetFactory::create(null, $fixtures = (new FixtureBag())->with($referredFixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create())), (new ObjectBag())->with(new SimpleObject('dummy', $expectedInstance = new \stdClass()))); $scope = []; $context = new GenerationContext(); $context->markAsNeedsCompleteGeneration(); $generatorContext = new GenerationContext(); $generatorContext->markIsResolvingFixture('dummy'); $generatorContext->markAsNeedsCompleteGeneration(); $generatorProphecy = $this->prophesize(ObjectGeneratorInterface::class); $generatorProphecy->generate($referredFixture, $set, $generatorContext)->willReturn($objects = new ObjectBag(['dummy' => $expectedInstance = StdClassFactory::create(['complete' => true])])); /** @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); }