Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Parser\TokenParser\Chainable\MethodReferenceTokenParser::parse PHP Method

parse() public method

public parse ( Token $token ) : FixtureMethodCallValue
$token Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token
return Nelmio\Alice\Definition\Value\FixtureMethodCallValue
    public function parse(Token $token) : FixtureMethodCallValue
    {
        parent::parse($token);
        $explodedValue = explode('->', $token->getValue());
        if (count($explodedValue) !== 2) {
            throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
        }
        $reference = $this->parser->parse($explodedValue[0]);
        $method = $this->parser->parse(sprintf('<%s>', $explodedValue[1]));
        if ($reference instanceof FixtureReferenceValue && $method instanceof FunctionCallValue) {
            return new FixtureMethodCallValue($reference, $method);
        }
        throw ExpressionLanguageExceptionFactory::createForUnparsableToken($token);
    }

Usage Example

 public function testReturnsAFixtureMethodCallValueIfCanParseToken()
 {
     $token = new Token('@user->getUserName(arg1, arg2)', new TokenType(TokenType::METHOD_REFERENCE_TYPE));
     $decoratedParserProphecy = $this->prophesize(ParserInterface::class);
     $decoratedParserProphecy->parse('@user')->willReturn($reference = new FixtureReferenceValue('user'));
     $decoratedParserProphecy->parse('<getUserName(arg1, arg2)>')->willReturn($call = new FunctionCallValue('getUserName', ['parsed_arg1', 'parsed_arg2']));
     /** @var ParserInterface $decoratedParser */
     $decoratedParser = $decoratedParserProphecy->reveal();
     $expected = new FixtureMethodCallValue($reference, $call);
     $parser = new MethodReferenceTokenParser($decoratedParser);
     $actual = $parser->parse($token);
     $this->assertEquals($expected, $actual);
     $decoratedParserProphecy->parse(Argument::any())->shouldHaveBeenCalledTimes(2);
 }
MethodReferenceTokenParser