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 |
||
| 21 | class TemplatingCompilerPassTest extends AbstractTestCase |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var TemplatingCompilerPass |
||
| 25 | */ |
||
| 26 | private $compilerPass; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | protected function setUp() |
||
| 32 | { |
||
| 33 | $this->compilerPass = new TemplatingCompilerPass(); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testPhpTemplating() |
||
| 37 | { |
||
| 38 | $containerBuilder = $this->createContainerBuilderMock(); |
||
| 39 | $containerBuilder |
||
| 40 | ->expects($this->exactly(2)) |
||
| 41 | ->method('hasDefinition') |
||
| 42 | ->will($this->returnValueMap([ |
||
| 43 | ['templating.engine.php', false], |
||
| 44 | ['twig', true], |
||
| 45 | ])); |
||
| 46 | |||
| 47 | $containerBuilder |
||
| 48 | ->expects($this->once()) |
||
| 49 | ->method('removeDefinition') |
||
| 50 | ->with($this->identicalTo('fos_ck_editor.templating.helper')); |
||
| 51 | |||
| 52 | $this->compilerPass->process($containerBuilder); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testTwigTemplating() |
||
| 56 | { |
||
| 57 | $containerBuilder = $this->createContainerBuilderMock(); |
||
| 58 | $containerBuilder |
||
| 59 | ->expects($this->exactly(2)) |
||
| 60 | ->method('hasDefinition') |
||
| 61 | ->will($this->returnValueMap([ |
||
| 62 | ['templating.engine.php', true], |
||
| 63 | ['twig', false], |
||
| 64 | ])); |
||
| 65 | |||
| 66 | $containerBuilder |
||
| 67 | ->expects($this->once()) |
||
| 68 | ->method('removeDefinition') |
||
| 69 | ->with($this->identicalTo('fos_ck_editor.twig_extension')); |
||
| 70 | |||
| 71 | $this->compilerPass->process($containerBuilder); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject |
||
| 76 | */ |
||
| 77 | private function createContainerBuilderMock() |
||
| 78 | { |
||
| 79 | return $this->getMockBuilder(ContainerBuilder::class) |
||
| 80 | ->disableOriginalConstructor() |
||
| 81 | ->setMethods(['hasDefinition', 'removeDefinition']) |
||
| 82 | ->getMock(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 |