]>
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\Extension; | |
13 | ||
14 | use Symfony\Bridge\Twig\Extension\FormExtension; | |
15 | use Symfony\Bridge\Twig\Form\TwigRenderer; | |
16 | use Symfony\Bridge\Twig\Form\TwigRendererEngine; | |
17 | use Symfony\Bridge\Twig\Extension\TranslationExtension; | |
18 | use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator; | |
19 | use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader; | |
20 | use Symfony\Component\Form\FormView; | |
21 | use Symfony\Component\Form\Extension\Core\View\ChoiceView; | |
22 | use Symfony\Component\Form\Tests\AbstractDivLayoutTest; | |
23 | ||
24 | class FormExtensionDivLayoutTest extends AbstractDivLayoutTest | |
25 | { | |
26 | /** | |
27 | * @var FormExtension | |
28 | */ | |
29 | protected $extension; | |
30 | ||
31 | protected function setUp() | |
32 | { | |
33 | if (!class_exists('Symfony\Component\Locale\Locale')) { | |
34 | $this->markTestSkipped('The "Locale" component is not available'); | |
35 | } | |
36 | ||
37 | if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) { | |
38 | $this->markTestSkipped('The "EventDispatcher" component is not available'); | |
39 | } | |
40 | ||
41 | if (!class_exists('Symfony\Component\Form\Form')) { | |
42 | $this->markTestSkipped('The "Form" component is not available'); | |
43 | } | |
44 | ||
45 | if (!class_exists('Twig_Environment')) { | |
46 | $this->markTestSkipped('Twig is not available.'); | |
47 | } | |
48 | ||
49 | parent::setUp(); | |
50 | ||
51 | $rendererEngine = new TwigRendererEngine(array( | |
52 | 'form_div_layout.html.twig', | |
53 | 'custom_widgets.html.twig', | |
54 | )); | |
55 | $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface')); | |
56 | ||
57 | $this->extension = new FormExtension($renderer); | |
58 | ||
59 | $loader = new StubFilesystemLoader(array( | |
60 | __DIR__.'/../../Resources/views/Form', | |
61 | __DIR__, | |
62 | )); | |
63 | ||
64 | $environment = new \Twig_Environment($loader, array('strict_variables' => true)); | |
65 | $environment->addExtension(new TranslationExtension(new StubTranslator())); | |
66 | $environment->addGlobal('global', ''); | |
67 | $environment->addExtension($this->extension); | |
68 | ||
69 | $this->extension->initRuntime($environment); | |
70 | } | |
71 | ||
72 | protected function tearDown() | |
73 | { | |
74 | parent::tearDown(); | |
75 | ||
76 | $this->extension = null; | |
77 | } | |
78 | ||
79 | public function testThemeBlockInheritanceUsingUse() | |
80 | { | |
81 | $view = $this->factory | |
82 | ->createNamed('name', 'email') | |
83 | ->createView() | |
84 | ; | |
85 | ||
86 | $this->setTheme($view, array('theme_use.html.twig')); | |
87 | ||
88 | $this->assertMatchesXpath( | |
89 | $this->renderWidget($view), | |
90 | '/input[@type="email"][@rel="theme"]' | |
91 | ); | |
92 | } | |
93 | ||
94 | public function testThemeBlockInheritanceUsingExtend() | |
95 | { | |
96 | $view = $this->factory | |
97 | ->createNamed('name', 'email') | |
98 | ->createView() | |
99 | ; | |
100 | ||
101 | $this->setTheme($view, array('theme_extends.html.twig')); | |
102 | ||
103 | $this->assertMatchesXpath( | |
104 | $this->renderWidget($view), | |
105 | '/input[@type="email"][@rel="theme"]' | |
106 | ); | |
107 | } | |
108 | ||
109 | public function isSelectedChoiceProvider() | |
110 | { | |
111 | // The commented cases should not be necessary anymore, because the | |
112 | // choice lists should assure that both values passed here are always | |
113 | // strings | |
114 | return array( | |
115 | // array(true, 0, 0), | |
116 | array(true, '0', '0'), | |
117 | array(true, '1', '1'), | |
118 | // array(true, false, 0), | |
119 | // array(true, true, 1), | |
120 | array(true, '', ''), | |
121 | // array(true, null, ''), | |
122 | array(true, '1.23', '1.23'), | |
123 | array(true, 'foo', 'foo'), | |
124 | array(true, 'foo10', 'foo10'), | |
125 | array(true, 'foo', array(1, 'foo', 'foo10')), | |
126 | ||
127 | array(false, 10, array(1, 'foo', 'foo10')), | |
128 | array(false, 0, array(1, 'foo', 'foo10')), | |
129 | ); | |
130 | } | |
131 | ||
132 | /** | |
133 | * @dataProvider isSelectedChoiceProvider | |
134 | */ | |
135 | public function testIsChoiceSelected($expected, $choice, $value) | |
136 | { | |
137 | $choice = new ChoiceView($choice, $choice, $choice.' label'); | |
138 | ||
139 | $this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value)); | |
140 | } | |
141 | ||
142 | protected function renderForm(FormView $view, array $vars = array()) | |
143 | { | |
144 | return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); | |
145 | } | |
146 | ||
147 | protected function renderEnctype(FormView $view) | |
148 | { | |
149 | return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype'); | |
150 | } | |
151 | ||
152 | protected function renderLabel(FormView $view, $label = null, array $vars = array()) | |
153 | { | |
154 | if ($label !== null) { | |
155 | $vars += array('label' => $label); | |
156 | } | |
157 | ||
158 | return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); | |
159 | } | |
160 | ||
161 | protected function renderErrors(FormView $view) | |
162 | { | |
163 | return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors'); | |
164 | } | |
165 | ||
166 | protected function renderWidget(FormView $view, array $vars = array()) | |
167 | { | |
168 | return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); | |
169 | } | |
170 | ||
171 | protected function renderRow(FormView $view, array $vars = array()) | |
172 | { | |
173 | return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); | |
174 | } | |
175 | ||
176 | protected function renderRest(FormView $view, array $vars = array()) | |
177 | { | |
178 | return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars); | |
179 | } | |
180 | ||
181 | protected function renderStart(FormView $view, array $vars = array()) | |
182 | { | |
183 | return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars); | |
184 | } | |
185 | ||
186 | protected function renderEnd(FormView $view, array $vars = array()) | |
187 | { | |
188 | return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars); | |
189 | } | |
190 | ||
191 | protected function setTheme(FormView $view, array $themes) | |
192 | { | |
193 | $this->extension->renderer->setTheme($view, $themes); | |
194 | } | |
195 | ||
196 | public static function themeBlockInheritanceProvider() | |
197 | { | |
198 | return array( | |
199 | array(array('theme.html.twig')) | |
200 | ); | |
201 | } | |
202 | ||
203 | public static function themeInheritanceProvider() | |
204 | { | |
205 | return array( | |
206 | array(array('parent_label.html.twig'), array('child_label.html.twig')) | |
207 | ); | |
208 | } | |
209 | } |