Completed
Push — master ( 3f65c6...83b473 )
by Vincent
02:48
created

TransactionalTest   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 264
Duplicated Lines 81.06 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 29
c 2
b 1
f 0
lcom 1
cbo 5
dl 214
loc 264
rs 10

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/**
4
 * Copyright 2016 Inneair
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 * @license http://www.apache.org/licenses/LICENSE-2.0.html Apache-2.0
19
 */
20
21
namespace Inneair\TransactionBundle\Test\Annotation;
22
23
use Doctrine\Common\Annotations\AnnotationException;
24
use Doctrine\Common\Annotations\AnnotationReader;
25
use Doctrine\Common\Annotations\AnnotationRegistry;
26
use Doctrine\Common\Annotations\Reader;
27
use Exception;
28
use InvalidArgumentException;
29
use ReflectionClass;
30
use Inneair\TransactionBundle\Annotation\Transactional;
31
use Inneair\TransactionBundle\Test\AbstractTest;
32
use Inneair\TransactionBundle\Test\Annotation\Fixture\UnannotatedClass;
33
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithDefaultOptions;
34
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithInvalidPolicyType;
35
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithInvalidPolicyValue;
36
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithPolicy;
37
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithInvalidNoRollbackExceptionType;
38
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithInvalidNoRollbackExceptionClass;
39
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithNoRollbackExceptions;
40
use Inneair\TransactionBundle\Test\Annotation\Fixture\AnnotatedClassWithUnknownNoRollbackExceptionClass;
41
42
/**
43
 * Class containing test suite for the {@link Transactional} class.
44
 */
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