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 |
||
| 16 | class QueueRepublishServiceTest extends PHPUnit_Framework_TestCase |
||
|
|
|||
| 17 | { |
||
| 18 | use QueueCommonServicesTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param array $queueMessages |
||
| 22 | * |
||
| 23 | * @dataProvider dataRestoreQueues |
||
| 24 | */ |
||
| 25 | public function testRestoreQueues(array $queueMessages): void |
||
| 26 | { |
||
| 27 | $mockQueueService = $this->getMockQueueService( |
||
| 28 | [ |
||
| 29 | 'getToRepublish', |
||
| 30 | 'beginTransaction', |
||
| 31 | 'commit', |
||
| 32 | 'flush', |
||
| 33 | 'isTransactionActive', |
||
| 34 | ] |
||
| 35 | ); |
||
| 36 | $mockQueueService |
||
| 37 | ->expects($this->once()) |
||
| 38 | ->method('beginTransaction'); |
||
| 39 | $mockQueueService |
||
| 40 | ->expects($this->once()) |
||
| 41 | ->method('commit'); |
||
| 42 | $mockQueueService |
||
| 43 | ->expects($this->once()) |
||
| 44 | ->method('flush'); |
||
| 45 | $mockQueueService |
||
| 46 | ->expects($this->once()) |
||
| 47 | ->method('getToRepublish') |
||
| 48 | ->willReturn($queueMessages); |
||
| 49 | $mockPublisherFactory = $this->getMockPublisherFactory(['republish', 'releaseAll']); |
||
| 50 | $mockPublisherFactory |
||
| 51 | ->expects($this->exactly(count($queueMessages))) |
||
| 52 | ->method('republish'); |
||
| 53 | $mockPublisherFactory |
||
| 54 | ->expects($this->once()) |
||
| 55 | ->method('releaseAll'); |
||
| 56 | |||
| 57 | $queueRepublishService = $this->createService($mockPublisherFactory, $mockQueueService); |
||
| 58 | |||
| 59 | $this->assertTrue($queueRepublishService->republishQueues(5)); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @throws \Exception |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function dataRestoreQueues(): array |
|
| 68 | { |
||
| 69 | $queueEntity = $this->getQueueEntity(); |
||
| 70 | $queueEntity2 = $this->getQueueEntity(); |
||
| 71 | Reflection::setProtectedProperty($queueEntity, 'id', 1); |
||
| 72 | Reflection::setProtectedProperty($queueEntity2, 'id', 2); |
||
| 73 | |||
| 74 | return [ |
||
| 75 | [ |
||
| 76 | [ |
||
| 77 | $queueEntity, |
||
| 78 | $queueEntity2, |
||
| 79 | ], |
||
| 80 | ], |
||
| 81 | ]; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param array $queueMessages |
||
| 86 | * |
||
| 87 | * @dataProvider dataBatchRestoreQueues |
||
| 88 | */ |
||
| 89 | public function testBatchRestoreQueues(array $queueMessages): void |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @throws \Exception |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function dataBatchRestoreQueues(): array |
||
| 151 | |||
| 152 | public function testRestoreQueuesFailed(): void |
||
| 153 | { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param array $queueMessages |
||
| 191 | * |
||
| 192 | * @dataProvider dataRestoreQueuesFailedOnPublisherReleaseFailed |
||
| 193 | */ |
||
| 194 | public function testRestoreQueuesFailedOnPublisherReleaseFailed(array $queueMessages): void |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @throws \Exception |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function dataRestoreQueuesFailedOnPublisherReleaseFailed(): array |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $jobName |
||
| 262 | * @param string $exchange |
||
| 263 | * @param string $queueName |
||
| 264 | * @param array $data |
||
| 265 | * |
||
| 266 | * @throws \Exception |
||
| 267 | * |
||
| 268 | * @return QueueEntity |
||
| 269 | */ |
||
| 270 | protected function getQueueEntity( |
||
| 282 | |||
| 283 | protected function createService( |
||
| 293 | } |
||
| 294 |