/** * @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 testResolvesAllArguments() { $specs = SpecificationBagFactory::create(new SimpleMethodCall('__construct', [$firstArg = new VariableValue('firstArg'), $secondArg = new VariableValue('secondArg')])); $resolvedSpecs = $specs->withConstructor(new SimpleMethodCall('__construct', ['resolvedFirstArg', 'resolvedSecondArg'])); $fixture = new SimpleFixture('dummy', 'stdClass', $specs); $set = ResolvedFixtureSetFactory::create(); $context = new GenerationContext(); $context->markIsResolvingFixture('foo'); $expected = ResolvedFixtureSetFactory::create(null, (new FixtureBag())->with($fixture->withSpecs($resolvedSpecs)), new ObjectBag(['dummy' => new \stdClass()])); $resolverProphecy = $this->prophesize(ValueResolverInterface::class); $setAfterFirstArgResolution = new ResolvedFixtureSet($set->getParameters(), (new FixtureBag())->with(new DummyFixture('dummy')), $set->getObjects()); $resolverProphecy->resolve($firstArg, $fixture, $set, [], $context)->willReturn(new ResolvedValueWithFixtureSet('resolvedFirstArg', $setAfterFirstArgResolution)); $setAfterSecondArgResolution = new ResolvedFixtureSet($setAfterFirstArgResolution->getParameters(), (new FixtureBag())->with(new DummyFixture('another_dummy')), $setAfterFirstArgResolution->getObjects()); $resolverProphecy->resolve($secondArg, $fixture, $setAfterFirstArgResolution, [], $context)->willReturn(new ResolvedValueWithFixtureSet('resolvedSecondArg', $setAfterSecondArgResolution)); /** @var ValueResolverInterface $resolver */ $resolver = $resolverProphecy->reveal(); $fixtureAfterResolution = $fixture->withSpecs($resolvedSpecs); $decoratedInstantiatorProphecy = $this->prophesize(InstantiatorInterface::class); $decoratedInstantiatorProphecy->instantiate($fixtureAfterResolution, $setAfterSecondArgResolution, $context)->willReturn($expected); /** @var InstantiatorInterface $decoratedInstantiator */ $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal(); $instantiator = new InstantiatorResolver($decoratedInstantiator, $resolver); $actual = $instantiator->instantiate($fixture, $set, $context); $this->assertSame($expected, $actual); $resolverProphecy->resolve(Argument::cetera())->shouldHaveBeenCalledTimes(2); $decoratedInstantiatorProphecy->instantiate(Argument::cetera())->shouldHaveBeenCalledTimes(1); }
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()); }