|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2017 WEBEWEB |
|
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 WBW\Bundle\CoreBundle\Tests\Twig\Extension\Asset; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
|
16
|
|
|
use Twig\Node\Node; |
|
17
|
|
|
use Twig\TwigFilter; |
|
18
|
|
|
use Twig\TwigFunction; |
|
19
|
|
|
use WBW\Bundle\CoreBundle\Asset\SyntaxHighlighterConfig; |
|
20
|
|
|
use WBW\Bundle\CoreBundle\Asset\SyntaxHighlighterDefaults; |
|
21
|
|
|
use WBW\Bundle\CoreBundle\Asset\SyntaxHighlighterStrings; |
|
22
|
|
|
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase; |
|
23
|
|
|
use WBW\Bundle\CoreBundle\Twig\Extension\Asset\SyntaxHighlighterTwigExtension; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SyntaxHighlighter Twig extension test. |
|
27
|
|
|
* |
|
28
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
29
|
|
|
* @package WBW\Bundle\CoreBundle\Tests\Twig\Extension\Asset |
|
30
|
|
|
*/ |
|
31
|
|
|
class SyntaxHighlighterTwigExtensionTest extends AbstractTestCase { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* SyntaxHighlighter config. |
|
35
|
|
|
* |
|
36
|
|
|
* @var SyntaxHighlighterConfig |
|
37
|
|
|
*/ |
|
38
|
|
|
private $syntaxHighlighterConfig; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* SyntaxHighlighter defaults. |
|
42
|
|
|
* |
|
43
|
|
|
* @var SyntaxHighlighterDefaults |
|
44
|
|
|
*/ |
|
45
|
|
|
private $syntaxHighlighterDefaults; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* SyntaxHighlighter strings. |
|
49
|
|
|
* |
|
50
|
|
|
* @var SyntaxHighlighterStrings |
|
51
|
|
|
*/ |
|
52
|
|
|
private $syntaxHighlighterStrings; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritDoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function setUp(): void { |
|
58
|
|
|
parent::setUp(); |
|
59
|
|
|
|
|
60
|
|
|
// Set a SyntaxHighlighter config mock. |
|
61
|
|
|
$this->syntaxHighlighterConfig = new SyntaxHighlighterConfig(); |
|
62
|
|
|
|
|
63
|
|
|
// Set a SyntaxHighlighter defauls mock. |
|
64
|
|
|
$this->syntaxHighlighterDefaults = new SyntaxHighlighterDefaults(); |
|
65
|
|
|
|
|
66
|
|
|
// Set a SyntaxHighlighter strings mock. |
|
67
|
|
|
$this->syntaxHighlighterStrings = new SyntaxHighlighterStrings(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Tests the getFilters() method. |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testGetFilters(): void { |
|
76
|
|
|
|
|
77
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
78
|
|
|
|
|
79
|
|
|
$res = $obj->getFilters(); |
|
80
|
|
|
$this->assertCount(2, $res); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$this->assertInstanceOf(TwigFilter::class, $res[0]); |
|
83
|
|
|
$this->assertEquals("syntaxHighlighterScript", $res[0]->getName()); |
|
84
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterScriptFilter"], $res[0]->getCallable()); |
|
85
|
|
|
$this->assertEquals(["html"], $res[0]->getSafe(new Node())); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertInstanceOf(TwigFilter::class, $res[1]); |
|
88
|
|
|
$this->assertEquals("shScript", $res[1]->getName()); |
|
89
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterScriptFilter"], $res[1]->getCallable()); |
|
90
|
|
|
$this->assertEquals(["html"], $res[1]->getSafe(new Node())); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Tests the getFunctions() method. |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testGetFunctions(): void { |
|
99
|
|
|
|
|
100
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
101
|
|
|
|
|
102
|
|
|
$res = $obj->getFunctions(); |
|
103
|
|
|
$this->assertCount(10, $res); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[0]); |
|
106
|
|
|
$this->assertEquals("syntaxHighlighterConfig", $res[0]->getName()); |
|
107
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterConfigFunction"], $res[0]->getCallable()); |
|
108
|
|
|
$this->assertEquals(["html"], $res[0]->getSafe(new Node())); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[1]); |
|
111
|
|
|
$this->assertEquals("shConfig", $res[1]->getName()); |
|
112
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterConfigFunction"], $res[1]->getCallable()); |
|
113
|
|
|
$this->assertEquals(["html"], $res[1]->getSafe(new Node())); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[2]); |
|
116
|
|
|
$this->assertEquals("syntaxHighlighterContent", $res[2]->getName()); |
|
117
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterContentFunction"], $res[2]->getCallable()); |
|
118
|
|
|
$this->assertEquals(["html"], $res[2]->getSafe(new Node())); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[3]); |
|
121
|
|
|
$this->assertEquals("shContent", $res[3]->getName()); |
|
122
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterContentFunction"], $res[3]->getCallable()); |
|
123
|
|
|
$this->assertEquals(["html"], $res[3]->getSafe(new Node())); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[4]); |
|
126
|
|
|
$this->assertEquals("syntaxHighlighterDefaults", $res[4]->getName()); |
|
127
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterDefaultsFunction"], $res[4]->getCallable()); |
|
128
|
|
|
$this->assertEquals(["html"], $res[4]->getSafe(new Node())); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[5]); |
|
131
|
|
|
$this->assertEquals("shDefaults", $res[5]->getName()); |
|
132
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterDefaultsFunction"], $res[5]->getCallable()); |
|
133
|
|
|
$this->assertEquals(["html"], $res[5]->getSafe(new Node())); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[6]); |
|
136
|
|
|
$this->assertEquals("syntaxHighlighterScript", $res[6]->getName()); |
|
137
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterScriptFilter"], $res[6]->getCallable()); |
|
138
|
|
|
$this->assertEquals(["html"], $res[6]->getSafe(new Node())); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[7]); |
|
141
|
|
|
$this->assertEquals("shScript", $res[7]->getName()); |
|
142
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterScriptFilter"], $res[7]->getCallable()); |
|
143
|
|
|
$this->assertEquals(["html"], $res[7]->getSafe(new Node())); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[8]); |
|
146
|
|
|
$this->assertEquals("syntaxHighlighterStrings", $res[8]->getName()); |
|
147
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterStringsFunction"], $res[8]->getCallable()); |
|
148
|
|
|
$this->assertEquals(["html"], $res[8]->getSafe(new Node())); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertInstanceOf(TwigFunction::class, $res[9]); |
|
151
|
|
|
$this->assertEquals("shStrings", $res[9]->getName()); |
|
152
|
|
|
$this->assertEquals([$obj, "syntaxHighlighterStringsFunction"], $res[9]->getCallable()); |
|
153
|
|
|
$this->assertEquals(["html"], $res[9]->getSafe(new Node())); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Tests the syntaxHighlighterConfigFunction() method. |
|
158
|
|
|
* |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
|
|
public function testSyntaxHighlighterConfigFunction(): void { |
|
162
|
|
|
|
|
163
|
|
|
// Set the SyntaxHighlighter config mock. |
|
164
|
|
|
$this->syntaxHighlighterConfig->setBloggerMode(true); |
|
165
|
|
|
$this->syntaxHighlighterConfig->setStripBrs(true); |
|
|
|
|
|
|
166
|
|
|
$this->syntaxHighlighterConfig->setTagName("blockquote"); |
|
167
|
|
|
|
|
168
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
169
|
|
|
|
|
170
|
|
|
$res = file_get_contents(__DIR__ . "/testSyntaxHighlighterConfigFunction.html.txt"); |
|
171
|
|
|
$this->assertEquals($res, $obj->syntaxHighlighterConfigFunction($this->syntaxHighlighterConfig) . "\n"); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Tests the syntaxHighlighterConfigFunction() method. |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testSyntaxHighlighterConfigFunctionWithStrings(): void { |
|
180
|
|
|
|
|
181
|
|
|
// Set the SyntaxHighlighter config mock. |
|
182
|
|
|
$this->syntaxHighlighterConfig->setBloggerMode(true); |
|
183
|
|
|
$this->syntaxHighlighterConfig->setStripBrs(true); |
|
|
|
|
|
|
184
|
|
|
$this->syntaxHighlighterConfig->setTagName("blockquote"); |
|
185
|
|
|
|
|
186
|
|
|
$this->syntaxHighlighterConfig->setStrings($this->syntaxHighlighterStrings); |
|
187
|
|
|
|
|
188
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
189
|
|
|
|
|
190
|
|
|
$res = file_get_contents(__DIR__ . "/testSyntaxHighlighterConfigFunctionWithStrings.html.txt"); |
|
191
|
|
|
$this->assertEquals($res, $obj->syntaxHighlighterConfigFunction($this->syntaxHighlighterConfig) . "\n"); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Tests the syntaxHighlighterContentFunction() method. |
|
196
|
|
|
* |
|
197
|
|
|
* @return void |
|
198
|
|
|
* @throws Exception Throws an exception if an error occurs. |
|
199
|
|
|
*/ |
|
200
|
|
|
public function testSyntaxHighlighterContentFunction(): void { |
|
201
|
|
|
|
|
202
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
203
|
|
|
|
|
204
|
|
|
$arg = ["content" => "<span>span</span>", "language" => "html"]; |
|
205
|
|
|
$res = file_get_contents(__DIR__ . "/testSyntaxHighlighterContentFunction.html.txt"); |
|
206
|
|
|
$this->assertEquals($res, $obj->syntaxHighlighterContentFunction($arg) . "\n"); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Tests the syntaxHighlighterContentFunction() method. |
|
211
|
|
|
* |
|
212
|
|
|
* @return void |
|
213
|
|
|
*/ |
|
214
|
|
|
public function testSyntaxHighlighterContentFunctionWithFileNotFoundException(): void { |
|
215
|
|
|
|
|
216
|
|
|
// Set a Filename mock. |
|
217
|
|
|
$filename = getcwd() . "/Tests/Twig/Extension/Asset/SyntaxHighlighterTwigExtensionTest"; |
|
218
|
|
|
|
|
219
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
220
|
|
|
|
|
221
|
|
|
$arg = ["filename" => $filename]; |
|
222
|
|
|
|
|
223
|
|
|
try { |
|
224
|
|
|
|
|
225
|
|
|
$obj->syntaxHighlighterContentFunction($arg); |
|
226
|
|
|
} catch (Exception $ex) { |
|
227
|
|
|
|
|
228
|
|
|
$this->assertInstanceOf(FileNotFoundException::class, $ex); |
|
229
|
|
|
$this->assertStringContainsString('/Tests/Twig/Extension/Asset/SyntaxHighlighterTwigExtensionTest" could not be found', $ex->getMessage()); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Tests the syntaxHighlighterContentFunction() method. |
|
235
|
|
|
* |
|
236
|
|
|
* @return void |
|
237
|
|
|
* @throws Exception Throws an exception if an error occurs. |
|
238
|
|
|
*/ |
|
239
|
|
|
public function testSyntaxHighlighterContentFunctionWithFilename(): void { |
|
240
|
|
|
|
|
241
|
|
|
// Set a Filename mock. |
|
242
|
|
|
$filename = getcwd() . "/Tests/Twig/Extension/Asset/SyntaxHighlighterTwigExtensionTest.txt"; |
|
243
|
|
|
|
|
244
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
245
|
|
|
|
|
246
|
|
|
$arg = ["filename" => $filename, "language" => "php"]; |
|
247
|
|
|
$res = file_get_contents(__DIR__ . "/testSyntaxHighlighterContentFunctionWithFilename.html.txt"); |
|
248
|
|
|
$this->assertEquals($res, $obj->syntaxHighlighterContentFunction($arg) . "\n"); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Tests the syntaxHighlighterDefaultsFunction() method. |
|
253
|
|
|
* |
|
254
|
|
|
* @return void |
|
255
|
|
|
*/ |
|
256
|
|
|
public function testSyntaxHighlighterDefaultsFunction(): void { |
|
257
|
|
|
|
|
258
|
|
|
// Set the SyntaxHighlighter defaults mock. |
|
259
|
|
|
$this->syntaxHighlighterDefaults->setAutoLinks(false); |
|
260
|
|
|
$this->syntaxHighlighterDefaults->setClassName("classname"); |
|
261
|
|
|
$this->syntaxHighlighterDefaults->setCollapse(true); |
|
262
|
|
|
$this->syntaxHighlighterDefaults->setFirstLine(0); |
|
263
|
|
|
$this->syntaxHighlighterDefaults->setGutter(false); |
|
264
|
|
|
$this->syntaxHighlighterDefaults->setHighlight([1, 2, 3]); |
|
265
|
|
|
$this->syntaxHighlighterDefaults->setHtmlScript(true); |
|
266
|
|
|
$this->syntaxHighlighterDefaults->setSmartTabs(false); |
|
267
|
|
|
$this->syntaxHighlighterDefaults->setTabSize(8); |
|
268
|
|
|
$this->syntaxHighlighterDefaults->setToolbar(false); |
|
269
|
|
|
|
|
270
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
271
|
|
|
|
|
272
|
|
|
$res = file_get_contents(__DIR__ . "/testSyntaxHighlighterDefaultsFunction.html.txt"); |
|
273
|
|
|
$this->assertEquals($res, $obj->syntaxHighlighterDefaultsFunction($this->syntaxHighlighterDefaults) . "\n"); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Tests the syntaxHighlighterScriptFilter() method. |
|
278
|
|
|
* |
|
279
|
|
|
* @return void |
|
280
|
|
|
*/ |
|
281
|
|
|
public function testSyntaxHighlighterScriptFilter(): void { |
|
282
|
|
|
|
|
283
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
284
|
|
|
|
|
285
|
|
|
$res = "<script type=\"text/javascript\">\ncontent\n</script>"; |
|
286
|
|
|
$this->assertEquals($res, $obj->syntaxHighlighterScriptFilter("content")); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Tests the syntaxHighlighterStringsFunction() method. |
|
291
|
|
|
* |
|
292
|
|
|
* @return void |
|
293
|
|
|
*/ |
|
294
|
|
|
public function testSyntaxHighlighterStringsFunction(): void { |
|
295
|
|
|
|
|
296
|
|
|
// Set the SyntaxHighlighter strings mock. |
|
297
|
|
|
$this->syntaxHighlighterStrings->setAlert("SyntaxHighlighter bundle"); |
|
298
|
|
|
$this->syntaxHighlighterStrings->setBrushNotHtmlScript("Brush wasn't made for HTML-Script option :"); |
|
299
|
|
|
$this->syntaxHighlighterStrings->setCopyToClipboard("Copy to clipboard"); |
|
300
|
|
|
$this->syntaxHighlighterStrings->setCopyToClipboardConfirmation("Operation success"); |
|
301
|
|
|
$this->syntaxHighlighterStrings->setExpandSource("Expand source"); |
|
302
|
|
|
$this->syntaxHighlighterStrings->setHelp("Help"); |
|
303
|
|
|
$this->syntaxHighlighterStrings->setNoBrush("Can't find brush for :"); |
|
304
|
|
|
$this->syntaxHighlighterStrings->setPrint("Print"); |
|
305
|
|
|
$this->syntaxHighlighterStrings->setViewSource("View source"); |
|
306
|
|
|
|
|
307
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
308
|
|
|
|
|
309
|
|
|
$res = file_get_contents(__DIR__ . "/testSyntaxHighlighterStringsFunction.html.txt"); |
|
310
|
|
|
$this->assertEquals($res, $obj->syntaxHighlighterStringsFunction($this->syntaxHighlighterStrings) . "\n"); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Tests the __construct() method. |
|
315
|
|
|
* |
|
316
|
|
|
* @return void |
|
317
|
|
|
*/ |
|
318
|
|
|
public function test__construct(): void { |
|
319
|
|
|
|
|
320
|
|
|
$this->assertEquals("wbw.core.twig.extension.asset.syntax_highlighter", SyntaxHighlighterTwigExtension::SERVICE_NAME); |
|
321
|
|
|
|
|
322
|
|
|
$obj = new SyntaxHighlighterTwigExtension($this->twigEnvironment); |
|
323
|
|
|
|
|
324
|
|
|
$this->assertSame($this->twigEnvironment, $obj->getTwigEnvironment()); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: