|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactory\ShortcodeBundle\Tests\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
6
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Fragment\FragmentHandler; |
|
8
|
|
|
|
|
9
|
|
|
final class ShortcodeFacadeTest extends KernelTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array [ |
|
13
|
|
|
* [ |
|
14
|
|
|
* 'controller' => 'app.controller.myFilter:myfilterPartialAction', |
|
15
|
|
|
* 'renderer' => 'esi', |
|
16
|
|
|
* 'name' => 'myShortcode', |
|
17
|
|
|
* ] |
|
18
|
|
|
* ] |
|
19
|
|
|
*/ |
|
20
|
|
|
static protected $shortcodesToRegister = []; |
|
21
|
|
|
|
|
22
|
|
|
protected static function createKernel(array $options = array()) |
|
23
|
|
|
{ |
|
24
|
|
|
return new TestKernel('test', true, static::$shortcodesToRegister); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** @test */ |
|
28
|
|
View Code Duplication |
public function shortcode_leads_to_rendering_of_controller_reference() |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
static::$shortcodesToRegister = [ |
|
31
|
|
|
[ |
|
32
|
|
|
'controller' => 'app.controller.myFilter:myfilterPartialAction', |
|
33
|
|
|
'renderer' => 'esi', |
|
34
|
|
|
'name' => 'myShortcode', |
|
35
|
|
|
] |
|
36
|
|
|
]; |
|
37
|
|
|
static::bootKernel(); |
|
38
|
|
|
|
|
39
|
|
|
// Replace fragment renderer in test kernel, as we only want to assert it is called with the correct parameters, |
|
40
|
|
|
// but not it's result. |
|
41
|
|
|
$container = static::$kernel->getContainer(); |
|
42
|
|
|
$fragmentHandler = $this->getMockBuilder(FragmentHandler::class)->disableOriginalConstructor()->getMock(); |
|
43
|
|
|
$fragmentHandler->expects($this->once()) |
|
44
|
|
|
->method('render') |
|
45
|
|
|
->with(new ControllerReference('app.controller.myFilter:myfilterPartialAction', ['id' => 42]), 'esi') |
|
46
|
|
|
->willReturn($mockedRenderResult = 'OK'); |
|
47
|
|
|
$container->set('fragment.handler', $fragmentHandler); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals( |
|
50
|
|
|
$mockedRenderResult, |
|
51
|
|
|
$this->renderTwigTemplate('{{ content |shortcodes }}', ['content' => '[myShortcode id=42]']) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @test */ |
|
56
|
|
View Code Duplication |
public function paragraphs_wrapping_shortcodes_get_removed() |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
static::$shortcodesToRegister = [ |
|
59
|
|
|
[ |
|
60
|
|
|
'controller' => 'app.controller.myFilter:myfilterPartialAction', |
|
61
|
|
|
'renderer' => 'esi', |
|
62
|
|
|
'name' => 'myShortcode', |
|
63
|
|
|
] |
|
64
|
|
|
]; |
|
65
|
|
|
static::bootKernel(); |
|
66
|
|
|
|
|
67
|
|
|
// Replace fragment renderer in test kernel, as we only want to assert it is called with the correct parameters, |
|
68
|
|
|
// but not it's result. |
|
69
|
|
|
$container = static::$kernel->getContainer(); |
|
70
|
|
|
$fragmentHandler = $this->getMockBuilder(FragmentHandler::class)->disableOriginalConstructor()->getMock(); |
|
71
|
|
|
$fragmentHandler->expects($this->once()) |
|
72
|
|
|
->method('render') |
|
73
|
|
|
->with(new ControllerReference('app.controller.myFilter:myfilterPartialAction', ['id' => 42]), 'esi') |
|
74
|
|
|
->willReturn($mockedRenderResult = 'OK'); |
|
75
|
|
|
$container->set('fragment.handler', $fragmentHandler); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals( |
|
78
|
|
|
$mockedRenderResult, |
|
79
|
|
|
$this->renderTwigTemplate('{{ content |shortcodes }}', ['content' => '<p> [myShortcode id=42] </p>']) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @test */ |
|
84
|
|
|
public function content_without_shortcodes_wont_be_changed() |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
static::bootKernel(); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertEquals( |
|
89
|
|
|
'<p>Content without shortcode</p>', |
|
90
|
|
|
$this->renderTwigTemplate('{{ \'<p>Content without shortcode</p>\' | shortcodes }}', []) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $templateCode |
|
96
|
|
|
* @param array $context |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function renderTwigTemplate($templateCode, array $context) |
|
100
|
|
|
{ |
|
101
|
|
|
/** @var $container ContainerInterface */ |
|
102
|
|
|
$container = static::$kernel->getContainer(); |
|
103
|
|
|
|
|
104
|
|
|
/** @var \Twig_Environment $twig */ |
|
105
|
|
|
$twig = $container->get('twig'); |
|
106
|
|
|
$template = $twig->createTemplate($templateCode); |
|
107
|
|
|
|
|
108
|
|
|
return $template->render($context); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.