public function testGenerateObjects() { $loadedParameters = new ParameterBag(['loaded' => true]); $injectedParameters = new ParameterBag(['injected' => true]); $fixture = new DummyFixture('dummy'); $fixtures = (new FixtureBag())->with($fixture); $objects = new ObjectBag(['std' => new \stdClass()]); $set = new FixtureSet($loadedParameters, $injectedParameters, $fixtures, $objects); $resolvedParameters = $injectedParameters->with(new Parameter('loaded', true)); $resolvedSet = new ResolvedFixtureSet($resolvedParameters, $fixtures, $objects); $resolverProphecy = $this->prophesize(FixtureSetResolverInterface::class); $resolverProphecy->resolve($set)->willReturn($resolvedSet); /** @var FixtureSetResolverInterface $resolver */ $resolver = $resolverProphecy->reveal(); $context = new GenerationContext(); $objectGeneratorProphecy = $this->prophesize(ObjectGeneratorInterface::class); $objectGeneratorProphecy->generate($fixture, $resolvedSet, $context)->willReturn($objectsAfterFirstPass = $objects->with(new SimpleObject('foo', StdClassFactory::create(['pass' => 'first'])))); $contextAfterFirstPass = clone $context; $contextAfterFirstPass->setToSecondPass(); $objectGeneratorProphecy->generate($fixture, new ResolvedFixtureSet($resolvedSet->getParameters(), $resolvedSet->getFixtures(), $objectsAfterFirstPass), $contextAfterFirstPass)->willReturn($objectsAfterFirstPass = $objects->with(new SimpleObject('foo', StdClassFactory::create(['pass' => 'second'])))); /** @var ObjectGeneratorInterface $objectGenerator */ $objectGenerator = $objectGeneratorProphecy->reveal(); $expected = new ObjectSet($resolvedParameters, $objects->with(new SimpleObject('foo', StdClassFactory::create(['pass' => 'second'])))); $generator = new DoublePassGenerator($resolver, $objectGenerator); $actual = $generator->generate($set); $this->assertEquals($expected, $actual); $resolverProphecy->resolve(Argument::any())->shouldHaveBeenCalledTimes(1); $objectGeneratorProphecy->generate(Argument::cetera())->shouldHaveBeenCalledTimes(2); }
public function testReturnsNewSetWithInstantiatedObject() { $fixture = new DummyFixture('dummy'); $set = new ResolvedFixtureSet($parameters = new ParameterBag(['foo' => 'bar']), $fixtures = (new FixtureBag())->with(new DummyFixture('another_dummy')), $objects = new ObjectBag(['ping' => new Dummy()])); $instantiatedObject = new \stdClass(); $instantiatedObject->instantiated = true; $decoratedInstantiatorProphecy = $this->prophesize(AbstractChainableInstantiator::class); $decoratedInstantiatorProphecy->createInstance($fixture)->willReturn($instantiatedObject); /** @var AbstractChainableInstantiator $decoratedInstantiator */ $decoratedInstantiator = $decoratedInstantiatorProphecy->reveal(); $expected = new ResolvedFixtureSet($parameters, $fixtures, $objects->with(new SimpleObject('dummy', $instantiatedObject))); $instantiator = new ProphecyChainableInstantiator($decoratedInstantiator); $actual = $instantiator->instantiate($fixture, $set, new GenerationContext()); $this->assertEquals($expected, $actual); $decoratedInstantiatorProphecy->createInstance(Argument::any())->shouldHaveBeenCalledTimes(1); }
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); }
public function testCountable() { $this->assertTrue(is_a(ObjectBag::class, \Countable::class, true)); $bag = new ObjectBag(); $this->assertEquals(0, $bag->count()); $bag = new ObjectBag(['foo' => new \stdClass(), 'bar' => new \stdClass()]); $this->assertEquals(2, $bag->count()); $object1 = new CompleteObject(new SimpleObject('foo', new \stdClass())); $object2 = new CompleteObject(new SimpleObject('bar', new \stdClass())); $bag = (new ObjectBag())->with($object1)->with($object2); $this->assertEquals(2, $bag->count()); $object3 = new CompleteObject(new SimpleObject('foz', new \stdClass())); $object4 = new CompleteObject(new SimpleObject('baz', new \stdClass())); $anotherBag = (new ObjectBag())->with($object3)->with($object4); $bag = $bag->mergeWith($anotherBag); $this->assertEquals(4, $bag->count()); }