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 QueueRequeueServiceTest extends PHPUnit_Framework_TestCase |
||
|
|||
17 | { |
||
18 | use QueueCommonServicesTrait; |
||
19 | |||
20 | /** |
||
21 | * @param array $queueMessages |
||
22 | * |
||
23 | * @dataProvider dataRestoreQueues |
||
24 | */ |
||
25 | View Code Duplication | public function testRestoreQueues(array $queueMessages): void |
|
63 | |||
64 | public function dataRestoreQueues(): array |
||
78 | |||
79 | public function testRestoreQueuesFailed(): void |
||
80 | { |
||
81 | $mockQueueService = $this->getMockQueueService( |
||
82 | [ |
||
83 | 'getToRestore', |
||
84 | 'beginTransaction', |
||
85 | 'commit', |
||
86 | 'flush', |
||
87 | 'isTransactionActive', |
||
88 | 'rollback', |
||
89 | ] |
||
90 | ); |
||
91 | $mockQueueService |
||
92 | ->expects($this->once()) |
||
93 | ->method('beginTransaction'); |
||
94 | $mockQueueService |
||
95 | ->expects($this->never()) |
||
96 | ->method('commit'); |
||
97 | $mockQueueService |
||
98 | ->expects($this->never()) |
||
99 | ->method('flush'); |
||
100 | $mockQueueService |
||
101 | ->expects($this->once()) |
||
102 | ->method('rollback'); |
||
103 | $mockQueueService |
||
104 | ->expects($this->once()) |
||
105 | ->method('getToRestore') |
||
106 | ->will($this->throwException(new \Exception('Something broken'))); |
||
107 | $queueRequeueService = $this->createService($this->getMockPublisherFactory(), $mockQueueService); |
||
108 | |||
109 | $this->assertFalse($queueRequeueService->restoreQueues(5)); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param QueueEntityInterface $queue |
||
114 | * |
||
115 | * @dataProvider dataRequeue |
||
116 | */ |
||
117 | public function testRequeue(QueueEntityInterface $queue): void |
||
131 | |||
132 | public function dataRequeue(): array |
||
142 | |||
143 | protected function getQueueEntity( |
||
151 | |||
152 | protected function createService( |
||
162 | } |
||
163 |