-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEndToEndTest.php
More file actions
85 lines (69 loc) · 2.92 KB
/
EndToEndTest.php
File metadata and controls
85 lines (69 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Webfactory\ShortcodeBundle\Tests\Functional;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Test that given test shortcodes are found in a Twig template and being replaced with the
* corresponding test controller's response content.
*/
final class EndToEndTest extends KernelTestCase
{
#[Test]
public function paragraphs_wrapping_shortcodes_get_removed(): void
{
$this->assertEquals(
'test',
$this->renderTwig("{{ '<p>[test-config-inline]</p>' | shortcodes }}")
);
}
#[Test]
public function content_without_shortcodes_wont_be_changed(): void
{
$this->assertEquals(
'<p>Content without shortcode</p>',
$this->renderTwig("{{ '<p>Content without shortcode</p>' | shortcodes }}")
);
}
#[DataProvider('provideShortcodeNames')]
#[Test]
public function expand_shortcode_in__twig(string $shortcodeName): void
{
$result = $this->renderTwig('{{ content | shortcodes }}', ['content' => "[$shortcodeName foo=bar]"]);
self::assertSame('test foo=bar', $result);
}
public static function provideShortcodeNames(): Generator
{
yield 'Inline shortcode defined in bundle config' => ['test-config-inline'];
yield 'ESI-based shortcode defined in bundle config' => ['test-config-esi'];
yield 'Inline shortcode defined in service definitions' => ['test-service-inline'];
yield 'ESI-based shortcode defined in service definitions' => ['test-service-esi'];
}
#[DataProvider('provideEsiShortcodes')]
#[Test]
public function uses_esi_fragments(string $shortcodeName): void
{
$request = new Request([], [], [], [], [], ['SCRIPT_URL' => '/', 'HTTP_HOST' => 'localhost']);
$request->headers->set('Surrogate-Capability', 'ESI/1.0');
$result = $this->renderTwig('{{ content | shortcodes }}', ['content' => "[$shortcodeName foo=bar]"], $request);
self::assertStringContainsString('<esi:include ', $result);
}
public static function provideEsiShortcodes(): Generator
{
yield 'ESI-based shortcode defined in bundle configuration' => ['test-config-esi'];
yield 'ESI-based shortcode defined in service configuration' => ['test-service-esi'];
}
private function renderTwig(string $templateCode, array $context = [], ?Request $request = null): string
{
self::bootKernel();
$container = static::getContainer();
$requestStack = $container->get(RequestStack::class);
$requestStack->push($request ?? new Request());
$twig = $container->get('twig');
$template = $twig->createTemplate($templateCode);
return $twig->render($template, $context);
}
}