/** * {@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 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 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 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); }
/** * @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]; }