|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactory\ShortcodeBundle\Tests\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Fragment\FragmentHandler; |
|
9
|
|
|
|
|
10
|
|
|
final class ShortcodeFacadeTest extends KernelTestCase |
|
11
|
|
|
{ |
|
12
|
|
|
private $fragmentHandler; |
|
13
|
|
|
|
|
14
|
|
|
protected static function getKernelClass(): string |
|
15
|
|
|
{ |
|
16
|
|
|
return 'Webfactory\ShortcodeBundle\Tests\Fixtures\TestKernel'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function setUp(): void |
|
20
|
|
|
{ |
|
21
|
|
|
parent::setUp(); |
|
22
|
|
|
static::bootKernel(); |
|
23
|
|
|
|
|
24
|
|
|
// Replace fragment renderer in test kernel, as we only want to assert it is called with the correct parameters, |
|
25
|
|
|
// but not it's result. |
|
26
|
|
|
$container = static::$kernel->getContainer(); |
|
27
|
|
|
$container->get('request_stack')->push(Request::create('/')); |
|
28
|
|
|
$this->fragmentHandler = $this->getMockBuilder(FragmentHandler::class)->disableOriginalConstructor()->getMock(); |
|
29
|
|
|
$container->set('fragment.handler', $this->fragmentHandler); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @test |
|
34
|
|
|
* @dataProvider shortcodeFixtures |
|
35
|
|
|
*/ |
|
36
|
|
|
public function processes_shortcodes( |
|
37
|
|
|
string $shortcode, |
|
38
|
|
|
ControllerReference $expectedControllerReference, |
|
39
|
|
|
string $expectedRenderStrategy |
|
40
|
|
|
): void { |
|
41
|
|
|
$this->fragmentHandler->expects($this->once()) |
|
42
|
|
|
->method('render') |
|
43
|
|
|
->willReturnCallback( |
|
44
|
|
|
function ( |
|
45
|
|
|
ControllerReference $actualControllerReference, |
|
46
|
|
|
string $actualRenderStrategy |
|
47
|
|
|
) use ( |
|
48
|
|
|
$expectedControllerReference, |
|
49
|
|
|
$expectedRenderStrategy |
|
50
|
|
|
) { |
|
51
|
|
|
return $actualControllerReference->controller === $expectedControllerReference->controller && $actualRenderStrategy === $expectedRenderStrategy |
|
52
|
|
|
? 'OK' |
|
53
|
|
|
: 'unexpected parameter values'; |
|
54
|
|
|
} |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertEquals( |
|
58
|
|
|
'OK', |
|
59
|
|
|
$this->renderTwigTemplate('{{ content | shortcodes }}', ['content' => $shortcode]) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function shortcodeFixtures(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'esi rendering' => ['[test-esi foo=bar]', new ControllerReference('test-esi-controller', ['foo' => 'bar']), 'esi'], |
|
67
|
|
|
'inline rendering' => ['[test-inline bar=baz]', new ControllerReference('test-inline-controller', ['bar' => 'baz']), 'inline'], |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @test */ |
|
72
|
|
|
public function paragraphs_wrapping_shortcodes_get_removed(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->fragmentHandler->method('render')->willReturn('RESULT'); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals( |
|
77
|
|
|
'RESULT', |
|
78
|
|
|
$this->renderTwigTemplate("{{ '<p>[test-inline]</p>' | shortcodes }}") |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @test */ |
|
83
|
|
|
public function content_without_shortcodes_wont_be_changed(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->assertEquals( |
|
86
|
|
|
'<p>Content without shortcode</p>', |
|
87
|
|
|
$this->renderTwigTemplate("{{ '<p>Content without shortcode</p>' | shortcodes }}") |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function renderTwigTemplate(string $templateCode, array $context = []): string |
|
92
|
|
|
{ |
|
93
|
|
|
/** @var $container ContainerInterface */ |
|
94
|
|
|
$container = static::$kernel->getContainer(); |
|
95
|
|
|
|
|
96
|
|
|
/** @var \Twig_Environment $twig */ |
|
97
|
|
|
$twig = $container->get('twig'); |
|
98
|
|
|
$template = $twig->createTemplate($templateCode); |
|
99
|
|
|
|
|
100
|
|
|
return $template->render($context); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|