Файл: symfony-2.7/src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Строк: 213
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyComponentExpressionLanguageTests;
use SymfonyComponentExpressionLanguageParser;
use SymfonyComponentExpressionLanguageLexer;
use SymfonyComponentExpressionLanguageNode;
class ParserTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException SymfonyComponentExpressionLanguageSyntaxError
* @expectedExceptionMessage Variable "foo" is not valid around position 1.
*/
public function testParseWithInvalidName()
{
$lexer = new Lexer();
$parser = new Parser(array());
$parser->parse($lexer->tokenize('foo'));
}
/**
* @expectedException SymfonyComponentExpressionLanguageSyntaxError
* @expectedExceptionMessage Variable "foo" is not valid around position 1.
*/
public function testParseWithZeroInNames()
{
$lexer = new Lexer();
$parser = new Parser(array());
$parser->parse($lexer->tokenize('foo'), array(0));
}
/**
* @dataProvider getParseData
*/
public function testParse($node, $expression, $names = array())
{
$lexer = new Lexer();
$parser = new Parser(array());
$this->assertEquals($node, $parser->parse($lexer->tokenize($expression), $names));
}
public function getParseData()
{
$arguments = new NodeArgumentsNode();
$arguments->addElement(new NodeConstantNode('arg1'));
$arguments->addElement(new NodeConstantNode(2));
$arguments->addElement(new NodeConstantNode(true));
return array(
array(
new NodeNameNode('a'),
'a',
array('a'),
),
array(
new NodeConstantNode('a'),
'"a"',
),
array(
new NodeConstantNode(3),
'3',
),
array(
new NodeConstantNode(false),
'false',
),
array(
new NodeConstantNode(true),
'true',
),
array(
new NodeConstantNode(null),
'null',
),
array(
new NodeUnaryNode('-', new NodeConstantNode(3)),
'-3',
),
array(
new NodeBinaryNode('-', new NodeConstantNode(3), new NodeConstantNode(3)),
'3 - 3',
),
array(
new NodeBinaryNode('*',
new NodeBinaryNode('-', new NodeConstantNode(3), new NodeConstantNode(3)),
new NodeConstantNode(2)
),
'(3 - 3) * 2',
),
array(
new NodeGetAttrNode(new NodeNameNode('foo'), new NodeConstantNode('bar'), new NodeArgumentsNode(), NodeGetAttrNode::PROPERTY_CALL),
'foo.bar',
array('foo'),
),
array(
new NodeGetAttrNode(new NodeNameNode('foo'), new NodeConstantNode('bar'), new NodeArgumentsNode(), NodeGetAttrNode::METHOD_CALL),
'foo.bar()',
array('foo'),
),
array(
new NodeGetAttrNode(new NodeNameNode('foo'), new NodeConstantNode('not'), new NodeArgumentsNode(), NodeGetAttrNode::METHOD_CALL),
'foo.not()',
array('foo'),
),
array(
new NodeGetAttrNode(
new NodeNameNode('foo'),
new NodeConstantNode('bar'),
$arguments,
NodeGetAttrNode::METHOD_CALL
),
'foo.bar("arg1", 2, true)',
array('foo'),
),
array(
new NodeGetAttrNode(new NodeNameNode('foo'), new NodeConstantNode(3), new NodeArgumentsNode(), NodeGetAttrNode::ARRAY_CALL),
'foo[3]',
array('foo'),
),
array(
new NodeConditionalNode(new NodeConstantNode(true), new NodeConstantNode(true), new NodeConstantNode(false)),
'true ? true : false',
),
array(
new NodeBinaryNode('matches', new NodeConstantNode('foo'), new NodeConstantNode('/foo/')),
'"foo" matches "/foo/"',
),
// chained calls
array(
$this->createGetAttrNode(
$this->createGetAttrNode(
$this->createGetAttrNode(
$this->createGetAttrNode(new NodeNameNode('foo'), 'bar', NodeGetAttrNode::METHOD_CALL),
'foo', NodeGetAttrNode::METHOD_CALL),
'baz', NodeGetAttrNode::PROPERTY_CALL),
'3', NodeGetAttrNode::ARRAY_CALL),
'foo.bar().foo().baz[3]',
array('foo'),
),
array(
new NodeNameNode('foo'),
'bar',
array('foo' => 'bar'),
),
);
}
private function createGetAttrNode($node, $item, $type)
{
return new NodeGetAttrNode($node, new NodeConstantNode($item), new NodeArgumentsNode(), $type);
}
}