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 |
||
| 18 | class AirbrakeServiceTest extends \PHPUnit_Framework_TestCase |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var NotifierBuilder |
||
| 22 | */ |
||
| 23 | protected $notifierBuilder; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @inheritDoc |
||
| 27 | */ |
||
| 28 | protected function setUp() |
||
| 29 | { |
||
| 30 | $this->notifierBuilder = new NotifierBuilder; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function testGivenInvalidConfigurationParametersThenAnExceptionWillBeThrown() |
||
| 34 | { |
||
| 35 | $configurationParameters = [ |
||
| 36 | 'projectKey' => ' ', |
||
| 37 | ] + $this->getValidConfigurationParameters(); |
||
| 38 | |||
| 39 | $this->expectException('SM\\AirbrakeBundle\\Exception\\AirbrakeConfigurationException'); |
||
| 40 | $this->expectExceptionMessage('Project ID cannot be empty'); |
||
| 41 | |||
| 42 | $this->createServiceFromConfigurationParameters($configurationParameters); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function testGivenValidConfigurationParametersThenTheServiceWillBeBootedCorrectly() |
||
| 46 | { |
||
| 47 | $configurationParameters = $this->getValidConfigurationParameters(); |
||
| 48 | $airbrakeService = $this->createServiceFromConfigurationParameters($configurationParameters); |
||
| 49 | |||
| 50 | $this->assertNotNull($airbrakeService); |
||
| 51 | $this->assertTrue($airbrakeService instanceof AirbrakeService); |
||
| 52 | |||
| 53 | $this->assertNull($airbrakeService->getErrorHandler()); |
||
| 54 | $this->assertTrue($airbrakeService->getNotifier() instanceof Notifier); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testGivenThatTheErrorHandlerIsRequiredThenTheInstanceWillBeAccessibleOnTheService() |
||
| 58 | { |
||
| 59 | $configurationParameters = [ |
||
| 60 | 'globalErrorAndExceptionHandler' => true, |
||
| 61 | ] + $this->getValidConfigurationParameters(); |
||
| 62 | $aibrakeService = $this->createServiceFromConfigurationParameters($configurationParameters); |
||
| 63 | |||
| 64 | $this->assertNotNull($aibrakeService); |
||
| 65 | $this->assertTrue($aibrakeService instanceof AirbrakeService); |
||
| 66 | |||
| 67 | $this->assertNotNull($aibrakeService->getErrorHandler()); |
||
| 68 | $this->assertTrue($aibrakeService->getErrorHandler() instanceof ErrorHandler); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | protected function getValidConfigurationParameters():array |
||
| 75 | { |
||
| 76 | $configurationParameters = [ |
||
| 77 | 'projectKey' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', |
||
| 78 | 'projectId' => '000000', |
||
| 79 | 'host' => 'api.airbrake.io', |
||
| 80 | 'rootDirectory' => '/var/www/app/', |
||
| 81 | 'httpClient' => 'default', |
||
| 82 | 'ignoredExceptions' => [ |
||
| 83 | 'Symfony\Component\HttpKernel\Exception\HttpException', |
||
| 84 | 'Symfony\Component\Security\Core\Exception\AccessDeniedException', |
||
| 85 | ], |
||
| 86 | 'globalExceptionInstance' => true, |
||
| 87 | 'globalErrorAndExceptionHandler' => false, |
||
| 88 | 'environment' => 'PRD', |
||
| 89 | 'appVersion' => '1.0', |
||
| 90 | ]; |
||
| 91 | |||
| 92 | return $configurationParameters; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testGivenThatTheAirbrakeNotificationEncounteredAnErrorThenTheNotifyMethodWillReturnFalse() |
||
| 96 | { |
||
| 97 | $notifierMock = $this->getMockBuilder('Airbrake\Notifier') |
||
| 98 | ->setMethods(['notify']) |
||
| 99 | ->disableOriginalConstructor() |
||
| 100 | ->getMock(); |
||
| 101 | $notifierMock->expects($this->atLeastOnce())->method('notify')->willReturn([ |
||
| 102 | 'error' => 'This is a mock test' |
||
| 103 | ]); |
||
| 104 | |||
| 105 | $configurationParameters = $this->getValidConfigurationParameters(); |
||
| 106 | $airbrakeService = $this->createServiceFromConfigurationParameters($configurationParameters); |
||
| 107 | |||
| 108 | $airbrakeReflection = new \ReflectionClass($airbrakeService); |
||
| 109 | $notifierProperty = $airbrakeReflection->getProperty('notifier'); |
||
| 110 | $notifierProperty->setAccessible(true); |
||
| 111 | $notifierProperty->setValue($airbrakeService, $notifierMock); |
||
| 112 | |||
| 113 | $this->expectException('SM\AirbrakeBundle\Exception\AirbrakeConnectionException'); |
||
| 114 | $this->expectExceptionMessage('This is a mock test'); |
||
| 115 | |||
| 116 | $airbrakeService->notify(new \Exception); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function testGivenThatTheAirbrakeNotificationDidNotEncounterAnErrorThenTheNotifyMethodWillReturnTrue() |
||
| 120 | { |
||
| 121 | $notifierMock = $this->getMockBuilder('Airbrake\Notifier') |
||
| 122 | ->setMethods(['notify']) |
||
| 123 | ->disableOriginalConstructor() |
||
| 124 | ->getMock(); |
||
| 125 | $notifierMock->expects($this->atLeastOnce())->method('notify')->willReturn([ |
||
| 126 | 'id' => 123456, |
||
| 127 | ]); |
||
| 128 | |||
| 129 | $configurationParameters = $this->getValidConfigurationParameters(); |
||
| 130 | $airbrakeService = $this->createServiceFromConfigurationParameters($configurationParameters); |
||
| 131 | |||
| 132 | $airbrakeReflection = new \ReflectionClass($airbrakeService); |
||
| 133 | $notifierProperty = $airbrakeReflection->getProperty('notifier'); |
||
| 134 | $notifierProperty->setAccessible(true); |
||
| 135 | $notifierProperty->setValue($airbrakeService, $notifierMock); |
||
| 136 | |||
| 137 | $operationValid = $airbrakeService->notify(new \Exception); |
||
| 138 | $this->assertTrue($operationValid); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $configurationParameters |
||
| 143 | * |
||
| 144 | * @return AirbrakeService |
||
| 145 | */ |
||
| 146 | protected function createServiceFromConfigurationParameters($configurationParameters):AirbrakeService |
||
| 147 | { |
||
| 148 | $aibrakeService = new AirbrakeService( |
||
| 149 | $this->notifierBuilder, |
||
| 150 | $configurationParameters['projectKey'], |
||
| 151 | $configurationParameters['projectId'], |
||
| 152 | $configurationParameters['globalExceptionInstance'], |
||
| 153 | $configurationParameters['globalErrorAndExceptionHandler'], |
||
| 154 | $configurationParameters['host'], |
||
| 155 | $configurationParameters['httpClient'], |
||
| 156 | $configurationParameters['rootDirectory'], |
||
| 157 | $configurationParameters['ignoredExceptions'], |
||
| 158 | $configurationParameters['environment'], |
||
| 159 | $configurationParameters['appVersion'] |
||
| 160 | ); |
||
| 161 | |||
| 162 | return $aibrakeService; |
||
| 163 | } |
||
| 164 | } |
||
| 165 |