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 |
||
| 20 | class PixelartSwiftmailerManipulatorExtensionTest extends \PHPUnit_Framework_TestCase |
||
| 21 | { |
||
| 22 | public function getConfigTypes() |
||
| 23 | { |
||
| 24 | return [ |
||
| 25 | ['php'], |
||
| 26 | ['yml'], |
||
| 27 | ]; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @dataProvider getConfigTypes |
||
| 32 | * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException |
||
| 33 | */ |
||
| 34 | public function testEmptyConfig($type) |
||
| 35 | { |
||
| 36 | $container = $this->loadContainerFromFile('empty', $type); |
||
| 37 | |||
| 38 | $container->get('pixelart_swiftmailer_manipulator.mailer.default.plugin'); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @dataProvider getConfigTypes |
||
| 43 | */ |
||
| 44 | public function testFull($type) |
||
| 45 | { |
||
| 46 | $container = $this->loadContainerFromFile('full', $type); |
||
| 47 | |||
| 48 | self::assertSame( |
||
| 49 | ['swiftmailer.default.plugin' => [[]]], |
||
| 50 | $container->getDefinition('pixelart_swiftmailer_manipulator.mailer.default.plugin')->getTags() |
||
| 51 | ); |
||
| 52 | |||
| 53 | self::assertSame( |
||
| 54 | '[TESTSYSTEM!]', |
||
| 55 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.default.prepend_subject') |
||
| 56 | ); |
||
| 57 | |||
| 58 | self::assertSame( |
||
| 59 | 'swiftmailer/prepend_body.txt.twig', |
||
| 60 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.default.prepend_body') |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @dataProvider getConfigTypes |
||
| 66 | */ |
||
| 67 | public function testOneMailer($type) |
||
| 68 | { |
||
| 69 | $container = $this->loadContainerFromFile('one_mailer', $type); |
||
| 70 | |||
| 71 | self::assertSame( |
||
| 72 | ['swiftmailer.main_mailer.plugin' => [[]]], |
||
| 73 | $container->getDefinition('pixelart_swiftmailer_manipulator.mailer.main_mailer.plugin')->getTags() |
||
| 74 | ); |
||
| 75 | |||
| 76 | self::assertSame( |
||
| 77 | '[TESTSYSTEM!]', |
||
| 78 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.main_mailer.prepend_subject') |
||
| 79 | ); |
||
| 80 | |||
| 81 | self::assertSame( |
||
| 82 | 'swiftmailer/prepend_body.txt.twig', |
||
| 83 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.main_mailer.prepend_body') |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @dataProvider getConfigTypes |
||
| 89 | */ |
||
| 90 | public function testManyMailers($type) |
||
| 91 | { |
||
| 92 | $container = $this->loadContainerFromFile('many_mailers', $type); |
||
| 93 | |||
| 94 | // first mailer |
||
| 95 | self::assertSame( |
||
| 96 | ['swiftmailer.first_mailer.plugin' => [[]]], |
||
| 97 | $container->getDefinition('pixelart_swiftmailer_manipulator.mailer.first_mailer.plugin')->getTags() |
||
| 98 | ); |
||
| 99 | |||
| 100 | self::assertSame( |
||
| 101 | '[TESTSYSTEM 1!]', |
||
| 102 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.first_mailer.prepend_subject') |
||
| 103 | ); |
||
| 104 | |||
| 105 | self::assertSame( |
||
| 106 | 'swiftmailer/prepend_body_1.txt.twig', |
||
| 107 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.first_mailer.prepend_body') |
||
| 108 | ); |
||
| 109 | |||
| 110 | // second mailer |
||
| 111 | self::assertSame( |
||
| 112 | ['swiftmailer.secondary_mailer.plugin' => [[]]], |
||
| 113 | $container->getDefinition('pixelart_swiftmailer_manipulator.mailer.secondary_mailer.plugin')->getTags() |
||
| 114 | ); |
||
| 115 | |||
| 116 | self::assertSame( |
||
| 117 | '[TESTSYSTEM 2!]', |
||
| 118 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.secondary_mailer.prepend_subject') |
||
| 119 | ); |
||
| 120 | |||
| 121 | self::assertSame( |
||
| 122 | 'swiftmailer/prepend_body_2.txt.twig', |
||
| 123 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.secondary_mailer.prepend_body') |
||
| 124 | ); |
||
| 125 | |||
| 126 | // third mailer |
||
| 127 | self::assertSame( |
||
| 128 | ['swiftmailer.third_mailer.plugin' => [[]]], |
||
| 129 | $container->getDefinition('pixelart_swiftmailer_manipulator.mailer.third_mailer.plugin')->getTags() |
||
| 130 | ); |
||
| 131 | |||
| 132 | self::assertSame( |
||
| 133 | '[TESTSYSTEM 3!]', |
||
| 134 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.third_mailer.prepend_subject') |
||
| 135 | ); |
||
| 136 | |||
| 137 | self::assertSame( |
||
| 138 | 'swiftmailer/prepend_body_3.txt.twig', |
||
| 139 | $container->getParameter('pixelart_swiftmailer_manipulator.mailer.third_mailer.prepend_body') |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $file |
||
| 145 | * @param string $type |
||
| 146 | * |
||
| 147 | * @return ContainerBuilder |
||
| 148 | */ |
||
| 149 | private function loadContainerFromFile($file, $type) |
||
| 150 | { |
||
| 151 | $container = new ContainerBuilder(); |
||
| 152 | |||
| 153 | $container->setParameter('kernel.debug', false); |
||
| 154 | $container->setParameter('kernel.cache_dir', '/tmp'); |
||
| 155 | |||
| 156 | $container->registerExtension(new PixelartSwiftmailerManipulatorExtension()); |
||
| 157 | $locator = new FileLocator(__DIR__.'/Fixtures/config/'.$type); |
||
| 158 | |||
| 159 | switch ($type) { |
||
| 160 | case 'yml': |
||
| 161 | $loader = new YamlFileLoader($container, $locator); |
||
| 162 | break; |
||
| 163 | |||
| 164 | case 'php': |
||
| 165 | $loader = new PhpFileLoader($container, $locator); |
||
| 166 | break; |
||
| 167 | |||
| 168 | default: |
||
| 169 | throw new \InvalidArgumentException(sprintf('Unexpected loader format "%s"', $type)); |
||
| 170 | } |
||
| 171 | |||
| 172 | $loader->load($file.'.'.$type); |
||
| 173 | |||
| 174 | $container->getCompilerPassConfig()->setOptimizationPasses([]); |
||
| 175 | $container->getCompilerPassConfig()->setRemovingPasses([]); |
||
| 176 | $container->compile(); |
||
| 177 | |||
| 178 | return $container; |
||
| 179 | } |
||
| 180 | } |
||
| 181 |