TransTest.php 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. require_once TWIG_LIB_DIR.'/../test/Twig/Tests/Node/TestCase.php';
  11. class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase
  12. {
  13. /**
  14. * @covers Twig_Node_Trans::__construct
  15. */
  16. public function testConstructor()
  17. {
  18. $count = new Twig_Node_Expression_Constant(12, 0);
  19. $body = new Twig_Node(array(
  20. new Twig_Node_Text('Hello', 0),
  21. ), array(), 0);
  22. $plural = new Twig_Node(array(
  23. new Twig_Node_Text('Hey ', 0),
  24. new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
  25. new Twig_Node_Text(', I have ', 0),
  26. new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
  27. new Twig_Node_Text(' apples', 0),
  28. ), array(), 0);
  29. $node = new Twig_Extensions_Node_Trans($body, $plural, $count, 0);
  30. $this->assertEquals($body, $node->getNode('body'));
  31. $this->assertEquals($count, $node->getNode('count'));
  32. $this->assertEquals($plural, $node->getNode('plural'));
  33. }
  34. public function getTests()
  35. {
  36. $tests = array();
  37. $body = new Twig_Node_Expression_Name('foo', 0);
  38. $node = new Twig_Extensions_Node_Trans($body, null, null, 0);
  39. $tests[] = array($node, sprintf('echo gettext(%s);', $this->getVariableGetter('foo')));
  40. $body = new Twig_Node_Expression_Constant('Hello', 0);
  41. $node = new Twig_Extensions_Node_Trans($body, null, null, 0);
  42. $tests[] = array($node, 'echo gettext("Hello");');
  43. $body = new Twig_Node(array(
  44. new Twig_Node_Text('Hello', 0),
  45. ), array(), 0);
  46. $node = new Twig_Extensions_Node_Trans($body, null, null, 0);
  47. $tests[] = array($node, 'echo gettext("Hello");');
  48. $body = new Twig_Node(array(
  49. new Twig_Node_Text('J\'ai ', 0),
  50. new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
  51. new Twig_Node_Text(' pommes', 0),
  52. ), array(), 0);
  53. $node = new Twig_Extensions_Node_Trans($body, null, null, 0);
  54. $tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
  55. $count = new Twig_Node_Expression_Constant(12, 0);
  56. $body = new Twig_Node(array(
  57. new Twig_Node_Text('Hey ', 0),
  58. new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
  59. new Twig_Node_Text(', I have one apple', 0),
  60. ), array(), 0);
  61. $plural = new Twig_Node(array(
  62. new Twig_Node_Text('Hey ', 0),
  63. new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
  64. new Twig_Node_Text(', I have ', 0),
  65. new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
  66. new Twig_Node_Text(' apples', 0),
  67. ), array(), 0);
  68. $node = new Twig_Extensions_Node_Trans($body, $plural, $count, 0);
  69. $tests[] = array($node, sprintf('echo strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have %%count%% apples", abs(12)), array("%%name%%" => %s, "%%name%%" => %s, "%%count%%" => abs(12), ));', $this->getVariableGetter('name'), $this->getVariableGetter('name')));
  70. // with escaper extension set to on
  71. $body = new Twig_Node(array(
  72. new Twig_Node_Text('J\'ai ', 0),
  73. new Twig_Node_Print(new Twig_Node_Expression_Filter(new Twig_Node_Expression_Name('foo', 0), new Twig_Node_Expression_Constant('escape', 0), new Twig_Node(), 0), 0),
  74. new Twig_Node_Text(' pommes', 0),
  75. ), array(), 0);
  76. $node = new Twig_Extensions_Node_Trans($body, null, null, 0);
  77. $tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
  78. return $tests;
  79. }
  80. }