/** * {@inheritdoc} * * @param FixtureReferenceValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { if ('self' === $value->getValue()) { return new ResolvedValueWithFixtureSet($fixtureSet->getObjects()->get($fixture)->getInstance(), $fixtureSet); } return $this->decoratedResolver->resolve($value, $fixture, $fixtureSet, $scope, $context); }
/** * {@inheritdoc} * * @param EvaluatedValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { // Scope exclusive to the evaluated expression // We make use of the underscore prefix (`_`) here to limit the possible conflicts with the variables injected // in the scope. $_scope = $scope; try { $_scope['current'] = $fixture->getValueForCurrent(); } catch (NoValueForCurrentException $exception) { // Continue } $expression = $this->replacePlaceholders($value->getValue()); // Closure in which the expression is evaluated; This is done in a closure to avoid the expression to have // access to this method variables (which should remain unknown) and we injected only the variables of the // closure. $evaluateExpression = function ($_expression) use($_scope) { foreach ($_scope as $_scopeVariableName => $_scopeVariableValue) { ${$_scopeVariableName} = $_scopeVariableValue; } return eval("return {$_expression};"); }; try { $evaluatedExpression = $evaluateExpression($expression); } catch (\Throwable $throwable) { throw UnresolvableValueExceptionFactory::createForCouldNotEvaluateExpression($value, 0, $throwable); } return new ResolvedValueWithFixtureSet($evaluatedExpression, $fixtureSet); }
/** * {@inheritdoc} * * @param VariableValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { $variableName = $value->getValue(); if (array_key_exists($variableName, $scope)) { return new ResolvedValueWithFixtureSet($scope[$variableName], $fixtureSet); } throw UnresolvableValueExceptionFactory::createForCouldNotFindVariable($value); }
/** * {@inheritdoc} * * @param ParameterValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { $parameterKey = $value->getValue(); $parameters = $fixtureSet->getParameters(); if (false === $parameters->has($parameterKey)) { throw UnresolvableValueExceptionFactory::createForCouldNotFindParameter($parameterKey); } return new ResolvedValueWithFixtureSet($parameters->get($parameterKey), $fixtureSet); }
/** * {@inheritdoc} * * @param FunctionCallValue $value */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { /** * @var FakerGenerator $generator * @var string $formatter */ list($generator, $formatter) = $this->getGenerator($this->generatorFactory, $value->getName()); return new ResolvedValueWithFixtureSet($generator->format($formatter, $value->getArguments()), $fixtureSet); }
/** * {@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 FixtureReferenceValue $value * * @throws UnresolvableValueException */ public function resolve(ValueInterface $value, FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context) : ResolvedValueWithFixtureSet { if (null === $this->generator) { throw ObjectGeneratorNotFoundExceptionFactory::createUnexpectedCall(__METHOD__); } $referredFixtureId = $value->getValue(); if ($referredFixtureId instanceof ValueInterface) { throw UnresolvableValueExceptionFactory::create($value); } $referredFixture = $this->getReferredFixture($referredFixtureId, $fixtureSet); return $this->resolveReferredFixture($referredFixture, $referredFixtureId, $fixtureSet, $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); }
/** * {@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); }
/** * @param FixtureInterface $scope * @param FlagBag $flags * @param mixed|ValueInterface $value * * @throws InvalidScopeException * * @return ValueInterface */ private function generateValue(FixtureInterface $scope, FlagBag $flags, $value) : ValueInterface { $uniqueId = sprintf('%s#%s', $scope->getClassName(), $flags->getKey()); if ('temporary_id' === substr($scope->getId(), 0, 12)) { throw DenormalizerExceptionFactory::createForInvalidScopeForUniqueValue(); } if ($value instanceof DynamicArrayValue) { $uniqueId = uniqid($uniqueId . '::', true); return new DynamicArrayValue($value->getQuantifier(), new UniqueValue($uniqueId, $value->getElement())); } if ($value instanceof ArrayValue) { $uniqueId = uniqid($uniqueId . '::', true); $elements = $value->getValue(); foreach ($elements as $key => $element) { // The key must be the same for each argument: the unique ID is bound to the array, not the argument // number. $elements[$key] = new UniqueValue($uniqueId, $element); } return new ArrayValue($elements); } return new UniqueValue($uniqueId, $value); }
/** * {@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); }
public static function createForNoFixtureOrObjectMatchingThePattern(ValueInterface $value) : UnresolvableValueException { return new UnresolvableValueException(sprintf('Could not find a fixture or object ID matching the pattern "%s".', $value->getValue())); }
/** * @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]; }