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 |
||
| 33 | abstract class AbstractIvoryCKEditorExtensionTest extends AbstractTestCase |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var ContainerBuilder |
||
| 37 | */ |
||
| 38 | private $container; |
||
| 39 | /** |
||
| 40 | * @var Packages|CoreAssetsHelper|\PHPUnit_Framework_MockObject_MockObject |
||
| 41 | */ |
||
| 42 | private $packages; |
||
| 43 | /** |
||
| 44 | * @var RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 45 | */ |
||
| 46 | private $router; |
||
| 47 | /** |
||
| 48 | * @var FormRendererInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 49 | */ |
||
| 50 | private $formRenderer; |
||
| 51 | /** |
||
| 52 | * @var RequestStack|\PHPUnit_Framework_MockObject_MockObject |
||
| 53 | */ |
||
| 54 | private $requestStack; |
||
| 55 | /** |
||
| 56 | * @var EngineInterface |
||
| 57 | */ |
||
| 58 | private $templating; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | protected function setUp() |
||
| 64 | { |
||
| 65 | $this->router = $this->createMock(RouterInterface::class); |
||
| 66 | $this->formRenderer = $this->createMock(FormRendererInterface::class); |
||
| 67 | $this->packages = $this->getMockBuilder(Packages::class) |
||
| 68 | ->disableOriginalConstructor() |
||
| 69 | ->getMock(); |
||
| 70 | $this->requestStack = $this->createMock(RequestStack::class); |
||
| 71 | $this->container = new ContainerBuilder(); |
||
| 72 | $this->templating = $this->createMock(EngineInterface::class); |
||
| 73 | |||
| 74 | $this->container->set('assets.packages', $this->packages); |
||
| 75 | $this->container->set('router', $this->router); |
||
| 76 | $this->container->set('templating.form.renderer', $this->formRenderer); |
||
| 77 | $this->container->set('twig.form.renderer', $this->formRenderer); |
||
| 78 | $this->container->set('request_stack', $this->requestStack); |
||
| 79 | $this->container->set('templating', $this->templating); |
||
| 80 | |||
| 81 | $this->container->registerExtension($extension = new IvoryCKEditorExtension()); |
||
| 82 | $this->container->loadFromExtension($extension->getAlias()); |
||
| 83 | |||
| 84 | $toBePublic = [ |
||
| 85 | 'ivory_ck_editor.template_manager', |
||
| 86 | 'ivory_ck_editor.form.type', |
||
| 87 | 'ivory_ck_editor.config_manager', |
||
| 88 | 'ivory_ck_editor.plugin_manager', |
||
| 89 | 'ivory_ck_editor.styles_set_manager', |
||
| 90 | 'ivory_ck_editor.toolbar_manager', |
||
| 91 | ]; |
||
| 92 | $this->container->addCompilerPass(new TestContainerPass($toBePublic), PassConfig::TYPE_OPTIMIZE); |
||
| 93 | (new IvoryCKEditorBundle())->build($this->container); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param ContainerBuilder $container |
||
| 98 | * @param string $configuration |
||
| 99 | */ |
||
| 100 | abstract protected function loadConfiguration(ContainerBuilder $container, $configuration); |
||
| 101 | |||
| 102 | public function testFormType() |
||
| 103 | { |
||
| 104 | $this->container->compile(); |
||
| 105 | |||
| 106 | $type = $this->container->get('ivory_ck_editor.form.type'); |
||
| 107 | |||
| 108 | $this->assertInstanceOf(CKEditorType::class, $type); |
||
| 109 | $this->assertTrue($type->isEnable()); |
||
| 110 | $this->assertTrue($type->isAutoload()); |
||
| 111 | $this->assertTrue($type->isAutoInline()); |
||
| 112 | $this->assertFalse($type->isInline()); |
||
| 113 | $this->assertFalse($type->useJquery()); |
||
| 114 | $this->assertFalse($type->isInputSync()); |
||
| 115 | $this->assertFalse($type->useRequireJs()); |
||
| 116 | $this->assertFalse($type->hasFilebrowsers()); |
||
| 117 | $this->assertSame('bundles/ivoryckeditor/', $type->getBasePath()); |
||
| 118 | $this->assertSame('bundles/ivoryckeditor/ckeditor.js', $type->getJsPath()); |
||
| 119 | $this->assertSame('bundles/ivoryckeditor/adapters/jquery.js', $type->getJqueryPath()); |
||
| 120 | $this->assertSame($this->container->get('ivory_ck_editor.config_manager'), $type->getConfigManager()); |
||
| 121 | $this->assertSame($this->container->get('ivory_ck_editor.plugin_manager'), $type->getPluginManager()); |
||
| 122 | $this->assertSame($this->container->get('ivory_ck_editor.styles_set_manager'), $type->getStylesSetManager()); |
||
| 123 | $this->assertSame($this->container->get('ivory_ck_editor.template_manager'), $type->getTemplateManager()); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function testFormTag() |
||
| 127 | { |
||
| 128 | $this->container->compile(); |
||
| 129 | |||
| 130 | $tag = $this->container->getDefinition('ivory_ck_editor.form.type')->getTag('form.type'); |
||
| 131 | |||
| 132 | if (!method_exists(AbstractType::class, 'getBlockPrefix')) { |
||
| 133 | $this->assertSame([['alias' => 'ckeditor']], $tag); |
||
| 134 | } else { |
||
| 135 | $this->assertSame([[]], $tag); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testDisable() |
||
| 140 | { |
||
| 141 | $this->loadConfiguration($this->container, 'disable'); |
||
| 142 | $this->container->compile(); |
||
| 143 | |||
| 144 | $this->assertFalse($this->container->get('ivory_ck_editor.form.type')->isEnable()); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testAsync() |
||
| 148 | { |
||
| 149 | $this->loadConfiguration($this->container, 'async'); |
||
| 150 | $this->container->compile(); |
||
| 151 | |||
| 152 | $this->assertTrue($this->container->get('ivory_ck_editor.form.type')->isAsync()); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function testAutoload() |
||
| 156 | { |
||
| 157 | $this->loadConfiguration($this->container, 'autoload'); |
||
| 158 | $this->container->compile(); |
||
| 159 | |||
| 160 | $this->assertFalse($this->container->get('ivory_ck_editor.form.type')->isAutoload()); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function testAutoInline() |
||
| 164 | { |
||
| 165 | $this->loadConfiguration($this->container, 'auto_inline'); |
||
| 166 | $this->container->compile(); |
||
| 167 | |||
| 168 | $this->assertFalse($this->container->get('ivory_ck_editor.form.type')->isAutoInline()); |
||
| 169 | } |
||
| 170 | |||
| 171 | public function testInline() |
||
| 172 | { |
||
| 173 | $this->loadConfiguration($this->container, 'inline'); |
||
| 174 | $this->container->compile(); |
||
| 175 | |||
| 176 | $this->assertTrue($this->container->get('ivory_ck_editor.form.type')->isInline()); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function testInputSync() |
||
| 180 | { |
||
| 181 | $this->loadConfiguration($this->container, 'input_sync'); |
||
| 182 | $this->container->compile(); |
||
| 183 | |||
| 184 | $this->assertTrue($this->container->get('ivory_ck_editor.form.type')->isInputSync()); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function testRequireJs() |
||
| 188 | { |
||
| 189 | $this->loadConfiguration($this->container, 'require_js'); |
||
| 190 | $this->container->compile(); |
||
| 191 | |||
| 192 | $this->assertTrue($this->container->get('ivory_ck_editor.form.type')->useRequireJs()); |
||
| 193 | } |
||
| 194 | |||
| 195 | public function testJquery() |
||
| 196 | { |
||
| 197 | $this->loadConfiguration($this->container, 'jquery'); |
||
| 198 | $this->container->compile(); |
||
| 199 | |||
| 200 | $this->assertTrue($this->container->get('ivory_ck_editor.form.type')->useJquery()); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function testJqueryPath() |
||
| 204 | { |
||
| 205 | $this->loadConfiguration($this->container, 'jquery_path'); |
||
| 206 | $this->container->compile(); |
||
| 207 | |||
| 208 | $this->assertSame('foo/jquery.js', $this->container->get('ivory_ck_editor.form.type')->getJqueryPath()); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function testCustomPaths() |
||
| 212 | { |
||
| 213 | $this->loadConfiguration($this->container, 'custom_paths'); |
||
| 214 | $this->container->compile(); |
||
| 215 | |||
| 216 | $ckEditorType = $this->container->get('ivory_ck_editor.form.type'); |
||
| 217 | |||
| 218 | $this->assertSame('foo/', $ckEditorType->getBasePath()); |
||
| 219 | $this->assertSame('foo/ckeditor.js', $ckEditorType->getJsPath()); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function testFilebrowsers() |
||
| 223 | { |
||
| 224 | $this->loadConfiguration($this->container, 'filebrowsers'); |
||
| 225 | $this->container->compile(); |
||
| 226 | |||
| 227 | $this->assertSame( |
||
| 228 | ['VideoBrowse', 'VideoUpload'], |
||
| 229 | $this->container->get('ivory_ck_editor.form.type')->getFilebrowsers() |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function testSingleConfiguration() |
||
| 234 | { |
||
| 235 | $this->loadConfiguration($this->container, 'single_configuration'); |
||
| 236 | $this->container->compile(); |
||
| 237 | |||
| 238 | $configManager = $this->container->get('ivory_ck_editor.config_manager'); |
||
| 239 | |||
| 240 | $expected = [ |
||
| 241 | 'default' => [ |
||
| 242 | 'toolbar' => 'default', |
||
| 243 | 'uiColor' => '#000000', |
||
| 244 | ], |
||
| 245 | ]; |
||
| 246 | |||
| 247 | $this->assertSame('default', $configManager->getDefaultConfig()); |
||
| 248 | $this->assertSame($expected, $configManager->getConfigs()); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function testMultipleConfiguration() |
||
| 252 | { |
||
| 253 | $this->loadConfiguration($this->container, 'multiple_configuration'); |
||
| 254 | $this->container->compile(); |
||
| 255 | |||
| 256 | $configManager = $this->container->get('ivory_ck_editor.config_manager'); |
||
| 257 | |||
| 258 | $expected = [ |
||
| 259 | 'default' => [ |
||
| 260 | 'toolbar' => 'default', |
||
| 261 | 'uiColor' => '#000000', |
||
| 262 | ], |
||
| 263 | 'custom' => [ |
||
| 264 | 'toolbar' => 'custom', |
||
| 265 | 'uiColor' => '#ffffff', |
||
| 266 | ], |
||
| 267 | ]; |
||
| 268 | |||
| 269 | $this->assertSame('default', $configManager->getDefaultConfig()); |
||
| 270 | $this->assertSame($expected, $configManager->getConfigs()); |
||
| 271 | } |
||
| 272 | |||
| 273 | public function testDefaultConfiguration() |
||
| 274 | { |
||
| 275 | $this->loadConfiguration($this->container, 'default_configuration'); |
||
| 276 | $this->container->compile(); |
||
| 277 | |||
| 278 | $configManager = $this->container->get('ivory_ck_editor.config_manager'); |
||
| 279 | |||
| 280 | $expected = [ |
||
| 281 | 'default' => ['uiColor' => '#000000'], |
||
| 282 | 'custom' => ['uiColor' => '#ffffff'], |
||
| 283 | ]; |
||
| 284 | |||
| 285 | $this->assertSame('default', $configManager->getDefaultConfig()); |
||
| 286 | $this->assertSame($expected, $configManager->getConfigs()); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function testPlugins() |
||
| 290 | { |
||
| 291 | $this->loadConfiguration($this->container, 'plugins'); |
||
| 292 | $this->container->compile(); |
||
| 293 | |||
| 294 | $expected = [ |
||
| 295 | 'plugin-name' => [ |
||
| 296 | 'path' => '/my/path', |
||
| 297 | 'filename' => 'plugin.js', |
||
| 298 | ], |
||
| 299 | ]; |
||
| 300 | |||
| 301 | $this->assertSame($expected, $this->container->get('ivory_ck_editor.plugin_manager')->getPlugins()); |
||
| 302 | } |
||
| 303 | |||
| 304 | public function testStylesSets() |
||
| 305 | { |
||
| 306 | $this->loadConfiguration($this->container, 'styles_sets'); |
||
| 307 | $this->container->compile(); |
||
| 308 | |||
| 309 | $expected = [ |
||
| 310 | 'styles-set-name' => [ |
||
| 311 | [ |
||
| 312 | 'name' => 'Blue Title', |
||
| 313 | 'element' => 'h2', |
||
| 314 | 'styles' => ['text-decoration' => 'underline'], |
||
| 315 | ], |
||
| 316 | [ |
||
| 317 | 'name' => 'CSS Style', |
||
| 318 | 'element' => 'span', |
||
| 319 | 'attributes' => ['data-class' => 'my-style'], |
||
| 320 | ], |
||
| 321 | [ |
||
| 322 | 'name' => 'Widget Style', |
||
| 323 | 'type' => 'widget', |
||
| 324 | 'widget' => 'my-widget', |
||
| 325 | 'attributes' => ['data-class' => 'my-style'], |
||
| 326 | ], |
||
| 327 | [ |
||
| 328 | 'name' => 'Multiple Elements Style', |
||
| 329 | 'element' => ['span', 'p', 'h3'], |
||
| 330 | 'attributes' => ['data-class' => 'my-style'], |
||
| 331 | ], |
||
| 332 | ], |
||
| 333 | ]; |
||
| 334 | |||
| 335 | $this->assertSame($expected, $this->container->get('ivory_ck_editor.styles_set_manager')->getStylesSets()); |
||
| 336 | } |
||
| 337 | |||
| 338 | public function testTemplates() |
||
| 339 | { |
||
| 340 | $this->loadConfiguration($this->container, 'templates'); |
||
| 341 | $this->container->compile(); |
||
| 342 | |||
| 343 | $expected = [ |
||
| 344 | 'template-name' => [ |
||
| 345 | 'imagesPath' => '/my/path', |
||
| 346 | 'templates' => [ |
||
| 347 | [ |
||
| 348 | 'title' => 'My Template', |
||
| 349 | 'image' => 'image.jpg', |
||
| 350 | 'description' => 'My awesome description', |
||
| 351 | 'html' => '<h1>Template</h1><p>Type your text here.</p>', |
||
| 352 | 'template' => 'AppBundle:CKEditor:template.html.twig', |
||
| 353 | 'template_parameters' => ['foo' => 'bar'], |
||
| 354 | ], |
||
| 355 | ], |
||
| 356 | ], |
||
| 357 | ]; |
||
| 358 | |||
| 359 | $this->assertSame($expected, $this->container->get('ivory_ck_editor.template_manager')->getTemplates()); |
||
| 360 | } |
||
| 361 | |||
| 362 | public function testToolbars() |
||
| 363 | { |
||
| 364 | $this->loadConfiguration($this->container, 'toolbars'); |
||
| 365 | $this->container->compile(); |
||
| 366 | |||
| 367 | $toolbarManager = $this->container->get('ivory_ck_editor.toolbar_manager'); |
||
| 368 | |||
| 369 | $this->assertSame( |
||
| 370 | [ |
||
| 371 | 'document' => ['Source', '-', 'Save'], |
||
| 372 | 'tools' => ['Maximize'], |
||
| 373 | ], |
||
| 374 | array_intersect_key($toolbarManager->getItems(), ['document' => true, 'tools' => true]) |
||
| 375 | ); |
||
| 376 | |||
| 377 | $this->assertSame( |
||
| 378 | [ |
||
| 379 | 'default' => [ |
||
| 380 | '@document', |
||
| 381 | '/', |
||
| 382 | ['Anchor'], |
||
| 383 | '/', |
||
| 384 | '@tools', |
||
| 385 | ], |
||
| 386 | 'custom' => [ |
||
| 387 | '@document', |
||
| 388 | '/', |
||
| 389 | ['Anchor'], |
||
| 390 | ], |
||
| 391 | ], |
||
| 392 | array_intersect_key($toolbarManager->getToolbars(), ['default' => true, 'custom' => true]) |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @expectedException \Ivory\CKEditorBundle\Exception\DependencyInjectionException |
||
| 398 | * @expectedExceptionMessage The default config "bar" does not exist. |
||
| 399 | */ |
||
| 400 | public function testInvalidDefaultConfig() |
||
| 401 | { |
||
| 402 | $this->loadConfiguration($this->container, 'invalid_default_config'); |
||
| 403 | $this->container->compile(); |
||
| 404 | } |
||
| 405 | } |
||
| 406 |