Completed
Push — master ( e207d9...c74d35 )
by Eric
09:33
created

TemplateNameParser::parse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Form Extra package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\FormExtraBundle\Tests\Templating;
13
14
use Ivory\FormExtraBundle\Templating\FormExtraHelper;
15
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
16
use Symfony\Component\Form\FormRenderer;
17
use Symfony\Component\Form\Forms;
18
use Symfony\Component\HttpKernel\Kernel;
19
use Symfony\Component\Templating\Loader\FilesystemLoader;
20
use Symfony\Component\Templating\PhpEngine;
21
use Symfony\Component\Templating\TemplateNameParser as TemplatingNameParser;
22
23
/**
24
 * Ivory Form Extra helper test.
25
 *
26
 * @author GeLo <[email protected]>
27
 */
28
class FormExtraHelperTest extends \PHPUnit_Framework_TestCase
29
{
30
    /** @var \Symfony\Component\Templating\PhpEngine */
31
    private $phpEngine;
32
33
    /** @var \Symfony\Component\Form\FormFactoryInterface */
34
    private $formFactory;
35
36
    /** @var \Symfony\Bridge\Twig\Form\TwigRenderer */
37
    private $formRenderer;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function setUp()
43
    {
44
        $this->phpEngine = new PhpEngine(new TemplateNameParser(), new FilesystemLoader(array(
45
            __DIR__.'/../../Resources/views/Form/%name%',
46
            __DIR__.'/../Fixtures/views/Templating/%name%',
47
        )));
48
49
        $this->formFactory = Forms::createFormFactory();
50
        $this->formRenderer = new FormRenderer(new TemplatingRendererEngine($this->phpEngine, array('')));
51
        $this->phpEngine->addHelpers(array('ivory_form_extra' => new FormExtraHelper($this->formRenderer)));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function tearDown()
58
    {
59
        unset($this->formRenderer);
60
        unset($this->formFactory);
61
        unset($this->phpEngine);
62
    }
63
64
    public function testDefaultJavascriptFragment()
65
    {
66
        $form = $this->formFactory->createBuilder()
67
            ->add('text', $this->getFormType('text'))
68
            ->add('submit', $this->getFormType('submit'))
69
            ->getForm()
70
            ->createView();
71
72
        $this->assertEmpty($this->normalize($this->phpEngine->render('javascript.html.php', array('form' => $form))));
73
    }
74
75
    public function testCustomJavascriptFragment()
76
    {
77
        $form = $this->formFactory->createBuilder()
78
            ->add('text', $this->getFormType('text'))
79
            ->add('submit', $this->getFormType('submit'))
80
            ->getForm()
81
            ->createView();
82
83
        $this->formRenderer->setTheme($form, 'Custom');
84
85
        $expected = '<script type="text/javascript">text-javascript</script>';
86
        $expected .= '<script type="text/javascript">submit-javascript</script>';
87
88
        $this->assertSame(
89
            $expected,
90
            $this->normalize($this->phpEngine->render('javascript.html.php', array('form' => $form)))
91
        );
92
    }
93
94
    public function testInheritanceJavascriptFragment()
95
    {
96
        $form = $this->formFactory->createBuilder()
97
            ->add('textarea', $this->getFormType('textarea'))
98
            ->add('submit', $this->getFormType('submit'))
99
            ->getForm()
100
            ->createView();
101
102
        $this->formRenderer->setTheme($form, 'Inheritance');
103
104
        $expected = '<script type="text/javascript">text-javascript</script>';
105
        $expected .= '<script type="text/javascript">button-javascript</script>';
106
107
        $this->assertSame(
108
            $expected,
109
            $this->normalize($this->phpEngine->render('javascript.html.php', array('form' => $form)))
110
        );
111
    }
112
113
    public function testDefaultStylesheetFragment()
114
    {
115
        $form = $this->formFactory->createBuilder()
116
            ->add('text', $this->getFormType('text'))
117
            ->add('submit', $this->getFormType('submit'))
118
            ->getForm()
119
            ->createView();
120
121
        $this->assertEmpty($this->normalize($this->phpEngine->render('stylesheet.html.php', array('form' => $form))));
122
    }
123
124
    public function testCustomStylesheetFragment()
125
    {
126
        $form = $this->formFactory->createBuilder()
127
            ->add('text', $this->getFormType('text'))
128
            ->add('submit', $this->getFormType('submit'))
129
            ->getForm()
130
            ->createView();
131
132
        $this->formRenderer->setTheme($form, 'Custom');
133
134
        $expected = '<style type="text/css">text-stylesheet</style>';
135
        $expected .= '<style type="text/css">submit-stylesheet</style>';
136
137
        $this->assertSame(
138
            $expected,
139
            $this->normalize($this->phpEngine->render('stylesheet.html.php', array('form' => $form)))
140
        );
141
    }
142
143
    public function testInheritanceStylesheetFragment()
144
    {
145
        $form = $this->formFactory->createBuilder()
146
            ->add('textarea', $this->getFormType('textarea'))
147
            ->add('submit', $this->getFormType('submit'))
148
            ->getForm()
149
            ->createView();
150
151
        $this->formRenderer->setTheme($form, 'Inheritance');
152
153
        $expected = '<style type="text/css">text-stylesheet</style>';
154
        $expected .= '<style type="text/css">button-stylesheet</style>';
155
156
        $this->assertSame(
157
            $expected,
158
            $this->normalize($this->phpEngine->render('stylesheet.html.php', array('form' => $form)))
159
        );
160
    }
161
162
    /**
163
     * Gets the form type according to the Symfony version.
164
     *
165
     * @param string $type The form type.
166
     *
167
     * @return string The form type.
168
     */
169
    private function getFormType($type)
170
    {
171
        return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
172
            ? 'Symfony\Component\Form\Extension\Core\Type\\'.ucfirst($type).'Type'
173
            : $type;
174
    }
175
176
    /**
177
     * Normalizes the output as there is no space control in the PHP templating component.
178
     *
179
     * @param string $output The output.
180
     *
181
     * @return string The normalized output.
182
     */
183
    private function normalize($output)
184
    {
185
        return trim(preg_replace('/>\s+</', '><', preg_replace('/\s+/', ' ', $output)));
186
    }
187
}
188
189
/**
190
 * @author GeLo <[email protected]>
191
 */
192
class TemplateNameParser extends TemplatingNameParser
193
{
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function parse($name)
198
    {
199
        if (is_string($name) && strpos($name, ':') !== false) {
200
            list($theme, $template) = explode(':', $name);
201
202
            if (empty($theme)) {
203
                $name = $template;
204
            } else {
205
                $name = $theme.'/'.$template;
206
            }
207
        }
208
209
        return parent::parse($name);
210
    }
211
}
212