]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Bridge\Twig\Tests\Node; | |
13 | ||
14 | use Symfony\Bridge\Twig\Tests\TestCase; | |
15 | use Symfony\Bridge\Twig\Node\FormThemeNode; | |
16 | ||
17 | class FormThemeTest extends TestCase | |
18 | { | |
19 | protected function setUp() | |
20 | { | |
21 | parent::setUp(); | |
22 | ||
23 | if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) { | |
24 | $this->markTestSkipped('Requires Twig version to be at least 1.5.0.'); | |
25 | } | |
26 | } | |
27 | ||
28 | public function testConstructor() | |
29 | { | |
30 | $form = new \Twig_Node_Expression_Name('form', 0); | |
31 | $resources = new \Twig_Node(array( | |
32 | new \Twig_Node_Expression_Constant('tpl1', 0), | |
33 | new \Twig_Node_Expression_Constant('tpl2', 0) | |
34 | )); | |
35 | ||
36 | $node = new FormThemeNode($form, $resources, 0); | |
37 | ||
38 | $this->assertEquals($form, $node->getNode('form')); | |
39 | $this->assertEquals($resources, $node->getNode('resources')); | |
40 | } | |
41 | ||
42 | public function testCompile() | |
43 | { | |
44 | $form = new \Twig_Node_Expression_Name('form', 0); | |
45 | $resources = new \Twig_Node_Expression_Array(array( | |
46 | new \Twig_Node_Expression_Constant(0, 0), | |
47 | new \Twig_Node_Expression_Constant('tpl1', 0), | |
48 | new \Twig_Node_Expression_Constant(1, 0), | |
49 | new \Twig_Node_Expression_Constant('tpl2', 0) | |
50 | ), 0); | |
51 | ||
52 | $node = new FormThemeNode($form, $resources, 0); | |
53 | ||
54 | $compiler = new \Twig_Compiler(new \Twig_Environment()); | |
55 | ||
56 | $this->assertEquals( | |
57 | sprintf( | |
58 | '$this->env->getExtension(\'form\')->renderer->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));', | |
59 | $this->getVariableGetter('form') | |
60 | ), | |
61 | trim($compiler->compile($node)->getSource()) | |
62 | ); | |
63 | ||
64 | $resources = new \Twig_Node_Expression_Constant('tpl1', 0); | |
65 | ||
66 | $node = new FormThemeNode($form, $resources, 0); | |
67 | ||
68 | $this->assertEquals( | |
69 | sprintf( | |
70 | '$this->env->getExtension(\'form\')->renderer->setTheme(%s, "tpl1");', | |
71 | $this->getVariableGetter('form') | |
72 | ), | |
73 | trim($compiler->compile($node)->getSource()) | |
74 | ); | |
75 | } | |
76 | ||
77 | protected function getVariableGetter($name) | |
78 | { | |
79 | if (version_compare(phpversion(), '5.4.0RC1', '>=')) { | |
80 | return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name); | |
81 | } | |
82 | ||
83 | return sprintf('$this->getContext($context, "%s")', $name); | |
84 | } | |
85 | } |