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 |
||
45 | class TransactionalTest extends AbstractTest |
||
46 | { |
||
47 | /** |
||
48 | * Annotation reader. |
||
49 | * @var Reader |
||
50 | */ |
||
51 | private $reader; |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | public function setUp() |
||
57 | { |
||
58 | parent::setUp(); |
||
59 | |||
60 | $this->reader = new AnnotationReader(); |
||
61 | AnnotationRegistry::registerLoader('class_exists'); |
||
62 | } |
||
63 | |||
64 | public function testUnannotatedClass() |
||
65 | { |
||
66 | $this->assertNull( |
||
67 | $this->reader->getClassAnnotation(new ReflectionClass(UnannotatedClass::class), Transactional::class) |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | public function testUnannotatedMethod() |
||
72 | { |
||
73 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
74 | $this->assertNull( |
||
75 | $this->reader->getMethodAnnotation($class->getMethod('unannotatedmethod'), Transactional::class) |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | public function testAnnotatedClassWithDefaultOptions() |
||
80 | { |
||
81 | /** @var Transactional $annotation */ |
||
82 | $annotation = $this->reader->getClassAnnotation( |
||
83 | new ReflectionClass(AnnotatedClassWithDefaultOptions::class), |
||
84 | Transactional::class |
||
85 | ); |
||
86 | $this->assertNotNull($annotation); |
||
87 | $this->assertInstanceOf(Transactional::class, $annotation); |
||
88 | $this->assertNull($annotation->getPolicy()); |
||
89 | $this->assertNull($annotation->getNoRollbackExceptions()); |
||
90 | } |
||
91 | |||
92 | public function testAnnotatedClassWithInvalidPolicyType() |
||
93 | { |
||
94 | $hasException = false; |
||
95 | try { |
||
96 | $this->reader->getClassAnnotation( |
||
97 | new ReflectionClass(AnnotatedClassWithInvalidPolicyType::class), |
||
98 | Transactional::class |
||
99 | ); |
||
100 | } catch (AnnotationException $e) { |
||
101 | $hasException = true; |
||
102 | } |
||
103 | $this->assertTrue($hasException); |
||
104 | } |
||
105 | |||
106 | public function testAnnotatedClassWithInvalidPolicyValue() |
||
107 | { |
||
108 | $hasException = false; |
||
109 | try { |
||
110 | $this->reader->getClassAnnotation( |
||
111 | new ReflectionClass(AnnotatedClassWithInvalidPolicyValue::class), |
||
112 | Transactional::class |
||
113 | ); |
||
114 | } catch (AnnotationException $e) { |
||
115 | $hasException = true; |
||
116 | } |
||
117 | $this->assertTrue($hasException); |
||
118 | } |
||
119 | |||
120 | public function testAnnotatedClassWithPolicy() |
||
121 | { |
||
122 | /** @var Transactional $annotation */ |
||
123 | $annotation = $this->reader->getClassAnnotation( |
||
124 | new ReflectionClass(AnnotatedClassWithPolicy::class), |
||
125 | Transactional::class |
||
126 | ); |
||
127 | $this->assertNotNull($annotation); |
||
128 | $this->assertInstanceOf(Transactional::class, $annotation); |
||
129 | $this->assertSame(Transactional::NESTED, $annotation->getPolicy()); |
||
130 | } |
||
131 | |||
132 | public function testAnnotatedClassWithInvalidNoRollbackExceptionType() |
||
133 | { |
||
134 | $hasException = false; |
||
135 | try { |
||
136 | $this->reader->getClassAnnotation( |
||
137 | new ReflectionClass(AnnotatedClassWithInvalidNoRollbackExceptionType::class), |
||
138 | Transactional::class |
||
139 | ); |
||
140 | } catch (AnnotationException $e) { |
||
141 | $hasException = true; |
||
142 | } |
||
143 | $this->assertTrue($hasException); |
||
144 | } |
||
145 | |||
146 | public function testAnnotatedClassWithInvalidNoRollbackExceptionClass() |
||
147 | { |
||
148 | $hasException = false; |
||
149 | try { |
||
150 | $this->reader->getClassAnnotation( |
||
151 | new ReflectionClass(AnnotatedClassWithInvalidNoRollbackExceptionClass::class), |
||
152 | Transactional::class |
||
153 | ); |
||
154 | } catch (AnnotationException $e) { |
||
155 | $hasException = true; |
||
156 | } |
||
157 | $this->assertTrue($hasException); |
||
158 | } |
||
159 | |||
160 | public function testAnnotatedClassWithUnknownNoRollbackExceptionClass() |
||
161 | { |
||
162 | $hasException = false; |
||
163 | try { |
||
164 | $this->reader->getClassAnnotation( |
||
165 | new ReflectionClass(AnnotatedClassWithUnknownNoRollbackExceptionClass::class), |
||
166 | Transactional::class |
||
167 | ); |
||
168 | } catch (AnnotationException $e) { |
||
169 | $hasException = true; |
||
170 | } |
||
171 | $this->assertTrue($hasException); |
||
172 | } |
||
173 | |||
174 | public function testAnnotatedClassWithNoRollbackExceptions() |
||
175 | { |
||
176 | /** @var Transactional $annotation */ |
||
177 | $annotation = $this->reader->getClassAnnotation( |
||
178 | new ReflectionClass(AnnotatedClassWithNoRollbackExceptions::class), |
||
179 | Transactional::class |
||
180 | ); |
||
181 | $this->assertNotNull($annotation); |
||
182 | $this->assertInstanceOf(Transactional::class, $annotation); |
||
183 | $noRollbackExceptions = $annotation->getNoRollbackExceptions(); |
||
184 | $this->assertTrue(is_array($noRollbackExceptions)); |
||
185 | $this->assertCount(2, $noRollbackExceptions); |
||
186 | $this->assertTrue(in_array(Exception::class, $noRollbackExceptions)); |
||
187 | $this->assertTrue(in_array(InvalidArgumentException::class, $noRollbackExceptions)); |
||
188 | } |
||
189 | |||
190 | public function testAnnotatedMethodWithDefaultOptions() |
||
191 | { |
||
192 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
193 | /** @var Transactional $annotation */ |
||
194 | $annotation = $this->reader->getMethodAnnotation( |
||
195 | $class->getMethod('annotatedMethodWithDefaultOptions'), |
||
196 | Transactional::class |
||
197 | ); |
||
198 | $this->assertNotNull($annotation); |
||
199 | $this->assertInstanceOf(Transactional::class, $annotation); |
||
200 | $this->assertNull($annotation->getPolicy()); |
||
201 | $this->assertNull($annotation->getNoRollbackExceptions()); |
||
202 | } |
||
203 | |||
204 | public function testAnnotatedMethodWithInvalidPolicyType() |
||
205 | { |
||
206 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
207 | $hasException = false; |
||
208 | try { |
||
209 | $this->reader->getMethodAnnotation( |
||
210 | $class->getMethod('annotatedMethodWithInvalidPolicyType'), |
||
211 | Transactional::class |
||
212 | ); |
||
213 | } catch (AnnotationException $e) { |
||
214 | $hasException = true; |
||
215 | } |
||
216 | $this->assertTrue($hasException); |
||
217 | } |
||
218 | |||
219 | public function testAnnotatedMethodWithInvalidPolicyValue() |
||
220 | { |
||
221 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
222 | $hasException = false; |
||
223 | try { |
||
224 | $this->reader->getMethodAnnotation( |
||
225 | $class->getMethod('annotatedMethodWithInvalidPolicyValue'), |
||
226 | Transactional::class |
||
227 | ); |
||
228 | } catch (AnnotationException $e) { |
||
229 | $hasException = true; |
||
230 | } |
||
231 | $this->assertTrue($hasException); |
||
232 | } |
||
233 | |||
234 | public function testAnnotatedMethodWithPolicy() |
||
235 | { |
||
236 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
237 | /** @var Transactional $annotation */ |
||
238 | $annotation = $this->reader->getMethodAnnotation( |
||
239 | $class->getMethod('annotatedMethodWithPolicy'), |
||
240 | Transactional::class |
||
241 | ); |
||
242 | $this->assertNotNull($annotation); |
||
243 | $this->assertInstanceOf(Transactional::class, $annotation); |
||
244 | $this->assertSame(Transactional::NESTED, $annotation->getPolicy()); |
||
245 | } |
||
246 | |||
247 | public function testAnnotatedMethodWithInvalidNoRollbackExceptionType() |
||
248 | { |
||
249 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
250 | $hasException = false; |
||
251 | try { |
||
252 | $this->reader->getMethodAnnotation( |
||
253 | $class->getMethod('annotatedMethodWithInvalidNoRollbackExceptionType'), |
||
254 | Transactional::class |
||
255 | ); |
||
256 | } catch (AnnotationException $e) { |
||
257 | $hasException = true; |
||
258 | } |
||
259 | $this->assertTrue($hasException); |
||
260 | } |
||
261 | |||
262 | public function testAnnotatedMethodWithInvalidNoRollbackExceptionClass() |
||
263 | { |
||
264 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
265 | $hasException = false; |
||
266 | try { |
||
267 | $this->reader->getMethodAnnotation( |
||
268 | $class->getMethod('annotatedMethodWithInvalidNoRollbackExceptionClass'), |
||
269 | Transactional::class |
||
270 | ); |
||
271 | } catch (AnnotationException $e) { |
||
272 | $hasException = true; |
||
273 | } |
||
274 | $this->assertTrue($hasException); |
||
275 | } |
||
276 | |||
277 | public function testAnnotatedMethodWithUnknownNoRollbackExceptionClass() |
||
278 | { |
||
279 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
280 | $hasException = false; |
||
281 | try { |
||
282 | $this->reader->getMethodAnnotation( |
||
283 | $class->getMethod('annotatedMethodWithUnknownNoRollbackExceptionClass'), |
||
284 | Transactional::class |
||
285 | ); |
||
286 | } catch (AnnotationException $e) { |
||
287 | $hasException = true; |
||
288 | } |
||
289 | $this->assertTrue($hasException); |
||
290 | } |
||
291 | |||
292 | public function testAnnotatedMethodWithNoRollbackExceptions() |
||
293 | { |
||
294 | $class = new ReflectionClass(AnnotatedClassWithDefaultOptions::class); |
||
295 | /** @var Transactional $annotation */ |
||
296 | $annotation = $this->reader->getMethodAnnotation( |
||
297 | $class->getMethod('annotatedMethodWithNoRollbackExceptions'), |
||
298 | Transactional::class |
||
299 | ); |
||
300 | $this->assertNotNull($annotation); |
||
301 | $this->assertInstanceOf(Transactional::class, $annotation); |
||
302 | $noRollbackExceptions = $annotation->getNoRollbackExceptions(); |
||
303 | $this->assertTrue(is_array($noRollbackExceptions)); |
||
304 | $this->assertCount(2, $noRollbackExceptions); |
||
305 | $this->assertTrue(in_array(Exception::class, $noRollbackExceptions)); |
||
306 | $this->assertTrue(in_array(InvalidArgumentException::class, $noRollbackExceptions)); |
||
307 | } |
||
308 | } |
||
309 |