1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12use Twig\Environment; 13use Twig\Node\Expression\ConstantExpression; 14use Twig\Node\Expression\Test\NullTest; 15use Twig\Node\Expression\TestExpression; 16use Twig\Node\Node; 17use Twig\Test\NodeTestCase; 18use Twig\TwigTest; 19 20class Twig_Tests_Node_Expression_TestTest extends NodeTestCase 21{ 22 public function testConstructor() 23 { 24 $expr = new ConstantExpression('foo', 1); 25 $name = new ConstantExpression('null', 1); 26 $args = new Node(); 27 $node = new TestExpression($expr, $name, $args, 1); 28 29 $this->assertEquals($expr, $node->getNode('node')); 30 $this->assertEquals($args, $node->getNode('arguments')); 31 $this->assertEquals($name, $node->getAttribute('name')); 32 } 33 34 public function getTests() 35 { 36 $environment = new Environment($this->getMockBuilder('\Twig\Loader\LoaderInterface')->getMock()); 37 $environment->addTest(new TwigTest('barbar', 'twig_tests_test_barbar', ['is_variadic' => true, 'need_context' => true])); 38 39 $tests = []; 40 41 $expr = new ConstantExpression('foo', 1); 42 $node = new NullTest($expr, 'null', new Node([]), 1); 43 $tests[] = [$node, '(null === "foo")']; 44 45 // test as an anonymous function 46 if (PHP_VERSION_ID >= 50300) { 47 $node = $this->createTest(new ConstantExpression('foo', 1), 'anonymous', [new ConstantExpression('foo', 1)]); 48 $tests[] = [$node, 'call_user_func_array($this->env->getTest(\'anonymous\')->getCallable(), ["foo", "foo"])']; 49 } 50 51 // arbitrary named arguments 52 $string = new ConstantExpression('abc', 1); 53 $node = $this->createTest($string, 'barbar'); 54 $tests[] = [$node, 'twig_tests_test_barbar("abc")', $environment]; 55 56 $node = $this->createTest($string, 'barbar', ['foo' => new ConstantExpression('bar', 1)]); 57 $tests[] = [$node, 'twig_tests_test_barbar("abc", null, null, ["foo" => "bar"])', $environment]; 58 59 $node = $this->createTest($string, 'barbar', ['arg2' => new ConstantExpression('bar', 1)]); 60 $tests[] = [$node, 'twig_tests_test_barbar("abc", null, "bar")', $environment]; 61 62 $node = $this->createTest($string, 'barbar', [ 63 new ConstantExpression('1', 1), 64 new ConstantExpression('2', 1), 65 new ConstantExpression('3', 1), 66 'foo' => new ConstantExpression('bar', 1), 67 ]); 68 $tests[] = [$node, 'twig_tests_test_barbar("abc", "1", "2", [0 => "3", "foo" => "bar"])', $environment]; 69 70 return $tests; 71 } 72 73 protected function createTest($node, $name, array $arguments = []) 74 { 75 return new TestExpression($node, $name, new Node($arguments), 1); 76 } 77 78 protected function getEnvironment() 79 { 80 if (PHP_VERSION_ID >= 50300) { 81 return include 'PHP53/TestInclude.php'; 82 } 83 84 return parent::getEnvironment(); 85 } 86} 87 88function twig_tests_test_barbar($string, $arg1 = null, $arg2 = null, array $args = []) 89{ 90} 91