Пример #1
0
 /**
  * @inheritdoc
  */
 public function hydrate(ObjectInterface $object, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : ResolvedFixtureSet
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $fixture = $fixtureSet->getFixtures()->get($object->getId());
     $properties = $fixture->getSpecs()->getProperties();
     $scope = ['_instances' => $fixtureSet->getObjects()->toArray()];
     foreach ($properties as $property) {
         /** @var Property $property */
         $propertyValue = $property->getValue();
         if ($propertyValue instanceof ValueInterface) {
             try {
                 $result = $this->resolver->resolve($propertyValue, $fixture, $fixtureSet, $scope, $context);
             } catch (ResolutionThrowable $throwable) {
                 throw UnresolvableValueDuringGenerationExceptionFactory::createFromResolutionThrowable($throwable);
             }
             list($propertyValue, $fixtureSet) = [$result->getValue(), $result->getSet()];
             $property = $property->withValue($propertyValue);
         }
         $scope[$property->getName()] = $propertyValue;
         $object = $this->hydrator->hydrate($object, $property, $context);
     }
     return $fixtureSet->withObjects($fixtureSet->getObjects()->with($object));
 }
 public function testTestCreateNewExceptionWithFactoryForUnexpectedCall()
 {
     $exception = ResolverNotFoundExceptionFactory::createUnexpectedCall('fake');
     $this->assertEquals('Expected method "fake" to be called only if it has a resolver.', $exception->getMessage());
     $this->assertEquals(0, $exception->getCode());
     $this->assertNull($exception->getPrevious());
 }
 /**
  * {@inheritdoc}
  *
  * @param FixtureReferenceValue $value
  *
  * @throws UnresolvableValueException
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     list($referredFixtureId, $fixtureSet) = $this->getReferredFixtureId($this->resolver, $value, $fixture, $fixtureSet, $scope, $context);
     return $this->decoratedResolver->resolve(new FixtureReferenceValue($referredFixtureId), $fixture, $fixtureSet, $scope, $context);
 }
Пример #4
0
 /**
  * {@inheritdoc}
  *
  * @throws ResolverNotFoundException
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     foreach ($this->resolvers as $resolver) {
         if ($resolver->canResolve($value)) {
             return $resolver->resolve($value, $fixture, $fixtureSet, $scope, $context);
         }
     }
     throw ResolverNotFoundExceptionFactory::createForValue($value);
 }
Пример #5
0
 /**
  * @inheritdoc
  */
 public function resolve(Parameter $parameter, ParameterBag $unresolvedParameters, ParameterBag $injectedParameters, ResolvingContext $context = null) : ParameterBag
 {
     foreach ($this->resolvers as $resolver) {
         if ($resolver->canResolve($parameter)) {
             return $resolver->resolve($parameter, $unresolvedParameters, $injectedParameters, $context);
         }
     }
     throw ResolverNotFoundExceptionFactory::createForParameter($parameter->getKey());
 }
 /**
  * {@inheritdoc}
  *
  * @param FixtureMatchReferenceValue $value
  *
  * @throws UnresolvableValueException
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $possibleIds = $this->getSuitableIds($value, $fixtureSet);
     $id = Base::randomElement($possibleIds);
     if (null === $id) {
         throw UnresolvableValueExceptionFactory::createForNoFixtureOrObjectMatchingThePattern($value);
     }
     return $this->resolver->resolve(new FixtureReferenceValue($id), $fixture, $fixtureSet, $scope, $context);
 }
Пример #7
0
 /**
  * @param FixtureInterface   $fixture
  * @param ResolvedFixtureSet $set
  * @param GenerationContext  $context
  *
  * @throws UnresolvableValueDuringGenerationException
  *
  * @return array
  */
 private function resolveFixtureConstructor(FixtureInterface $fixture, ResolvedFixtureSet $set, GenerationContext $context) : array
 {
     $specs = $fixture->getSpecs();
     $constructor = $specs->getConstructor();
     if (null === $constructor || $constructor instanceof NoMethodCall) {
         return [$fixture, $set];
     }
     if (null === $this->valueResolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     list($resolvedArguments, $set) = $this->resolveArguments($constructor->getArguments(), $this->valueResolver, $fixture, $set, $context);
     return [$fixture->withSpecs($specs->withConstructor($constructor->withArguments($resolvedArguments))), $set];
 }
Пример #8
0
 /**
  * {@inheritdoc}
  *
  * @param ListValue $list
  */
 public function resolve(ValueInterface $list, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $values = $list->getValue();
     foreach ($values as $index => $value) {
         if ($value instanceof ValueInterface) {
             $resolvedSet = $this->resolver->resolve($value, $fixture, $fixtureSet, $scope, $context);
             $values[$index] = $resolvedSet->getValue();
             $fixtureSet = $resolvedSet->getSet();
         }
     }
     return new ResolvedValueWithFixtureSet(implode('', $values), $fixtureSet);
 }
Пример #9
0
 /**
  * {@inheritdoc}
  *
  * @param FunctionCallValue $value
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if (null === $this->argumentResolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $arguments = $value->getArguments();
     foreach ($arguments as $index => $argument) {
         if ($argument instanceof ValueInterface) {
             $resolvedSet = $this->argumentResolver->resolve($argument, $fixture, $fixtureSet, $scope, $context);
             $arguments[$index] = $resolvedSet->getValue();
             $fixtureSet = $resolvedSet->getSet();
         }
     }
     return $this->resolver->resolve(new FunctionCallValue($value->getName(), $arguments), $fixture, $fixtureSet, $scope, $context);
 }
Пример #10
0
 /**
  * @inheritdoc
  */
 public function resolve(Parameter $unresolvedArrayParameter, ParameterBag $unresolvedParameters, ParameterBag $resolvedParameters, ResolvingContext $context = null) : ParameterBag
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $context = ResolvingContext::createFrom($context, $unresolvedArrayParameter->getKey());
     $resolvedArray = [];
     /* @var array $unresolvedArray */
     $unresolvedArray = $unresolvedArrayParameter->getValue();
     foreach ($unresolvedArray as $index => $unresolvedValue) {
         // Iterate over all the values of the array to resolve each of them
         $resolvedParameters = $this->resolver->resolve(new Parameter((string) $index, $unresolvedValue), $unresolvedParameters, $resolvedParameters, $context);
         $resolvedArray[$index] = $resolvedParameters->get((string) $index);
         $resolvedParameters = $resolvedParameters->without((string) $index);
     }
     $resolvedParameters = $resolvedParameters->with($unresolvedArrayParameter->withValue($resolvedArray));
     return $resolvedParameters;
 }
 /**
  * {@inheritdoc}
  *
  * @param FixturePropertyValue $value
  *
  * @throws NoSuchPropertyException
  * @throws UnresolvableValueException
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $context->markAsNeedsCompleteGeneration();
     $fixtureReferenceResult = $this->resolver->resolve($value->getReference(), $fixture, $fixtureSet, $scope, $context);
     $context->unmarkAsNeedsCompleteGeneration();
     /** @var ResolvedFixtureSet $fixtureSet */
     list($instance, $fixtureSet) = [$fixtureReferenceResult->getValue(), $fixtureReferenceResult->getSet()];
     try {
         $propertyValue = $this->propertyAccessor->getValue($instance, $value->getProperty());
     } catch (SymfonyNoSuchPropertyException $exception) {
         throw NoSuchPropertyExceptionFactory::createForFixture($fixture, $value, 0, $exception);
     } catch (\Exception $exception) {
         throw UnresolvableValueExceptionFactory::create($value, 0, $exception);
     }
     return new ResolvedValueWithFixtureSet($propertyValue, $fixtureSet);
 }
Пример #12
0
 /**
  * {@inheritdoc}
  *
  * @param OptionalValue $value
  *
  * @throws UnresolvableValueException
  */
 public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall(__METHOD__);
     }
     $quantifier = $value->getQuantifier();
     if ($quantifier instanceof ValueInterface) {
         $resolvedSet = $this->resolver->resolve($quantifier, $fixture, $fixtureSet, $scope, $context);
         list($quantifier, $fixtureSet) = [$resolvedSet->getValue(), $resolvedSet->getSet()];
         if (false === is_int($quantifier) && false === is_string($quantifier)) {
             throw UnresolvableValueExceptionFactory::createForInvalidResolvedQuantifierTypeForOptionalValue($value, $quantifier);
         }
     }
     $realValue = mt_rand(0, 100) <= $quantifier ? $value->getFirstMember() : $value->getSecondMember();
     if ($realValue instanceof ValueInterface) {
         return $this->resolver->resolve($realValue, $fixture, $fixtureSet, $scope, $context);
     }
     return new ResolvedValueWithFixtureSet($realValue, $fixtureSet);
 }
Пример #13
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);
 }
Пример #14
0
 private function checkResolver(string $checkedMethod)
 {
     if (null === $this->resolver) {
         throw ResolverNotFoundExceptionFactory::createUnexpectedCall($checkedMethod);
     }
 }