Resolves a value, i.e. determine the final value. Once resolved, the value will be ready to be passed to the
object.
public resolve ( Nelmio\Alice\Definition\ValueInterface $value, Nelmio\Alice\FixtureInterface $fixture, |
||
$value | Nelmio\Alice\Definition\ValueInterface | |
$fixture | Nelmio\Alice\FixtureInterface | Fixture to which belongs the arguments. |
$fixtureSet | ||
$scope | array | List of variables accessible while resolving the arguments. |
$context | ||
Результат |
/** * @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)); }
/** * {@inheritdoc} * * @param FunctionCallValue $value */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { $functionName = $value->getName(); if (false === function_exists($functionName) || array_key_exists($functionName, $this->functionBlacklist)) { return $this->resolver->resolve($value, $fixture, $fixtureSet, $scope, $context); } $arguments = $value->getArguments(); return new ResolvedValueWithFixtureSet($functionName(...$arguments), $fixtureSet); }
/** * {@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); }
/** * {@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); }
/** * {@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); }
private function generateValue(UniqueValue $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : array { $realValue = $value->getValue(); if ($realValue instanceof ValueInterface) { $result = $this->resolver->resolve($value->getValue(), $fixture, $fixtureSet, $scope, $context); return [$value->withValue($result->getValue()), $result->getSet()]; } return [$value, $fixtureSet]; }
/** * {@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); }
/** * {@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); }
/** * {@inheritdoc} * * @param DynamicArrayValue $value */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { $this->checkResolver(__METHOD__); $quantifier = $value->getQuantifier(); if ($quantifier instanceof ValueInterface) { $result = $this->resolver->resolve($quantifier, $fixture, $fixtureSet, $scope, $context); list($quantifier, $fixtureSet) = [$result->getValue(), $result->getSet()]; } if ($quantifier < 0) { throw InvalidArgumentExceptionFactory::createForInvalidDynamicArrayQuantifier($fixture, $quantifier); } $element = $value->getElement(); if (false === $element instanceof ValueInterface) { $array = array_fill(0, $quantifier, $element); return new ResolvedValueWithFixtureSet($array, $fixtureSet); } $array = []; for ($i = 0; $i < $quantifier; $i++) { $result = $this->resolver->resolve($element, $fixture, $fixtureSet, $scope, $context); $array[] = $result->getValue(); $fixtureSet = $result->getSet(); } return new ResolvedValueWithFixtureSet($array, $fixtureSet); }
/** * @param array $arguments * @param ValueResolverInterface $resolver * @param FixtureInterface $fixture * @param ResolvedFixtureSet $fixtureSet * @param GenerationContext $context * * @throws UnresolvableValueDuringGenerationException * * @return array The first element is an array ($arguments) which is the resolved arguments and the second the new * ResolvedFixtureSet which may contains new fixtures (from the arguments resolution) */ private function resolveArguments(array $arguments, ValueResolverInterface $resolver, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, GenerationContext $context) : array { foreach ($arguments as $index => $argument) { if ($argument instanceof ValueInterface) { try { $result = $resolver->resolve($argument, $fixture, $fixtureSet, [], $context); } catch (ResolutionThrowable $throwable) { throw UnresolvableValueDuringGenerationExceptionFactory::createFromResolutionThrowable($throwable); } $fixtureSet = $result->getSet(); $arguments[$index] = $result->getValue(); } } return [$arguments, $fixtureSet]; }
/** * @throws UnresolvableValueException */ private function getReferredFixtureId(ValueResolverInterface $resolver, ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $set, array $scope, GenerationContext $context) : array { $referredFixtureId = $value->getValue(); if ($referredFixtureId instanceof ValueInterface) { $resolvedSet = $resolver->resolve($referredFixtureId, $fixture, $set, $scope, $context); list($referredFixtureId, $set) = [$resolvedSet->getValue(), $resolvedSet->getSet()]; if (false === is_string($referredFixtureId)) { throw UnresolvableValueExceptionFactory::createForInvalidReferenceId($value, $referredFixtureId); } } return [$referredFixtureId, $set]; }