Skip to content

Commit 9f5ed0e

Browse files
committed
[TwigBridge] Add FormLayoutTestCase class
1 parent 541e845 commit 9f5ed0e

12 files changed

+282
-653
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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\Test;
13+
14+
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
15+
use Symfony\Bridge\Twig\Test\Traits\RuntimeLoaderProvider;
16+
use Symfony\Component\Form\FormRenderer;
17+
use Symfony\Component\Form\FormRendererInterface;
18+
use Symfony\Component\Form\FormView;
19+
use Symfony\Component\Form\Test\FormIntegrationTestCase;
20+
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
21+
use Twig\Environment;
22+
use Twig\Loader\FilesystemLoader;
23+
24+
/**
25+
* @author Romain Monteil <monteil.romain@gmail.com>
26+
*/
27+
abstract class FormLayoutTestCase extends FormIntegrationTestCase
28+
{
29+
use RuntimeLoaderProvider;
30+
31+
protected FormRendererInterface $renderer;
32+
33+
protected function setUp(): void
34+
{
35+
parent::setUp();
36+
37+
$loader = new FilesystemLoader($this->getTemplatePaths());
38+
39+
$environment = new Environment($loader, ['strict_variables' => true]);
40+
$environment->setExtensions($this->getTwigExtensions());
41+
42+
foreach ($this->getTwigGlobals() as $name => $value) {
43+
$environment->addGlobal($name, $value);
44+
}
45+
46+
$rendererEngine = new TwigRendererEngine($this->getThemes(), $environment);
47+
$this->renderer = new FormRenderer($rendererEngine, $this->createMock(CsrfTokenManagerInterface::class));
48+
$this->registerTwigRuntimeLoader($environment, $this->renderer);
49+
}
50+
51+
protected function assertMatchesXpath($html, $expression, $count = 1): void
52+
{
53+
$dom = new \DOMDocument('UTF-8');
54+
55+
$html = preg_replace('/(<input [^>]+)(?<!\/)>/', '$1/>', $html);
56+
57+
try {
58+
// Wrap in <root> node so we can load HTML with multiple tags at
59+
// the top level
60+
$dom->loadXML('<root>'.$html.'</root>');
61+
} catch (\Exception $e) {
62+
$this->fail(sprintf(
63+
"Failed loading HTML:\n\n%s\n\nError: %s",
64+
$html,
65+
$e->getMessage()
66+
));
67+
}
68+
$xpath = new \DOMXPath($dom);
69+
$nodeList = $xpath->evaluate('/root'.$expression);
70+
71+
if ($nodeList->length != $count) {
72+
$dom->formatOutput = true;
73+
$this->fail(sprintf(
74+
"Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
75+
$expression,
76+
1 == $count ? 'once' : $count.' times',
77+
1 == $nodeList->length ? 'once' : $nodeList->length.' times',
78+
// strip away <root> and </root>
79+
substr($dom->saveHTML(), 6, -8)
80+
));
81+
} else {
82+
$this->addToAssertionCount(1);
83+
}
84+
}
85+
86+
abstract protected function getTemplatePaths(): array;
87+
88+
abstract protected function getTwigExtensions(): array;
89+
90+
protected function getTwigGlobals(): array
91+
{
92+
return [];
93+
}
94+
95+
abstract protected function getThemes(): array;
96+
97+
protected function renderForm(FormView $view, array $vars = []): string
98+
{
99+
return $this->renderer->renderBlock($view, 'form', $vars);
100+
}
101+
102+
protected function renderLabel(FormView $view, $label = null, array $vars = []): string
103+
{
104+
if (null !== $label) {
105+
$vars += ['label' => $label];
106+
}
107+
108+
return $this->renderer->searchAndRenderBlock($view, 'label', $vars);
109+
}
110+
111+
protected function renderHelp(FormView $view): string
112+
{
113+
return $this->renderer->searchAndRenderBlock($view, 'help');
114+
}
115+
116+
protected function renderErrors(FormView $view): string
117+
{
118+
return $this->renderer->searchAndRenderBlock($view, 'errors');
119+
}
120+
121+
protected function renderWidget(FormView $view, array $vars = []): string
122+
{
123+
return $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
124+
}
125+
126+
protected function renderRow(FormView $view, array $vars = []): string
127+
{
128+
return $this->renderer->searchAndRenderBlock($view, 'row', $vars);
129+
}
130+
131+
protected function renderRest(FormView $view, array $vars = []): string
132+
{
133+
return $this->renderer->searchAndRenderBlock($view, 'rest', $vars);
134+
}
135+
136+
protected function renderStart(FormView $view, array $vars = []): string
137+
{
138+
return $this->renderer->renderBlock($view, 'form_start', $vars);
139+
}
140+
141+
protected function renderEnd(FormView $view, array $vars = []): string
142+
{
143+
return $this->renderer->renderBlock($view, 'form_end', $vars);
144+
}
145+
146+
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true): void
147+
{
148+
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
149+
}
150+
}

src/Symfony/Bridge/Twig/Tests/Extension/RuntimeLoaderProvider.php renamed to src/Symfony/Bridge/Twig/Test/Traits/RuntimeLoaderProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Bridge\Twig\Tests\Extension;
12+
namespace Symfony\Bridge\Twig\Test\Traits;
1313

1414
use Symfony\Component\Form\FormRenderer;
1515
use Twig\Environment;

src/Symfony/Bridge/Twig/Tests/Extension/AbstractLayoutTestCase.php

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

1414
use PHPUnit\Framework\MockObject\MockObject;
15+
use Symfony\Bridge\Twig\Test\FormLayoutTestCase;
1516
use Symfony\Component\Form\Extension\Core\Type\PercentType;
1617
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1718
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
1819
use Symfony\Component\Form\FormError;
1920
use Symfony\Component\Form\FormView;
20-
use Symfony\Component\Form\Test\FormIntegrationTestCase;
2121
use Symfony\Component\Form\Tests\VersionAwareTest;
2222
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
2323
use Symfony\Component\Translation\TranslatableMessage;
2424
use Symfony\Contracts\Translation\TranslatableInterface;
2525
use Symfony\Contracts\Translation\TranslatorInterface;
2626

27-
abstract class AbstractLayoutTestCase extends FormIntegrationTestCase
27+
abstract class AbstractLayoutTestCase extends FormLayoutTestCase
2828
{
2929
use VersionAwareTest;
3030

@@ -61,49 +61,6 @@ protected function tearDown(): void
6161
parent::tearDown();
6262
}
6363

64-
protected function assertXpathNodeValue(\DOMElement $element, $expression, $nodeValue)
65-
{
66-
$xpath = new \DOMXPath($element->ownerDocument);
67-
$nodeList = $xpath->evaluate($expression);
68-
$this->assertEquals(1, $nodeList->length);
69-
$this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
70-
}
71-
72-
protected function assertMatchesXpath($html, $expression, $count = 1)
73-
{
74-
$dom = new \DOMDocument('UTF-8');
75-
76-
$html = preg_replace('/(<input [^>]+)(?<!\/)>/', '$1/>', $html);
77-
78-
try {
79-
// Wrap in <root> node so we can load HTML with multiple tags at
80-
// the top level
81-
$dom->loadXML('<root>'.$html.'</root>');
82-
} catch (\Exception $e) {
83-
$this->fail(sprintf(
84-
"Failed loading HTML:\n\n%s\n\nError: %s",
85-
$html,
86-
$e->getMessage()
87-
));
88-
}
89-
$xpath = new \DOMXPath($dom);
90-
$nodeList = $xpath->evaluate('/root'.$expression);
91-
92-
if ($nodeList->length != $count) {
93-
$dom->formatOutput = true;
94-
$this->fail(sprintf(
95-
"Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
96-
$expression,
97-
1 == $count ? 'once' : $count.' times',
98-
1 == $nodeList->length ? 'once' : $nodeList->length.' times',
99-
// strip away <root> and </root>
100-
substr($dom->saveHTML(), 6, -8)
101-
));
102-
} else {
103-
$this->addToAssertionCount(1);
104-
}
105-
}
106-
10764
protected function assertWidgetMatchesXpath(FormView $view, array $vars, $xpath)
10865
{
10966
// include ampersands everywhere to validate escaping
@@ -125,29 +82,6 @@ protected function assertWidgetMatchesXpath(FormView $view, array $vars, $xpath)
12582
$this->assertMatchesXpath($html, $xpath);
12683
}
12784

128-
abstract protected function renderForm(FormView $view, array $vars = []);
129-
130-
abstract protected function renderLabel(FormView $view, $label = null, array $vars = []);
131-
132-
protected function renderHelp(FormView $view)
133-
{
134-
$this->markTestSkipped(sprintf('%s::renderHelp() is not implemented.', static::class));
135-
}
136-
137-
abstract protected function renderErrors(FormView $view);
138-
139-
abstract protected function renderWidget(FormView $view, array $vars = []);
140-
141-
abstract protected function renderRow(FormView $view, array $vars = []);
142-
143-
abstract protected function renderRest(FormView $view, array $vars = []);
144-
145-
abstract protected function renderStart(FormView $view, array $vars = []);
146-
147-
abstract protected function renderEnd(FormView $view, array $vars = []);
148-
149-
abstract protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true);
150-
15185
public function testLabel()
15286
{
15387
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,96 +13,35 @@
1313

1414
use Symfony\Bridge\Twig\Extension\FormExtension;
1515
use Symfony\Bridge\Twig\Extension\TranslationExtension;
16-
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
1716
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
18-
use Symfony\Component\Form\FormRenderer;
19-
use Symfony\Component\Form\FormView;
20-
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
21-
use Twig\Environment;
22-
use Twig\Loader\FilesystemLoader;
2317

2418
class FormExtensionBootstrap3HorizontalLayoutTest extends AbstractBootstrap3HorizontalLayoutTestCase
2519
{
26-
use RuntimeLoaderProvider;
27-
2820
protected array $testableFeatures = [
2921
'choice_attr',
3022
];
3123

32-
private FormRenderer $renderer;
33-
34-
protected function setUp(): void
24+
protected function getTemplatePaths(): array
3525
{
36-
parent::setUp();
37-
38-
$loader = new FilesystemLoader([
26+
return [
3927
__DIR__.'/../../Resources/views/Form',
4028
__DIR__.'/Fixtures/templates/form',
41-
]);
42-
43-
$environment = new Environment($loader, ['strict_variables' => true]);
44-
$environment->addExtension(new TranslationExtension(new StubTranslator()));
45-
$environment->addExtension(new FormExtension());
46-
47-
$rendererEngine = new TwigRendererEngine([
48-
'bootstrap_3_horizontal_layout.html.twig',
49-
'custom_widgets.html.twig',
50-
], $environment);
51-
$this->renderer = new FormRenderer($rendererEngine, $this->createMock(CsrfTokenManagerInterface::class));
52-
$this->registerTwigRuntimeLoader($environment, $this->renderer);
53-
}
54-
55-
protected function renderForm(FormView $view, array $vars = [])
56-
{
57-
return $this->renderer->renderBlock($view, 'form', $vars);
58-
}
59-
60-
protected function renderLabel(FormView $view, $label = null, array $vars = [])
61-
{
62-
if (null !== $label) {
63-
$vars += ['label' => $label];
64-
}
65-
66-
return $this->renderer->searchAndRenderBlock($view, 'label', $vars);
67-
}
68-
69-
protected function renderHelp(FormView $view)
70-
{
71-
return $this->renderer->searchAndRenderBlock($view, 'help');
29+
];
7230
}
7331

74-
protected function renderErrors(FormView $view)
32+
protected function getTwigExtensions(): array
7533
{
76-
return $this->renderer->searchAndRenderBlock($view, 'errors');
34+
return [
35+
new TranslationExtension(new StubTranslator()),
36+
new FormExtension(),
37+
];
7738
}
7839

79-
protected function renderWidget(FormView $view, array $vars = [])
40+
protected function getThemes(): array
8041
{
81-
return $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
82-
}
83-
84-
protected function renderRow(FormView $view, array $vars = [])
85-
{
86-
return $this->renderer->searchAndRenderBlock($view, 'row', $vars);
87-
}
88-
89-
protected function renderRest(FormView $view, array $vars = [])
90-
{
91-
return $this->renderer->searchAndRenderBlock($view, 'rest', $vars);
92-
}
93-
94-
protected function renderStart(FormView $view, array $vars = [])
95-
{
96-
return $this->renderer->renderBlock($view, 'form_start', $vars);
97-
}
98-
99-
protected function renderEnd(FormView $view, array $vars = [])
100-
{
101-
return $this->renderer->renderBlock($view, 'form_end', $vars);
102-
}
103-
104-
protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
105-
{
106-
$this->renderer->setTheme($view, $themes, $useDefaultThemes);
42+
return [
43+
'bootstrap_3_horizontal_layout.html.twig',
44+
'custom_widgets.html.twig',
45+
];
10746
}
10847
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy