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
12class Twig_Tests_Node_TransTest extends Twig_Test_NodeTestCase
13{
14    /**
15     * @covers \Twig_Node_Trans::__construct
16     */
17    public function testConstructor()
18    {
19        $count = new Twig_Node_Expression_Constant(12, 0);
20        $body = new Twig_Node(array(
21            new Twig_Node_Text('Hello', 0),
22        ), array(), 0);
23        $plural = new Twig_Node(array(
24            new Twig_Node_Text('Hey ', 0),
25            new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
26            new Twig_Node_Text(', I have ', 0),
27            new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
28            new Twig_Node_Text(' apples', 0),
29        ), array(), 0);
30        $node = new Twig_Extensions_Node_Trans($body, $plural, $count, null, 0);
31
32        $this->assertEquals($body, $node->getNode('body'));
33        $this->assertEquals($count, $node->getNode('count'));
34        $this->assertEquals($plural, $node->getNode('plural'));
35    }
36
37    public function getTests()
38    {
39        $tests = array();
40
41        $body = new Twig_Node_Expression_Name('foo', 0);
42        $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
43        $tests[] = array($node, sprintf('echo gettext(%s);', $this->getVariableGetter('foo')));
44
45        $body = new Twig_Node_Expression_Constant('Hello', 0);
46        $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
47        $tests[] = array($node, 'echo gettext("Hello");');
48
49        $body = new Twig_Node(array(
50            new Twig_Node_Text('Hello', 0),
51        ), array(), 0);
52        $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
53        $tests[] = array($node, 'echo gettext("Hello");');
54
55        $body = new Twig_Node(array(
56            new Twig_Node_Text('J\'ai ', 0),
57            new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0),
58            new Twig_Node_Text(' pommes', 0),
59        ), array(), 0);
60        $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
61        $tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
62
63        $count = new Twig_Node_Expression_Constant(12, 0);
64        $body = new Twig_Node(array(
65            new Twig_Node_Text('Hey ', 0),
66            new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
67            new Twig_Node_Text(', I have one apple', 0),
68        ), array(), 0);
69        $plural = new Twig_Node(array(
70            new Twig_Node_Text('Hey ', 0),
71            new Twig_Node_Print(new Twig_Node_Expression_Name('name', 0), 0),
72            new Twig_Node_Text(', I have ', 0),
73            new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
74            new Twig_Node_Text(' apples', 0),
75        ), array(), 0);
76        $node = new Twig_Extensions_Node_Trans($body, $plural, $count, null, 0);
77        $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')));
78
79        // with escaper extension set to on
80        $body = new Twig_Node(array(
81            new Twig_Node_Text('J\'ai ', 0),
82            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),
83            new Twig_Node_Text(' pommes', 0),
84        ), array(), 0);
85
86        $node = new Twig_Extensions_Node_Trans($body, null, null, null, 0);
87        $tests[] = array($node, sprintf('echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));', $this->getVariableGetter('foo')));
88
89        // with notes
90        $body = new Twig_Node_Expression_Constant('Hello', 0);
91        $notes = new Twig_Node_Text('Notes for translators', 0);
92        $node = new Twig_Extensions_Node_Trans($body, null, null, $notes, 0);
93        $tests[] = array($node, "// notes: Notes for translators\necho gettext(\"Hello\");");
94
95        $body = new Twig_Node_Expression_Constant('Hello', 0);
96        $notes = new Twig_Node_Text("Notes for translators\nand line breaks", 0);
97        $node = new Twig_Extensions_Node_Trans($body, null, null, $notes, 0);
98        $tests[] = array($node, "// notes: Notes for translators and line breaks\necho gettext(\"Hello\");");
99
100        $count = new Twig_Node_Expression_Constant(5, 0);
101        $body = new Twig_Node_Text('There is 1 pending task', 0);
102        $plural = new Twig_Node(array(
103            new Twig_Node_Text('There are ', 0),
104            new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0),
105            new Twig_Node_Text(' pending tasks', 0),
106        ), array(), 0);
107        $notes = new Twig_Node_Text('Notes for translators', 0);
108        $node = new Twig_Extensions_Node_Trans($body, $plural, $count, $notes, 0);
109        $tests[] = array($node, "// notes: Notes for translators\n".'echo strtr(ngettext("There is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));');
110
111        return $tests;
112    }
113}
114