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 |
||
| 9 | class RegistryTest extends TestCase |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var Registry |
||
| 13 | */ |
||
| 14 | protected $registry; |
||
| 15 | |||
| 16 | public function setUp() |
||
| 17 | { |
||
| 18 | $this->registry = new Registry(); |
||
| 19 | } |
||
| 20 | |||
| 21 | public function testAddingHandlers() |
||
| 22 | { |
||
| 23 | $handler = $this->getMockForAbstractClass(HandlerInterface::class); |
||
| 24 | $this->registry->addHandler('some_handler', $handler); |
||
| 25 | |||
| 26 | $this->assertEquals($this->registry->getHandler('some_handler'), $handler); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function testGettingHandlers() |
||
| 30 | { |
||
| 31 | $handler = $this->getMockForAbstractClass(HandlerInterface::class); |
||
| 32 | $this->registry->addHandler('some_handler', $handler); |
||
| 33 | |||
| 34 | $returnedHandler = $this->registry->getHandler('some_handler'); |
||
| 35 | |||
| 36 | $this->assertSame($handler, $returnedHandler); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @expectedException \Netgen\Bundle\OpenGraphBundle\Exception\HandlerNotFoundException |
||
| 41 | * @expectedExceptionMessage Meta tag handler with 'some_handler' identifier not found. |
||
| 42 | */ |
||
| 43 | public function testGettingNonExistentHandler() |
||
| 44 | { |
||
| 45 | $this->registry->getHandler('some_handler'); |
||
| 46 | } |
||
| 47 | } |
||
| 48 |