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 |
||
| 20 | class TemplateManagerTest extends AbstractTestCase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var TemplateManager |
||
| 24 | */ |
||
| 25 | private $templateManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | */ |
||
| 30 | protected function setUp() |
||
| 31 | { |
||
| 32 | $this->templateManager = new TemplateManager(); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function testDefaultState() |
||
| 36 | { |
||
| 37 | $this->assertFalse($this->templateManager->hasTemplates()); |
||
| 38 | $this->assertSame([], $this->templateManager->getTemplates()); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testInitialState() |
||
| 42 | { |
||
| 43 | $templates = [ |
||
| 44 | 'default' => [ |
||
| 45 | 'imagesPath' => '/my/path', |
||
| 46 | 'templates' => [ |
||
| 47 | [ |
||
| 48 | 'title' => 'My Template', |
||
| 49 | 'html' => '<h1>Template</h1><p>Type your text here.</p>', |
||
| 50 | ], |
||
| 51 | ], |
||
| 52 | ], |
||
| 53 | ]; |
||
| 54 | |||
| 55 | $this->templateManager = new TemplateManager($templates); |
||
| 56 | |||
| 57 | $this->assertTrue($this->templateManager->hasTemplates()); |
||
| 58 | $this->assertTrue($this->templateManager->hasTemplate('default')); |
||
| 59 | $this->assertSame($templates['default'], $this->templateManager->getTemplate('default')); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testTemplates() |
||
| 63 | { |
||
| 64 | $templates = [ |
||
| 65 | 'default' => [ |
||
| 66 | 'imagesPath' => '/my/path', |
||
| 67 | 'templates' => [ |
||
| 68 | [ |
||
| 69 | 'title' => 'My Template', |
||
| 70 | 'html' => '<h1>Template</h1><p>Type your text here.</p>', |
||
| 71 | ], |
||
| 72 | ], |
||
| 73 | ], |
||
| 74 | ]; |
||
| 75 | |||
| 76 | $this->templateManager->setTemplates($templates); |
||
| 77 | |||
| 78 | $this->assertTrue($this->templateManager->hasTemplates()); |
||
| 79 | $this->assertTrue($this->templateManager->hasTemplate('default')); |
||
| 80 | $this->assertSame($templates, $this->templateManager->getTemplates()); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @expectedException \Ivory\CKEditorBundle\Exception\TemplateManagerException |
||
| 85 | * @expectedExceptionMessage The CKEditor template "foo" does not exist. |
||
| 86 | */ |
||
| 87 | public function testGetTemplateWithInvalidValue() |
||
| 88 | { |
||
| 89 | $this->templateManager->getTemplate('foo'); |
||
| 90 | } |
||
| 91 | } |
||
| 92 |