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 ResourceCompilerPassTest extends AbstractTestCase |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var ResourceCompilerPass |
||
| 25 | */ |
||
| 26 | private $compilerPass; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | protected function setUp() |
||
| 32 | { |
||
| 33 | $this->compilerPass = new ResourceCompilerPass(); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testTwigResource() |
||
| 37 | { |
||
| 38 | $containerBuilder = $this->createContainerBuilderMock(); |
||
| 39 | $containerBuilder |
||
| 40 | ->expects($this->exactly(2)) |
||
| 41 | ->method('hasParameter') |
||
| 42 | ->will($this->returnValueMap([ |
||
| 43 | ['templating.helper.form.resources', false], |
||
| 44 | [$parameter = 'twig.form.resources', true], |
||
| 45 | ])); |
||
| 46 | |||
| 47 | $containerBuilder |
||
| 48 | ->expects($this->once()) |
||
| 49 | ->method('getParameter') |
||
| 50 | ->with($this->identicalTo($parameter)) |
||
| 51 | ->will($this->returnValue([$template = 'layout.html.twig'])); |
||
| 52 | |||
| 53 | $containerBuilder |
||
| 54 | ->expects($this->once()) |
||
| 55 | ->method('setParameter') |
||
| 56 | ->with( |
||
| 57 | $this->identicalTo($parameter), |
||
| 58 | $this->identicalTo([ |
||
| 59 | '@IvoryCKEditor/Form/ckeditor_widget.html.twig', |
||
| 60 | $template, |
||
| 61 | ]) |
||
| 62 | ); |
||
| 63 | |||
| 64 | $this->compilerPass->process($containerBuilder); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function testPhpResource() |
||
| 68 | { |
||
| 69 | $containerBuilder = $this->createContainerBuilderMock(); |
||
| 70 | $containerBuilder |
||
| 71 | ->expects($this->exactly(2)) |
||
| 72 | ->method('hasParameter') |
||
| 73 | ->will($this->returnValueMap([ |
||
| 74 | [$parameter = 'templating.helper.form.resources', true], |
||
| 75 | ['twig.form.resources', false], |
||
| 76 | ])); |
||
| 77 | |||
| 78 | $containerBuilder |
||
| 79 | ->expects($this->once()) |
||
| 80 | ->method('getParameter') |
||
| 81 | ->with($this->identicalTo($parameter)) |
||
| 82 | ->will($this->returnValue([$template = 'layout.html.php'])); |
||
| 83 | |||
| 84 | $containerBuilder |
||
| 85 | ->expects($this->once()) |
||
| 86 | ->method('setParameter') |
||
| 87 | ->with( |
||
| 88 | $this->identicalTo($parameter), |
||
| 89 | $this->identicalTo(['IvoryCKEditorBundle:Form', $template]) |
||
| 90 | ); |
||
| 91 | |||
| 92 | $this->compilerPass->process($containerBuilder); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject |
||
| 97 | */ |
||
| 98 | private function createContainerBuilderMock() |
||
| 99 | { |
||
| 100 | return $this->getMockBuilder(ContainerBuilder::class) |
||
| 101 | ->disableOriginalConstructor() |
||
| 102 | ->setMethods(['hasParameter', 'getParameter', 'setParameter']) |
||
| 103 | ->getMock(); |
||
| 104 | } |
||
| 105 | } |
||
| 106 |