Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
29 | class CKEditorRendererTest extends AbstractTestCase |
||
30 | { |
||
31 | /** |
||
32 | * @var CKEditorRenderer |
||
33 | */ |
||
34 | private $renderer; |
||
35 | |||
36 | /** |
||
37 | * @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject |
||
38 | */ |
||
39 | private $container; |
||
40 | |||
41 | /** |
||
42 | * @var Packages|\PHPUnit_Framework_MockObject_MockObject |
||
43 | */ |
||
44 | private $packages; |
||
45 | |||
46 | /** |
||
47 | * @var Request|\PHPUnit_Framework_MockObject_MockObject |
||
48 | */ |
||
49 | private $request; |
||
50 | |||
51 | /** |
||
52 | * @var RequestStack|\PHPUnit_Framework_MockObject_MockObject |
||
53 | */ |
||
54 | private $requestStack; |
||
55 | |||
56 | /** |
||
57 | * @var RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
||
58 | */ |
||
59 | private $router; |
||
60 | |||
61 | /** |
||
62 | * @var EngineInterface|\PHPUnit_Framework_MockObject_MockObject |
||
63 | */ |
||
64 | private $templating; |
||
65 | |||
66 | /** |
||
67 | * @var \Twig\Environment|\PHPUnit_Framework_MockObject_MockObject |
||
68 | */ |
||
69 | private $twig; |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | protected function setUp() |
||
75 | { |
||
76 | $this->request = $this->createMock(Request::class); |
||
77 | $this->router = $this->createMock(RouterInterface::class); |
||
78 | $this->packages = $this->getMockBuilder(Packages::class) |
||
79 | ->disableOriginalConstructor() |
||
80 | ->getMock(); |
||
81 | |||
82 | $this->requestStack = $this->createMock(RequestStack::class); |
||
83 | $this->requestStack->expects($this->any())->method('getCurrentRequest')->will($this->returnValue($this->request)); |
||
84 | $this->templating = $this->createMock(EngineInterface::class); |
||
85 | $this->twig = $this->getMockBuilder(\Twig_Environment::class) |
||
86 | ->disableOriginalConstructor() |
||
87 | ->getMock(); |
||
88 | |||
89 | $this->renderer = new CKEditorRenderer(new JsonBuilder(), $this->router, $this->packages, $this->requestStack, $this->templating); |
||
90 | } |
||
91 | |||
92 | public function testDefaultState() |
||
93 | { |
||
94 | $this->assertInstanceOf(CKEditorRendererInterface::class, $this->renderer); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @group legacy |
||
99 | * @expectedDeprecation Passing a %s as %s first argument is deprecated since %s, and will be removed in %s. Use %s instead. |
||
100 | */ |
||
101 | public function testLegacyContstructor() |
||
102 | { |
||
103 | $container = $this->createMock(ContainerInterface::class); |
||
104 | $container->expects($this->exactly(5)) |
||
105 | ->method('get') |
||
106 | ->withConsecutive( |
||
107 | ['fos_ck_editor.renderer.json_builder'], |
||
108 | ['router'], |
||
109 | ['assets.packages'], |
||
110 | ['request_stack'], |
||
111 | ['templating'] |
||
112 | ) |
||
113 | ->willReturnMap([ |
||
114 | ['fos_ck_editor.renderer.json_builder', new JsonBuilder()], |
||
115 | ['router', $this->router], |
||
116 | ['assets.packages', $this->packages], |
||
117 | ['request_stack', $this->requestStack], |
||
118 | ['templating', $this->templating], |
||
119 | ]); |
||
120 | |||
121 | new CKEditorRenderer($container); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $path |
||
126 | * @param string $asset |
||
127 | * @param string $url |
||
128 | * |
||
129 | * @dataProvider directoryAssetProvider |
||
130 | */ |
||
131 | public function testRenderBasePath($path, $asset, $url) |
||
132 | { |
||
133 | $this->packages |
||
134 | ->expects($this->once()) |
||
135 | ->method('getUrl') |
||
136 | ->with($this->equalTo($path)) |
||
137 | ->will($this->returnValue($asset)); |
||
138 | |||
139 | $this->assertSame($url, $this->renderer->renderBasePath($path)); |
||
140 | } |
||
141 | |||
142 | public function testRenderJsPath() |
||
143 | { |
||
144 | $this->packages |
||
145 | ->expects($this->once()) |
||
146 | ->method('getUrl') |
||
147 | ->with($this->equalTo('foo')) |
||
148 | ->will($this->returnValue('bar')); |
||
149 | |||
150 | $this->assertSame('bar', $this->renderer->renderJsPath('foo')); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param string $symfonyLocale |
||
155 | * @param string $ckEditorLocale |
||
156 | * |
||
157 | * @dataProvider languageProvider |
||
158 | */ |
||
159 | public function testRenderWidgetWithLocaleRequest($symfonyLocale, $ckEditorLocale) |
||
160 | { |
||
161 | $this->request |
||
162 | ->expects($this->once()) |
||
163 | ->method('getLocale') |
||
164 | ->will($this->returnValue($symfonyLocale)); |
||
165 | |||
166 | $this->assertSame( |
||
167 | 'CKEDITOR.replace("foo", {"language":"'.$ckEditorLocale.'"});', |
||
168 | $this->renderer->renderWidget('foo', []) |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param string $symfonyLocale |
||
174 | * @param string $ckEditorLocale |
||
175 | * |
||
176 | * @dataProvider languageProvider |
||
177 | */ |
||
178 | public function testRenderWidgetWithLocaleParameter($symfonyLocale, $ckEditorLocale) |
||
179 | { |
||
180 | $this->request->expects($this->once())->method('getLocale')->will($this->returnValue($symfonyLocale)); |
||
181 | $this->assertSame( |
||
182 | 'CKEDITOR.replace("foo", {"language":"'.$ckEditorLocale.'"});', |
||
183 | $this->renderer->renderWidget('foo', []) |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param string $symfonyLocale |
||
189 | * @param string $ckEditorLocale |
||
190 | * |
||
191 | * @dataProvider languageProvider |
||
192 | */ |
||
193 | public function testRenderWidgetWithExplicitLanguage($symfonyLocale, $ckEditorLocale) |
||
194 | { |
||
195 | $this->assertSame( |
||
196 | 'CKEDITOR.replace("foo", {"language":"'.$ckEditorLocale.'"});', |
||
197 | $this->renderer->renderWidget('foo', ['language' => $symfonyLocale]) |
||
198 | ); |
||
199 | } |
||
200 | |||
201 | public function testRenderWidgetWithoutLocale() |
||
202 | { |
||
203 | $this->request |
||
204 | ->expects($this->once()) |
||
205 | ->method('getLocale') |
||
206 | ->will($this->returnValue(null)); |
||
207 | |||
208 | $this->assertSame( |
||
209 | 'CKEDITOR.replace("foo", []);', |
||
210 | $this->renderer->renderWidget('foo', []) |
||
211 | ); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param string $path |
||
216 | * @param string $asset |
||
217 | * @param string $url |
||
218 | * |
||
219 | * @dataProvider fileAssetProvider |
||
220 | */ |
||
221 | public function testRenderWidgetWithStringContentsCss($path, $asset, $url) |
||
222 | { |
||
223 | $this->packages |
||
224 | ->expects($this->once()) |
||
225 | ->method('getUrl') |
||
226 | ->with($this->equalTo($path)) |
||
227 | ->will($this->returnValue($asset)); |
||
228 | |||
229 | $this->assertSame( |
||
230 | 'CKEDITOR.replace("foo", {"contentsCss":["'.$url.'"]});', |
||
231 | $this->renderer->renderWidget('foo', ['contentsCss' => $path]) |
||
232 | ); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param string[] $paths |
||
237 | * @param string[] $assets |
||
238 | * @param string[] $urls |
||
239 | * |
||
240 | * @dataProvider filesAssetProvider |
||
241 | */ |
||
242 | public function testRenderWidgetWithArrayContentsCss(array $paths, array $assets, array $urls) |
||
243 | { |
||
244 | foreach (array_keys($paths) as $key) { |
||
245 | $this->packages |
||
246 | ->expects($this->at($key)) |
||
247 | ->method('getUrl') |
||
248 | ->with($this->equalTo($paths[$key])) |
||
249 | ->will($this->returnValue($assets[$key])); |
||
250 | } |
||
251 | |||
252 | $this->assertSame( |
||
253 | 'CKEDITOR.replace("foo", {"contentsCss":'.json_encode($urls).'});', |
||
254 | $this->renderer->renderWidget('foo', ['contentsCss' => $paths]) |
||
255 | ); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param string $filebrowser |
||
260 | * |
||
261 | * @dataProvider filebrowserProvider |
||
262 | */ |
||
263 | public function testRenderWidgetWithFileBrowser($filebrowser) |
||
264 | { |
||
265 | $this->assertSame( |
||
266 | 'CKEDITOR.replace("foo", {"filebrowser'.$filebrowser.'Url":"'.($url = 'browse_url').'"});', |
||
267 | $this->renderer->renderWidget('foo', ['filebrowser'.$filebrowser.'Url' => $url]) |
||
268 | ); |
||
269 | } |
||
270 | |||
271 | public function testRenderWidgetWithCustomFileBrowser() |
||
272 | { |
||
273 | $this->assertSame( |
||
274 | 'CKEDITOR.replace("foo", {"filebrowser'.($filebrowser = 'VideoBrowse').'Url":"'.($url = 'browse_url').'"});', |
||
275 | $this->renderer->renderWidget( |
||
276 | 'foo', |
||
277 | ['filebrowser'.$filebrowser.'Url' => $url], |
||
278 | ['filebrowsers' => [$filebrowser]] |
||
279 | ) |
||
280 | ); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $filebrowser |
||
285 | * |
||
286 | * @dataProvider filebrowserProvider |
||
287 | */ |
||
288 | public function testRenderWidgetWithMinimalRouteFileBrowser($filebrowser) |
||
289 | { |
||
290 | $this->router |
||
291 | ->expects($this->once()) |
||
292 | ->method('generate') |
||
293 | ->with( |
||
294 | $this->identicalTo($route = 'browse_route'), |
||
295 | $this->identicalTo([]), |
||
296 | $this->identicalTo(UrlGeneratorInterface::ABSOLUTE_PATH) |
||
297 | ) |
||
298 | ->will($this->returnValue('browse_url')); |
||
299 | |||
300 | $this->assertSame( |
||
301 | 'CKEDITOR.replace("foo", {"filebrowser'.$filebrowser.'Url":"browse_url"});', |
||
302 | $this->renderer->renderWidget('foo', ['filebrowser'.$filebrowser.'Route' => $route]) |
||
303 | ); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @param string $filebrowser |
||
308 | * |
||
309 | * @dataProvider filebrowserProvider |
||
310 | */ |
||
311 | public function testRenderWidgetWithMaximalRouteFileBrowser($filebrowser) |
||
312 | { |
||
313 | $this->router |
||
314 | ->expects($this->once()) |
||
315 | ->method('generate') |
||
316 | ->with( |
||
317 | $this->identicalTo($route = 'browse_route'), |
||
318 | $this->identicalTo($routeParameters = ['foo' => 'bar']), |
||
319 | $this->identicalTo($routeType = UrlGeneratorInterface::ABSOLUTE_URL) |
||
320 | ) |
||
321 | ->will($this->returnValue('browse_url')); |
||
322 | |||
323 | $this->assertSame( |
||
324 | 'CKEDITOR.replace("foo", {"filebrowser'.$filebrowser.'Url":"browse_url"});', |
||
325 | $this->renderer->renderWidget('foo', [ |
||
326 | 'filebrowser'.$filebrowser.'Route' => $route, |
||
327 | 'filebrowser'.$filebrowser.'RouteParameters' => $routeParameters, |
||
328 | 'filebrowser'.$filebrowser.'RouteType' => $routeType, |
||
329 | ]) |
||
330 | ); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @param string $filebrowser |
||
335 | * |
||
336 | * @dataProvider filebrowserProvider |
||
337 | */ |
||
338 | public function testRenderWidgetWithRouteFileBrowserHandler($filebrowser) |
||
339 | { |
||
340 | $this->router |
||
341 | ->expects($this->once()) |
||
342 | ->method('generate') |
||
343 | ->with( |
||
344 | $this->equalTo('browse_route'), |
||
345 | $this->equalTo(['foo' => 'bar']), |
||
346 | $this->equalTo(UrlGeneratorInterface::ABSOLUTE_URL) |
||
347 | ) |
||
348 | ->will($this->returnValue('browse_url')); |
||
349 | |||
350 | $this->assertSame( |
||
351 | 'CKEDITOR.replace("foo", {"filebrowser'.$filebrowser.'Url":"browse_url"});', |
||
352 | $this->renderer->renderWidget('foo', [ |
||
353 | 'filebrowser'.$filebrowser.'Handler' => function (RouterInterface $router) { |
||
354 | return $router->generate('browse_route', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL); |
||
355 | }, |
||
356 | ]) |
||
357 | ); |
||
358 | } |
||
359 | |||
360 | public function testRenderWidgetWithProtectedSource() |
||
361 | { |
||
362 | $this->assertSame( |
||
363 | 'CKEDITOR.replace("foo", {"protectedSource":[/<\?[\s\S]*?\?>/g,/<%[\s\S]*?%>/g]});', |
||
364 | $this->renderer->renderWidget('foo', [ |
||
365 | 'protectedSource' => [ |
||
366 | '/<\?[\s\S]*?\?>/g', |
||
367 | '/<%[\s\S]*?%>/g', |
||
368 | ], |
||
369 | ]) |
||
370 | ); |
||
371 | } |
||
372 | |||
373 | public function testRenderWidgetWithStylesheetParserSkipSelectors() |
||
374 | { |
||
375 | $this->assertSame( |
||
376 | 'CKEDITOR.replace("foo", {"stylesheetParser_skipSelectors":/(^body\.|^caption\.|\.high|^\.)/i});', |
||
377 | $this->renderer->renderWidget('foo', [ |
||
378 | 'stylesheetParser_skipSelectors' => '/(^body\.|^caption\.|\.high|^\.)/i', |
||
379 | ]) |
||
380 | ); |
||
381 | } |
||
382 | |||
383 | public function testRenderWidgetWithStylesheetParserValidSelectors() |
||
384 | { |
||
385 | $this->assertSame( |
||
386 | 'CKEDITOR.replace("foo", {"stylesheetParser_validSelectors":/\^(p|span)\.\w+/});', |
||
387 | $this->renderer->renderWidget('foo', [ |
||
388 | 'stylesheetParser_validSelectors' => '/\^(p|span)\.\w+/', |
||
389 | ]) |
||
390 | ); |
||
391 | } |
||
392 | |||
393 | public function testRenderWidgetWithCKEditorConstants() |
||
394 | { |
||
395 | $this->assertSame( |
||
396 | 'CKEDITOR.replace("foo", {"config":{"enterMode":CKEDITOR.ENTER_BR,"shiftEnterMode":CKEDITOR.ENTER_BR}});', |
||
397 | $this->renderer->renderWidget('foo', [ |
||
398 | 'config' => [ |
||
399 | 'enterMode' => 'CKEDITOR.ENTER_BR', |
||
400 | 'shiftEnterMode' => 'CKEDITOR.ENTER_BR', |
||
401 | ], |
||
402 | ]) |
||
403 | ); |
||
404 | } |
||
405 | |||
406 | public function testRenderWidgetWithoutAutoInline() |
||
407 | { |
||
408 | $this->assertSame( |
||
409 | 'CKEDITOR.disableAutoInline = true;'."\n".'CKEDITOR.replace("foo", []);', |
||
410 | $this->renderer->renderWidget('foo', [], ['auto_inline' => false]) |
||
411 | ); |
||
412 | } |
||
413 | |||
414 | public function testRenderWidgetWithInline() |
||
415 | { |
||
416 | $this->assertSame( |
||
417 | 'CKEDITOR.inline("foo", []);', |
||
418 | $this->renderer->renderWidget('foo', [], ['inline' => true]) |
||
419 | ); |
||
420 | } |
||
421 | |||
422 | public function testRenderWidgetWithInputSync() |
||
423 | { |
||
424 | $this->assertSame( |
||
425 | 'var ivory_ckeditor_foo = CKEDITOR.replace("foo", []);'."\n". |
||
426 | 'ivory_ckeditor_foo.on(\'change\', function() { ivory_ckeditor_foo.updateElement(); });', |
||
427 | $this->renderer->renderWidget('foo', [], ['input_sync' => true]) |
||
428 | ); |
||
429 | } |
||
430 | |||
431 | public function testRenderDestroy() |
||
432 | { |
||
433 | $this->assertSame( |
||
434 | 'if (CKEDITOR.instances["foo"]) { '. |
||
435 | 'CKEDITOR.instances["foo"].destroy(true); '. |
||
436 | 'delete CKEDITOR.instances["foo"]; '. |
||
437 | '}', |
||
438 | $this->renderer->renderDestroy('foo') |
||
439 | ); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @param string $path |
||
444 | * @param string $asset |
||
445 | * @param string $url |
||
446 | * |
||
447 | * @dataProvider directoryAssetProvider |
||
448 | */ |
||
449 | public function testRenderPlugin($path, $asset, $url) |
||
450 | { |
||
451 | $this->packages |
||
452 | ->expects($this->once()) |
||
453 | ->method('getUrl') |
||
454 | ->with($this->equalTo($path)) |
||
455 | ->will($this->returnValue($asset)); |
||
456 | |||
457 | $this->assertSame( |
||
458 | 'CKEDITOR.plugins.addExternal("foo", "'.$url.'", "bat");', |
||
459 | $this->renderer->renderPlugin('foo', ['path' => $path, 'filename' => 'bat']) |
||
460 | ); |
||
461 | } |
||
462 | |||
463 | public function testRenderStylesSet() |
||
464 | { |
||
465 | $this->assertSame( |
||
466 | 'if (CKEDITOR.stylesSet.get("foo") === null) { CKEDITOR.stylesSet.add("foo", {"foo":"bar"}); }', |
||
467 | $this->renderer->renderStylesSet('foo', ['foo' => 'bar']) |
||
468 | ); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * @param string $path |
||
473 | * @param string $asset |
||
474 | * @param string $url |
||
475 | * |
||
476 | * @dataProvider directoryAssetProvider |
||
477 | */ |
||
478 | public function testRenderTemplate($path, $asset, $url) |
||
479 | { |
||
480 | $templates = [ |
||
481 | [ |
||
482 | 'title' => 'Template title', |
||
483 | 'html' => '<p>Template content</p>', |
||
484 | ], |
||
485 | ]; |
||
486 | |||
487 | $this->packages |
||
488 | ->expects($this->once()) |
||
489 | ->method('getUrl') |
||
490 | ->with($this->equalTo($path)) |
||
491 | ->will($this->returnValue($asset)); |
||
492 | |||
493 | $json = json_encode(['imagesPath' => $url, 'templates' => $templates]); |
||
494 | |||
495 | $this->assertSame( |
||
496 | 'CKEDITOR.addTemplates("foo", '.$json.');', |
||
497 | $this->renderer->renderTemplate('foo', ['imagesPath' => $path, 'templates' => $templates]) |
||
498 | ); |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * @param string $path |
||
503 | * @param string $asset |
||
504 | * @param string $url |
||
505 | * |
||
506 | * @dataProvider directoryAssetProvider |
||
507 | */ |
||
508 | public function testRenderTemplateWithEngineInteface($path, $asset, $url) |
||
509 | { |
||
510 | $templates = [ |
||
511 | [ |
||
512 | 'title' => 'Template title', |
||
513 | 'template' => $template = 'template_name', |
||
514 | 'template_parameters' => $templateParameters = ['foo' => 'bar'], |
||
515 | ], |
||
516 | ]; |
||
517 | |||
518 | $processedTemplates = [ |
||
519 | [ |
||
520 | 'title' => 'Template title', |
||
521 | 'html' => $html = '<p>Template content</p>', |
||
522 | ], |
||
523 | ]; |
||
524 | |||
525 | $this->packages |
||
526 | ->expects($this->once()) |
||
527 | ->method('getUrl') |
||
528 | ->with($this->equalTo($path)) |
||
529 | ->will($this->returnValue($asset)); |
||
530 | |||
531 | $this->templating |
||
532 | ->expects($this->once()) |
||
533 | ->method('render') |
||
534 | ->with($this->identicalTo($template), $this->identicalTo($templateParameters)) |
||
535 | ->will($this->returnValue($html)); |
||
536 | |||
537 | $json = json_encode(['imagesPath' => $url, 'templates' => $processedTemplates]); |
||
538 | |||
539 | $this->assertSame( |
||
540 | 'CKEDITOR.addTemplates("foo", '.$json.');', |
||
541 | $this->renderer->renderTemplate('foo', ['imagesPath' => $path, 'templates' => $templates]) |
||
542 | ); |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * @return array |
||
547 | */ |
||
548 | public function languageProvider() |
||
549 | { |
||
550 | return [ |
||
551 | ['en', 'en'], |
||
552 | ['pt_BR', 'pt-br'], |
||
553 | ]; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * @return array |
||
558 | */ |
||
559 | public function directoryAssetProvider() |
||
560 | { |
||
561 | return [ |
||
562 | ['directory/', 'url/', 'url/'], |
||
563 | ['directory/', 'url/?v=2', 'url/'], |
||
564 | ]; |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * @return array |
||
569 | */ |
||
570 | public function fileAssetProvider() |
||
571 | { |
||
572 | return [ |
||
573 | ['file.js', 'url.js', 'url.js'], |
||
574 | ['file.js', 'url.js?v=2', 'url.js?v=2'], |
||
575 | ]; |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @return array |
||
580 | */ |
||
581 | public function filesAssetProvider() |
||
582 | { |
||
583 | return [ |
||
584 | [['file'], ['url'], ['url']], |
||
585 | [['file'], ['url?v=2'], ['url?v=2']], |
||
586 | [['file1', 'file2'], ['url1', 'url2'], ['url1', 'url2']], |
||
587 | [['file1', 'file2'], ['url1?v=2', 'url2'], ['url1?v=2', 'url2']], |
||
588 | [['file1', 'file2'], ['url1', 'url2?v=2'], ['url1', 'url2?v=2']], |
||
589 | [['file1', 'file2'], ['url1?v=2', 'url2?v=2'], ['url1?v=2', 'url2?v=2']], |
||
590 | ]; |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * @return array |
||
595 | */ |
||
596 | public function filebrowserProvider() |
||
597 | { |
||
598 | return [ |
||
599 | ['Browse'], |
||
600 | ['FlashBrowse'], |
||
601 | ['ImageBrowse'], |
||
602 | ['ImageBrowseLink'], |
||
603 | ['Upload'], |
||
604 | ['FlashUpload'], |
||
605 | ['ImageUpload'], |
||
606 | ]; |
||
607 | } |
||
608 | } |
||
609 |