PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/twig/twig/test/Twig/Tests/Node/Expression/FilterTest.php

https://gitlab.com/pr0055/symfonypizza
PHP | 154 lines | 105 code | 27 blank | 22 comment | 4 complexity | 3f919fee965c2ac7445c82004f17f4b1 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase
  11. {
  12. public function testConstructor()
  13. {
  14. $expr = new Twig_Node_Expression_Constant('foo', 1);
  15. $name = new Twig_Node_Expression_Constant('upper', 1);
  16. $args = new Twig_Node();
  17. $node = new Twig_Node_Expression_Filter($expr, $name, $args, 1);
  18. $this->assertEquals($expr, $node->getNode('node'));
  19. $this->assertEquals($name, $node->getNode('filter'));
  20. $this->assertEquals($args, $node->getNode('arguments'));
  21. }
  22. public function getTests()
  23. {
  24. $environment = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
  25. $environment->addFilter(new Twig_SimpleFilter('bar', 'bar', array('needs_environment' => true)));
  26. $environment->addFilter(new Twig_SimpleFilter('barbar', 'twig_tests_filter_barbar', array('needs_context' => true, 'is_variadic' => true)));
  27. $tests = array();
  28. $expr = new Twig_Node_Expression_Constant('foo', 1);
  29. $node = $this->createFilter($expr, 'upper');
  30. $node = $this->createFilter($node, 'number_format', array(new Twig_Node_Expression_Constant(2, 1), new Twig_Node_Expression_Constant('.', 1), new Twig_Node_Expression_Constant(',', 1)));
  31. if (function_exists('mb_get_info')) {
  32. $tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")');
  33. } else {
  34. $tests[] = array($node, 'twig_number_format_filter($this->env, strtoupper("foo"), 2, ".", ",")');
  35. }
  36. // named arguments
  37. $date = new Twig_Node_Expression_Constant(0, 1);
  38. $node = $this->createFilter($date, 'date', array(
  39. 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1),
  40. 'format' => new Twig_Node_Expression_Constant('d/m/Y H:i:s P', 1),
  41. ));
  42. $tests[] = array($node, 'twig_date_format_filter($this->env, 0, "d/m/Y H:i:s P", "America/Chicago")');
  43. // skip an optional argument
  44. $date = new Twig_Node_Expression_Constant(0, 1);
  45. $node = $this->createFilter($date, 'date', array(
  46. 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1),
  47. ));
  48. $tests[] = array($node, 'twig_date_format_filter($this->env, 0, null, "America/Chicago")');
  49. // underscores vs camelCase for named arguments
  50. $string = new Twig_Node_Expression_Constant('abc', 1);
  51. $node = $this->createFilter($string, 'reverse', array(
  52. 'preserve_keys' => new Twig_Node_Expression_Constant(true, 1),
  53. ));
  54. $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
  55. $node = $this->createFilter($string, 'reverse', array(
  56. 'preserveKeys' => new Twig_Node_Expression_Constant(true, 1),
  57. ));
  58. $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
  59. // filter as an anonymous function
  60. if (PHP_VERSION_ID >= 50300) {
  61. $node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous');
  62. $tests[] = array($node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))');
  63. }
  64. // needs environment
  65. $node = $this->createFilter($string, 'bar');
  66. $tests[] = array($node, 'bar($this->env, "abc")', $environment);
  67. $node = $this->createFilter($string, 'bar', array(new Twig_Node_Expression_Constant('bar', 1)));
  68. $tests[] = array($node, 'bar($this->env, "abc", "bar")', $environment);
  69. // arbitrary named arguments
  70. $node = $this->createFilter($string, 'barbar');
  71. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc")', $environment);
  72. $node = $this->createFilter($string, 'barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
  73. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, null, array("foo" => "bar"))', $environment);
  74. $node = $this->createFilter($string, 'barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
  75. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, "bar")', $environment);
  76. $node = $this->createFilter($string, 'barbar', array(
  77. new Twig_Node_Expression_Constant('1', 1),
  78. new Twig_Node_Expression_Constant('2', 1),
  79. new Twig_Node_Expression_Constant('3', 1),
  80. 'foo' => new Twig_Node_Expression_Constant('bar', 1),
  81. ));
  82. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
  83. return $tests;
  84. }
  85. /**
  86. * @expectedException Twig_Error_Syntax
  87. * @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)" at line 1.
  88. */
  89. public function testCompileWithWrongNamedArgumentName()
  90. {
  91. $date = new Twig_Node_Expression_Constant(0, 1);
  92. $node = $this->createFilter($date, 'date', array(
  93. 'foobar' => new Twig_Node_Expression_Constant('America/Chicago', 1),
  94. ));
  95. $compiler = $this->getCompiler();
  96. $compiler->compile($node);
  97. }
  98. /**
  99. * @expectedException Twig_Error_Syntax
  100. * @expectedExceptionMessage Value for argument "from" is required for filter "replace".
  101. */
  102. public function testCompileWithMissingNamedArgument()
  103. {
  104. $value = new Twig_Node_Expression_Constant(0, 1);
  105. $node = $this->createFilter($value, 'replace', array(
  106. 'to' => new Twig_Node_Expression_Constant('foo', 1),
  107. ));
  108. $compiler = $this->getCompiler();
  109. $compiler->compile($node);
  110. }
  111. protected function createFilter($node, $name, array $arguments = array())
  112. {
  113. $name = new Twig_Node_Expression_Constant($name, 1);
  114. $arguments = new Twig_Node($arguments);
  115. return new Twig_Node_Expression_Filter($node, $name, $arguments, 1);
  116. }
  117. protected function getEnvironment()
  118. {
  119. if (PHP_VERSION_ID >= 50300) {
  120. return include 'PHP53/FilterInclude.php';
  121. }
  122. return parent::getEnvironment();
  123. }
  124. }
  125. function twig_tests_filter_barbar($context, $string, $arg1 = null, $arg2 = null, array $args = array())
  126. {
  127. }