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 StylesSetManagerTest extends AbstractTestCase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var StylesSetManager |
||
| 24 | */ |
||
| 25 | private $stylesSetManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | */ |
||
| 30 | protected function setUp() |
||
| 31 | { |
||
| 32 | $this->stylesSetManager = new StylesSetManager(); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function testDefaultState() |
||
| 36 | { |
||
| 37 | $this->assertFalse($this->stylesSetManager->hasStylesSets()); |
||
| 38 | $this->assertSame([], $this->stylesSetManager->getStylesSets()); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testInitialState() |
||
| 42 | { |
||
| 43 | $stylesSets = [ |
||
| 44 | 'default' => [ |
||
| 45 | ['name' => 'Blue Title', 'element' => 'h2', 'styles' => ['color' => 'Blue']], |
||
| 46 | ['name' => 'CSS Style', 'element' => 'span', 'attributes' => ['class' => 'my_style']], |
||
| 47 | ], |
||
| 48 | ]; |
||
| 49 | |||
| 50 | $this->stylesSetManager = new StylesSetManager($stylesSets); |
||
| 51 | |||
| 52 | $this->assertTrue($this->stylesSetManager->hasStylesSets()); |
||
| 53 | $this->assertTrue($this->stylesSetManager->hasStylesSet('default')); |
||
| 54 | $this->assertSame($stylesSets['default'], $this->stylesSetManager->getStylesSet('default')); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testTemplates() |
||
| 58 | { |
||
| 59 | $stylesSets = [ |
||
| 60 | 'default' => [ |
||
| 61 | ['name' => 'Blue Title', 'element' => 'h2', 'styles' => ['color' => 'Blue']], |
||
| 62 | ['name' => 'CSS Style', 'element' => 'span', 'attributes' => ['class' => 'my_style']], |
||
| 63 | ], |
||
| 64 | ]; |
||
| 65 | |||
| 66 | $this->stylesSetManager->setStylesSets($stylesSets); |
||
| 67 | |||
| 68 | $this->assertTrue($this->stylesSetManager->hasStylesSets()); |
||
| 69 | $this->assertTrue($this->stylesSetManager->hasStylesSet('default')); |
||
| 70 | $this->assertSame($stylesSets, $this->stylesSetManager->getStylesSets()); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @expectedException \Ivory\CKEditorBundle\Exception\StylesSetManagerException |
||
| 75 | * @expectedExceptionMessage The CKEditor styles set "foo" does not exist. |
||
| 76 | */ |
||
| 77 | public function testGetStylesSetWithInvalidValue() |
||
| 78 | { |
||
| 79 | $this->stylesSetManager->getStylesSet('foo'); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |