PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/arthur_quiroga/dystawork
PHP | 151 lines | 101 code | 28 blank | 22 comment | 0 complexity | 48a5ce42b0f8e3a2f842bfb83fcb555a 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_Filter('bar', 'twig_tests_filter_dummy', array('needs_environment' => true)));
  26. $environment->addFilter(new Twig_Filter('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. $tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")');
  32. // named arguments
  33. $date = new Twig_Node_Expression_Constant(0, 1);
  34. $node = $this->createFilter($date, 'date', array(
  35. 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1),
  36. 'format' => new Twig_Node_Expression_Constant('d/m/Y H:i:s P', 1),
  37. ));
  38. $tests[] = array($node, 'twig_date_format_filter($this->env, 0, "d/m/Y H:i:s P", "America/Chicago")');
  39. // skip an optional argument
  40. $date = new Twig_Node_Expression_Constant(0, 1);
  41. $node = $this->createFilter($date, 'date', array(
  42. 'timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1),
  43. ));
  44. $tests[] = array($node, 'twig_date_format_filter($this->env, 0, null, "America/Chicago")');
  45. // underscores vs camelCase for named arguments
  46. $string = new Twig_Node_Expression_Constant('abc', 1);
  47. $node = $this->createFilter($string, 'reverse', array(
  48. 'preserve_keys' => new Twig_Node_Expression_Constant(true, 1),
  49. ));
  50. $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
  51. $node = $this->createFilter($string, 'reverse', array(
  52. 'preserveKeys' => new Twig_Node_Expression_Constant(true, 1),
  53. ));
  54. $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
  55. // filter as an anonymous function
  56. $node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous');
  57. $tests[] = array($node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))');
  58. // needs environment
  59. $node = $this->createFilter($string, 'bar');
  60. $tests[] = array($node, 'twig_tests_filter_dummy($this->env, "abc")', $environment);
  61. $node = $this->createFilter($string, 'bar', array(new Twig_Node_Expression_Constant('bar', 1)));
  62. $tests[] = array($node, 'twig_tests_filter_dummy($this->env, "abc", "bar")', $environment);
  63. // arbitrary named arguments
  64. $node = $this->createFilter($string, 'barbar');
  65. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc")', $environment);
  66. $node = $this->createFilter($string, 'barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
  67. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, null, array("foo" => "bar"))', $environment);
  68. $node = $this->createFilter($string, 'barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
  69. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, "bar")', $environment);
  70. $node = $this->createFilter($string, 'barbar', array(
  71. new Twig_Node_Expression_Constant('1', 1),
  72. new Twig_Node_Expression_Constant('2', 1),
  73. new Twig_Node_Expression_Constant('3', 1),
  74. 'foo' => new Twig_Node_Expression_Constant('bar', 1),
  75. ));
  76. $tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
  77. return $tests;
  78. }
  79. /**
  80. * @expectedException Twig_Error_Syntax
  81. * @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)" at line 1.
  82. */
  83. public function testCompileWithWrongNamedArgumentName()
  84. {
  85. $date = new Twig_Node_Expression_Constant(0, 1);
  86. $node = $this->createFilter($date, 'date', array(
  87. 'foobar' => new Twig_Node_Expression_Constant('America/Chicago', 1),
  88. ));
  89. $compiler = $this->getCompiler();
  90. $compiler->compile($node);
  91. }
  92. /**
  93. * @expectedException Twig_Error_Syntax
  94. * @expectedExceptionMessage Value for argument "from" is required for filter "replace".
  95. */
  96. public function testCompileWithMissingNamedArgument()
  97. {
  98. $value = new Twig_Node_Expression_Constant(0, 1);
  99. $node = $this->createFilter($value, 'replace', array(
  100. 'to' => new Twig_Node_Expression_Constant('foo', 1),
  101. ));
  102. $compiler = $this->getCompiler();
  103. $compiler->compile($node);
  104. }
  105. protected function createFilter($node, $name, array $arguments = array())
  106. {
  107. $name = new Twig_Node_Expression_Constant($name, 1);
  108. $arguments = new Twig_Node($arguments);
  109. return new Twig_Node_Expression_Filter($node, $name, $arguments, 1);
  110. }
  111. protected function getEnvironment()
  112. {
  113. $env = new Twig_Environment(new Twig_Loader_Array(array()));
  114. $env->addFilter(new Twig_Filter('anonymous', function () {}));
  115. return $env;
  116. }
  117. }
  118. function twig_tests_filter_dummy()
  119. {
  120. }
  121. function twig_tests_filter_barbar($context, $string, $arg1 = null, $arg2 = null, array $args = array())
  122. {
  123. }