Ejemplo n.º 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);
 }
 /**
  * @inheritdoc
  */
 public function resolve(ParameterBag $unresolvedParameters, ParameterBag $injectedParameters = null) : ParameterBag
 {
     if (null === $injectedParameters) {
         $injectedParameters = new ParameterBag();
     }
     foreach ($injectedParameters as $injectedParameterKey => $injectedParameterValue) {
         if ($unresolvedParameters->has($injectedParameterKey)) {
             $injectedParameters = $injectedParameters->without($injectedParameterKey);
         }
     }
     return $this->resolver->resolve($unresolvedParameters, $injectedParameters);
 }
Ejemplo n.º 3
0
 /**
  * @param ParameterResolverInterface $resolver
  * @param Parameter                  $parameter Parameter being resolved
  * @param string                     $key       Key of the parameter that need to be resolved to resolve $parameter
  * @param ParameterBag               $unresolvedParameters
  * @param ParameterBag               $resolvedParameters
  * @param ResolvingContext           $context
  *
  * @return ParameterBag
  */
 private function resolveStringKey(ParameterResolverInterface $resolver = null, Parameter $parameter, string $key, ParameterBag $unresolvedParameters, ParameterBag $resolvedParameters, ResolvingContext $context) : ParameterBag
 {
     if ($resolvedParameters->has($key)) {
         return $resolvedParameters;
     }
     if (false === $unresolvedParameters->has($key)) {
         throw ParameterNotFoundExceptionFactory::createForWhenResolvingParameter($key, $parameter);
     }
     $context->checkForCircularReference($key);
     $context->add($key);
     if (null === $resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     return $resolver->resolve(new Parameter($key, $unresolvedParameters->get($key)), $unresolvedParameters, $resolvedParameters, $context);
 }
Ejemplo n.º 4
0
 /**
  * Resolves a parameter two times and if a different result is obtained will resolve the parameter again until two
  * successive resolution give the same result.
  *
  * {@inheritdoc}
  *
  * @param Parameter $parameter
  *
  * @throws RecursionLimitReachedException
  */
 public function resolve(Parameter $parameter, ParameterBag $unresolvedParameters, ParameterBag $resolvedParameters, ResolvingContext $context = null, ParameterBag $previousResult = null, int $counter = 1) : ParameterBag
 {
     if (null === $previousResult) {
         $result = $this->resolver->resolve($parameter, $unresolvedParameters, $resolvedParameters, $context);
         return $this->resolve($parameter, $unresolvedParameters, $resolvedParameters, $context, $result);
     }
     $parameterKey = $parameter->getKey();
     $previousParameterValue = $previousResult->get($parameterKey);
     $counter = $this->incrementCounter($counter, $this->limit, $parameterKey);
     $newResult = $this->resolver->resolve($parameter->withValue($previousParameterValue), $unresolvedParameters, $resolvedParameters, $context);
     $newParameterValue = $newResult->get($parameterKey);
     $result = $this->mergeResults($previousResult, $newResult);
     if ($previousParameterValue === $newParameterValue) {
         return $result;
     }
     return $this->resolve($parameter, $unresolvedParameters, $resolvedParameters, $context, $result, $counter);
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  *
  * @param bool|int|float $parameter
  */
 public function resolve(Parameter $parameter, ParameterBag $unresolvedParameters, ParameterBag $resolvedParameters) : ParameterBag
 {
     return $resolvedParameters->with($parameter);
 }