Nelmio\Alice\ObjectBag::with PHP Метод

with() публичный Метод

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
Результат self
    public function with(ObjectInterface $object) : self
    {
        $clone = clone $this;
        $clone->objects[$object->getId()] = $object;
        $clone->array[$object->getId()] = $object->getInstance();
        return $clone;
    }

Usage Example

Пример #1
0
 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);
 }
All Usage Examples Of Nelmio\Alice\ObjectBag::with