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 PluginManagerTest extends AbstractTestCase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var PluginManager |
||
| 24 | */ |
||
| 25 | private $pluginManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | */ |
||
| 30 | protected function setUp() |
||
| 31 | { |
||
| 32 | $this->pluginManager = new PluginManager(); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function testDefaultState() |
||
| 36 | { |
||
| 37 | $this->assertFalse($this->pluginManager->hasPlugins()); |
||
| 38 | $this->assertSame([], $this->pluginManager->getPlugins()); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testInitialState() |
||
| 42 | { |
||
| 43 | $plugins = [ |
||
| 44 | 'wordcount' => [ |
||
| 45 | 'path' => '/my/path', |
||
| 46 | 'filename' => 'plugin.js', |
||
| 47 | ], |
||
| 48 | ]; |
||
| 49 | |||
| 50 | $this->pluginManager = new PluginManager($plugins); |
||
| 51 | |||
| 52 | $this->assertTrue($this->pluginManager->hasPlugins()); |
||
| 53 | $this->assertTrue($this->pluginManager->hasPlugin('wordcount')); |
||
| 54 | $this->assertSame($plugins['wordcount'], $this->pluginManager->getPlugin('wordcount')); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testPlugins() |
||
| 58 | { |
||
| 59 | $plugins = [ |
||
| 60 | 'wordcount' => [ |
||
| 61 | 'path' => '/my/path', |
||
| 62 | 'filename' => 'plugin.js', |
||
| 63 | ], |
||
| 64 | ]; |
||
| 65 | |||
| 66 | $this->pluginManager->setPlugins($plugins); |
||
| 67 | |||
| 68 | $this->assertTrue($this->pluginManager->hasPlugins()); |
||
| 69 | $this->assertTrue($this->pluginManager->hasPlugin('wordcount')); |
||
| 70 | $this->assertSame($plugins, $this->pluginManager->getPlugins()); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @expectedException \Ivory\CKEditorBundle\Exception\PluginManagerException |
||
| 75 | * @expectedExceptionMessage The CKEditor plugin "foo" does not exist. |
||
| 76 | */ |
||
| 77 | public function testGetPluginWithInvalidValue() |
||
| 78 | { |
||
| 79 | $this->pluginManager->getPlugin('foo'); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |