Creates a new instance which will contain the given object. If an object with the same reference already exists,
it will be overridden by the new object.
public with ( Nelmio\Alice\ObjectInterface $object ) : self | ||
$object | Nelmio\Alice\ObjectInterface | |
return | self |
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 testWithersReturnNewModifiedInstance() { $bag = new ObjectBag(['foo' => new \stdClass()]); $std = new \stdClass(); $std->ping = 'pong'; $newBag = $bag->with(new CompleteObject(new SimpleObject('bar', $std))); $this->assertEquals(new ObjectBag(['foo' => new \stdClass()]), $bag); $this->assertEquals(new ObjectBag(['foo' => new \stdClass(), 'bar' => new CompleteObject(new SimpleObject('bar', $std))]), $newBag); }
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 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); }