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