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 |
||
| 15 | class SMAirbrakeExtensionTest extends \PHPUnit_Framework_TestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var SMAirbrakeExtension |
||
| 19 | */ |
||
| 20 | protected $extension; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @inheritDoc |
||
| 24 | */ |
||
| 25 | protected function setUp() |
||
| 26 | { |
||
| 27 | $this->extension = new SMAirbrakeExtension; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function testGivenThatTheExtensionIsLoadedThenTheConfigurationParametersWillBeExposed() |
||
| 31 | { |
||
| 32 | $configs = $this->getValidConfigurationParameters(); |
||
| 33 | $containerBuilder = $this->getContainerMock(); |
||
| 34 | |||
| 35 | $containerBuilder->expects($this->atLeastOnce())->method('setParameter'); |
||
| 36 | $containerBuilder->expects($this->atLeastOnce())->method('getParameter')->with('kernel.root_dir')->willReturn('/var/www/symfony/app'); |
||
| 37 | |||
| 38 | $this->extension->load($configs, $containerBuilder); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testGivenThatNoConfigurationIsMadeThenTheConfigurationParametersAreStillExposedUsingTheirDefaultValues() |
||
| 42 | { |
||
| 43 | $configs = []; |
||
| 44 | $containerBuilder = $this->getContainerMock(); |
||
| 45 | |||
| 46 | $containerBuilder->expects($this->atLeastOnce())->method('setParameter'); |
||
| 47 | $containerBuilder->expects($this->atLeastOnce())->method('getParameter')->with('kernel.root_dir')->willReturn(__FILE__); |
||
| 48 | |||
| 49 | $this->extension->load($configs, $containerBuilder); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testGivenNoConfigurationAndHavingAnAppVersionStoredInTheContainerFromEnvironmentalVariablesOrOtherwiseThenTheContainerParametersWillBePopulatedWithTheCustomValue() |
||
| 53 | { |
||
| 54 | $configs = [ |
||
| 55 | 'sm_airbrake' => [ |
||
| 56 | 'root_directory' => '/var/www/symfony/app', |
||
| 57 | 'app_version' => '1.0RC', |
||
| 58 | ] |
||
| 59 | ]; |
||
| 60 | $containerBuilder = $this->getContainerMock(); |
||
| 61 | |||
| 62 | $containerBuilder->expects($this->atLeastOnce())->method('setParameter'); |
||
| 63 | $containerBuilder->expects($this->atLeastOnce())->method('hasParameter')->with('app.environment')->willReturn(true); |
||
| 64 | $containerBuilder->expects($this->atLeastOnce())->method('getParameter')->with('app.environment')->willReturn('TST'); |
||
| 65 | |||
| 66 | $this->extension->load($configs, $containerBuilder); |
||
| 67 | } |
||
| 68 | |||
| 69 | private function getContainerMock(): \PHPUnit_Framework_MockObject_MockObject |
||
| 70 | { |
||
| 71 | return $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') |
||
| 72 | ->disableOriginalConstructor() |
||
| 73 | ->setMethods(['setParameter', 'getParameter', 'hasParameter']) |
||
| 74 | ->getMock(); |
||
| 75 | } |
||
| 76 | |||
| 77 | private function getValidConfigurationParameters(): array |
||
| 78 | { |
||
| 79 | return [ |
||
| 80 | 'sm_airbrake' => [ |
||
| 81 | 'global_exception_instance' => true, |
||
| 82 | 'global_error_and_exception_handler' => false, |
||
| 83 | 'ignored_exceptions' => [ |
||
| 84 | 'Symfony\Component\HttpKernel\Exception\HttpException', |
||
| 85 | 'Symfony\Component\Security\Core\Exception\AccessDeniedException', |
||
| 86 | ], |
||
| 87 | 'host' => 'api.airbrake.io', |
||
| 88 | 'http_client' => 'default', |
||
| 89 | 'project_id' => '000000', |
||
| 90 | 'project_key' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', |
||
| 91 | ], |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | } |
||
| 95 |