/** * @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); }
public function testWithersReturnNewModifiedInstance() { $reference = 'user0'; $specs = SpecificationBagFactory::create(); $newSpecs = SpecificationBagFactory::create(new FakeMethodCall()); $newDecoratedFixtureProphecy = $this->prophesize(FixtureInterface::class); $newDecoratedFixtureProphecy->getSpecs()->willReturn($newSpecs); /** @var FixtureInterface $newDecoratedFixture */ $newDecoratedFixture = $newDecoratedFixtureProphecy->reveal(); $decoratedFixtureProphecy = $this->prophesize(FixtureInterface::class); $decoratedFixtureProphecy->getId()->willReturn($reference); $decoratedFixtureProphecy->withSpecs($newSpecs)->willReturn($newDecoratedFixture); $decoratedFixtureProphecy->getSpecs()->willReturn($specs); /** @var FixtureInterface $decoratedFixture */ $decoratedFixture = $decoratedFixtureProphecy->reveal(); $flags = new FlagBag('user0'); $fixture = new SimpleFixtureWithFlags($decoratedFixture, $flags); $newFixture = $fixture->withSpecs($newSpecs); $this->assertInstanceOf(SimpleFixtureWithFlags::class, $newFixture); $this->assertNotSame($fixture, $newFixture); $this->assertEquals($specs, $fixture->getSpecs()); $this->assertEquals($flags, $fixture->getFlags()); $this->assertEquals($newSpecs, $newFixture->getSpecs()); $this->assertEquals($flags, $newFixture->getFlags()); }
public function testTestCreateForInvalidInstanceType() { $exception = InstantiationExceptionFactory::createForInvalidInstanceType(new SimpleFixture('foo', 'Dummy', SpecificationBagFactory::create()), new \stdClass()); $this->assertEquals('Instantiated fixture was expected to be an instance of "Dummy". Got "stdClass" instead.', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); $this->assertNull($exception->getPrevious()); }
public function testDenormalizesListToBuildFixtures() { $className = 'Nelmio\\Alice\\Entity\\User'; $fixtures = $expected = (new FixtureBag())->with(new TemplatingFixture(new SimpleFixtureWithFlags(new SimpleFixture('user1', $className, SpecificationBagFactory::create(), 'alice'), new FlagBag('user1'))))->with(new TemplatingFixture(new SimpleFixtureWithFlags(new SimpleFixture('user2', $className, SpecificationBagFactory::create(), 'bob'), new FlagBag('user2')))); $reference = 'user{1..2}'; $specs = ['username' => '<name()>']; $flags = new FlagBag(''); $denormalizer = new NullListNameDenormalizer(); $actual = $denormalizer->denormalize($fixtures, $className, $reference, $specs, $flags); $this->assertSame($expected, $actual); }
/** * @depends Nelmio\Alice\FixtureBagTest::testIsImmutable */ public function testIsImmutable() { $fixture = new MutableFixture('user0', 'Nelmio\\Alice\\Entity\\User', SpecificationBagFactory::create()); $originalFixture = deep_clone($fixture); $bag = (new TemplatingFixtureBag())->with($fixture); // Mutate injected value $fixture->setSpecs(SpecificationBagFactory::create(new FakeMethodCall())); // Mutate retrieved fixture $bag->getFixtures()->get('user0')->setSpecs(SpecificationBagFactory::create(new NoMethodCall())); $this->assertEquals($originalFixture, $bag->getFixtures()->get('user0')); }
public function testCurrentThrowsAnExceptionIfFixtureHasNoCurrentValue() { $fixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()); try { AliceProvider::current($fixture); $this->fail('Expected exception to be thrown.'); } catch (NoValueForCurrentException $exception) { $this->assertEquals('No value for \'<current()>\' found for the fixture "dummy".', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); $this->assertNull($exception->getPrevious()); } }
public function testMergeBagsWillReturnANewInstanceWithTheMergedFixtures() { $fixture1 = new DummyFixture('foo'); $fixture2 = new MutableFixture('foo', 'Nelmio\\Alice\\Entity\\User', SpecificationBagFactory::create()); $fixture3 = new DummyFixture('bar'); $bag1 = (new FixtureBag())->with($fixture1); $bag2 = (new FixtureBag())->with($fixture2)->with($fixture3); $bag3 = $bag1->mergeWith($bag2); $this->assertInstanceOf(FixtureBag::class, $bag2); $this->assertSameFixtures(['foo' => $fixture1], $bag1); $this->assertSameFixtures(['foo' => $fixture2, 'bar' => $fixture3], $bag2); $this->assertSameFixtures(['foo' => $fixture2, 'bar' => $fixture3], $bag3); }
public function testWithersReturnNewModifiedInstance() { $reference = 'user0'; $className = 'Nelmio\\Alice\\Entity\\User'; $specs = SpecificationBagFactory::create(); $newSpecs = SpecificationBagFactory::create(new DummyMethodCall('dummy')); $fixture = new SimpleFixture($reference, $className, $specs); $newFixture = $fixture->withSpecs($newSpecs); $this->assertInstanceOf(SimpleFixture::class, $newFixture); $this->assertNotSame($fixture, $newFixture); $this->assertEquals($specs, $fixture->getSpecs()); $this->assertEquals($newSpecs, $newFixture->getSpecs()); }
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 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); }
public function testThrowsAnExceptionIfResolvedReferenceHasNoSuchProperty() { try { $value = new FixturePropertyValue($reference = new FakeValue(), $property = 'prop'); $instance = new \stdClass(); $instance->prop = 'foo'; $set = ResolvedFixtureSetFactory::create(); $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class); $valueResolverProphecy->resolve(Argument::cetera())->willReturn(new ResolvedValueWithFixtureSet(new \stdClass(), $set)); /** @var ValueResolverInterface $valueResolver */ $valueResolver = $valueResolverProphecy->reveal(); $resolver = new FixturePropertyReferenceResolver(PropertyAccess::createPropertyAccessor(), $valueResolver); $resolver->resolve($value, new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()), $set, [], new GenerationContext()); $this->fail('Expected exception to be thrown.'); } catch (NoSuchPropertyException $exception) { $this->assertEquals('Could not find the property "prop" of the object "dummy" (class: Dummy).', $exception->getMessage()); $this->assertEquals(0, $exception->getCode()); $this->assertNotNull($exception->getPrevious()); } }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage Could not instantiate fixture "dummy". */ public function testThrowsAnExceptionIfCouldNotInstantiateObject() { $fixture = new SimpleFixture('dummy', AbstractDummyWithRequiredParameterInConstructor::class, SpecificationBagFactory::create(new SimpleMethodCall('fake', [10]))); $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext()); }
/** * @inheritdoc */ public function denormalize(FixtureInterface $fixture, FlagParserInterface $parser, array $unparsedSpecs) : SpecificationBag { return SpecificationBagFactory::create(); }
public function testIfParsedValueIsArrayValueThenUniqueFlagAppliesToItsElementInstead() { $fixture = new SimpleFixture('dummy_id', 'Dummy', SpecificationBagFactory::create()); $value = 'string value'; $denormalizedValue = new ArrayValue(['foo', 'bar']); $flags = (new FlagBag(''))->withFlag(new UniqueFlag()); $decoratedDenormalizerProphecy = $this->prophesize(ValueDenormalizerInterface::class); $decoratedDenormalizerProphecy->denormalize($fixture, $flags, $value)->willReturn($denormalizedValue); /** @var ValueDenormalizerInterface $decoratedDenormalizer */ $decoratedDenormalizer = $decoratedDenormalizerProphecy->reveal(); $denormalizer = new UniqueValueDenormalizer($decoratedDenormalizer); $result = $denormalizer->denormalize($fixture, $flags, $value); $this->assertInstanceOf(ArrayValue::class, $result); /** @var ArrayValue $result */ $this->assertInstanceOf(UniqueValue::class, $result->getValue()[0]); $this->stringContains('dummy_id', $result->getValue()[0]->getId()); $this->assertEquals('foo', $result->getValue()[0]->getValue()); $this->assertInstanceOf(UniqueValue::class, $result->getValue()[1]); $this->stringContains('dummy_id', $result->getValue()[1]->getId()); $this->assertEquals('bar', $result->getValue()[1]->getValue()); }
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]; })()); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage Could not instantiate "dummy", the constructor of "Nelmio\Alice\Entity\Instantiator\DummyWithProtectedConstructor" is not public. */ public function testThrowsAnExceptionIfObjectConstructorIsProtected() { $fixture = new SimpleFixture('dummy', DummyWithProtectedConstructor::class, SpecificationBagFactory::create()); $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext()); }
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 static function createTemplating(string $id, $valueForCurrent) { return new TemplatingFixture(new SimpleFixtureWithFlags(new SimpleFixture($id, 'Dummy', SpecificationBagFactory::create(), $valueForCurrent), new FlagBag($id))); }
public function testVariablesInferenceWithCurrent() { $value = new EvaluatedValue('["foo" => $foo, "expression" => $_expression, "scope" => $_scope]'); $fixture = new SimpleFixture('dummy_1', 'Dummy', SpecificationBagFactory::create(), '1'); $set = ResolvedFixtureSetFactory::create(); $scope = ['foo' => 'bar']; $expected = new ResolvedValueWithFixtureSet(['foo' => 'bar', 'expression' => '["foo" => $foo, "expression" => $_expression, "scope" => $_scope]', 'scope' => ['foo' => 'bar', 'current' => '1']], $set); $resolver = new EvaluatedValueResolver(); $actual = $resolver->resolve($value, $fixture, $set, $scope, new GenerationContext()); $this->assertEquals($expected, $actual); $value = new EvaluatedValue('$scope'); try { $resolver->resolve($value, $fixture, $set, $scope, new GenerationContext()); $this->fail('Expected an exception to be thrown.'); } catch (UnresolvableValueException $exception) { $this->assertEquals('Could not evaluate the expression "$scope".', $exception->getMessage()); } }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationException * @expectedExceptionMessage Instantiated fixture was expected to be an instance of "Nelmio\Alice\Entity\Instantiator\DummyWithFakeNamedConstructor". Got "Nelmio\Alice\Throwable\Exception\Generator\Instantiator\InstantiationExceptionFactory" instead. */ public function testThrowsAnExceptionIfFactoryDoesNotReturnAnInstance() { $fixture = new SimpleFixture('dummy', DummyWithFakeNamedConstructor::class, SpecificationBagFactory::create(new MethodCallWithReference(new StaticReference(DummyWithFakeNamedConstructor::class), 'namedConstruct'))); $this->instantiator->instantiate($fixture, ResolvedFixtureSetFactory::create(), new GenerationContext()); }
/** * @expectedException \Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueException * @expectedExceptionMessage Expected fixture reference value "@bob" to be resolved into a string. Got "(integer) 200" instead. */ public function testThrowsAnExceptionIfResolvedIdIsInvalid() { $idValue = new DummyValue('bob'); $value = new FixtureReferenceValue($idValue); $dummyFixture = new SimpleFixture('dummy', 'Dummy', SpecificationBagFactory::create()); $set = ResolvedFixtureSetFactory::create(); $scope = []; $context = new GenerationContext(); $valueResolverProphecy = $this->prophesize(ValueResolverInterface::class); $valueResolverProphecy->resolve(Argument::cetera())->willReturn(new ResolvedValueWithFixtureSet(200, ResolvedFixtureSetFactory::create())); /** @var ValueResolverInterface $valueResolver */ $valueResolver = $valueResolverProphecy->reveal(); $resolver = new UnresolvedFixtureReferenceIdResolver(new FakeChainableValueResolver(), $valueResolver); $resolver->resolve($value, $dummyFixture, $set, $scope, $context); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Fixture "user0" extends "user_base" but "user_base" is not a template. */ public function testThrowsAnExceptionIfAFixtureExtendANonTemplateFixture() { $unresolvedFixtures = (new FixtureBag())->with(new TemplatingFixture(new SimpleFixtureWithFlags(new SimpleFixture('user0', 'Nelmio\\Alice\\Entity\\User', SpecificationBagFactory::create()), (new FlagBag('user0'))->withFlag(new ExtendFlag(new FixtureReference('user_base'))))))->with(new SimpleFixture('user_base', 'Nelmio\\Alice\\Entity\\User', SpecificationBagFactory::create())); $this->resolver->resolve($unresolvedFixtures); }
private function createFixtureWithFlags(FlagBag $flags) : SimpleFixtureWithFlags { return new SimpleFixtureWithFlags(new SimpleFixture($flags->getKey(), 'Dummy', SpecificationBagFactory::create()), $flags); }