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 \PHPUnit_Framework_TestCase |
||
| 22 | { |
||
| 23 | /** @var \Ivory\FormExtraBundle\DependencyInjection\Compiler\TemplatingCompilerPass */ |
||
| 24 | private $compilerPass; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | protected function setUp() |
||
| 30 | { |
||
| 31 | $this->compilerPass = new TemplatingCompilerPass(); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | protected function tearDown() |
||
| 38 | { |
||
| 39 | unset($this->compilerPass); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function testPhpTemplating() |
||
| 43 | { |
||
| 44 | $containerBuilder = $this->createContainerBuilderMock(); |
||
| 45 | $containerBuilder |
||
| 46 | ->expects($this->exactly(2)) |
||
| 47 | ->method('hasDefinition') |
||
| 48 | ->will($this->returnValueMap(array( |
||
| 49 | array('templating.engine.php', false), |
||
| 50 | array('twig', true), |
||
| 51 | ))); |
||
| 52 | |||
| 53 | $containerBuilder |
||
| 54 | ->expects($this->once()) |
||
| 55 | ->method('removeDefinition') |
||
| 56 | ->with($this->identicalTo('ivory_form_extra.templating.helper')); |
||
| 57 | |||
| 58 | $this->compilerPass->process($containerBuilder); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testTwigTemplating() |
||
| 62 | { |
||
| 63 | $containerBuilder = $this->createContainerBuilderMock(); |
||
| 64 | $containerBuilder |
||
| 65 | ->expects($this->exactly(2)) |
||
| 66 | ->method('hasDefinition') |
||
| 67 | ->will($this->returnValueMap(array( |
||
| 68 | array('templating.engine.php', true), |
||
| 69 | array('twig', false), |
||
| 70 | ))); |
||
| 71 | |||
| 72 | $containerBuilder |
||
| 73 | ->expects($this->once()) |
||
| 74 | ->method('removeDefinition') |
||
| 75 | ->with($this->identicalTo('ivory_form_extra.twig.extension')); |
||
| 76 | |||
| 77 | $this->compilerPass->process($containerBuilder); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Creates a container builder mock. |
||
| 82 | * |
||
| 83 | * @return \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject The container builder mock. |
||
| 84 | */ |
||
| 85 | private function createContainerBuilderMock() |
||
| 86 | { |
||
| 87 | return $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') |
||
| 88 | ->disableOriginalConstructor() |
||
| 89 | ->setMethods(array('hasDefinition', 'removeDefinition')) |
||
| 90 | ->getMock(); |
||
| 91 | } |
||
| 92 | } |
||
| 93 |