Nelmio\Alice\Generator\Resolver\Value\Chainable\EvaluatedValueResolver::resolve PHP Method

resolve() public method

public resolve ( Nelmio\Alice\Definition\ValueInterface $value, Nelmio\Alice\FixtureInterface $fixture, ResolvedFixtureSet $fixtureSet, array $scope, GenerationContext $context ) : ResolvedValueWithFixtureSet
$value Nelmio\Alice\Definition\ValueInterface
$fixture Nelmio\Alice\FixtureInterface
$fixtureSet Nelmio\Alice\Generator\ResolvedFixtureSet
$scope array
$context Nelmio\Alice\Generator\GenerationContext
return Nelmio\Alice\Generator\ResolvedValueWithFixtureSet
    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);
    }

Usage Example

 public function testVariablesInferenceWithCurrent()
 {
     $value = new EvaluatedValue('["foo" => $foo, "expression" => $_expression, "scope" => $_scope]');
     $fixture = new SimpleFixture('dummy_1', 'Dummy', SpecificationBagFactory::create(), '1');
     $set = ResolvedFixtureSetFactory::create();
     $scope = ['foo' => 'bar'];
     $expected = new ResolvedValueWithFixtureSet(['foo' => 'bar', 'expression' => '["foo" => $foo, "expression" => $_expression, "scope" => $_scope]', 'scope' => ['foo' => 'bar', 'current' => '1']], $set);
     $resolver = new EvaluatedValueResolver();
     $actual = $resolver->resolve($value, $fixture, $set, $scope, new GenerationContext());
     $this->assertEquals($expected, $actual);
     $value = new EvaluatedValue('$scope');
     try {
         $resolver->resolve($value, $fixture, $set, $scope, new GenerationContext());
         $this->fail('Expected an exception to be thrown.');
     } catch (UnresolvableValueException $exception) {
         $this->assertEquals('Could not evaluate the expression "$scope".', $exception->getMessage());
     }
 }