-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathViolationsTest.php
More file actions
281 lines (235 loc) · 8.05 KB
/
ViolationsTest.php
File metadata and controls
281 lines (235 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
declare(strict_types=1);
namespace Arkitect\Tests\Unit\Rules;
use Arkitect\Exceptions\IndexNotFoundException;
use Arkitect\Rules\Violation;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;
class ViolationsTest extends TestCase
{
private Violations $violationStore;
private Violation $violation;
protected function setUp(): void
{
$this->violationStore = new Violations();
$this->violation = new Violation(
'App\Controller\ProductController',
'should implement ContainerInterface'
);
$this->violationStore->add($this->violation);
}
public function test_add_elements_to_store_and_get_it(): void
{
self::assertEquals($this->violation, $this->violationStore->get(0));
}
public function test_add_elements_to_store_and_cant_get_it_if_index_not_valid(): void
{
$this->expectException(IndexNotFoundException::class);
$this->expectExceptionMessage('Index not found 1111');
self::assertEquals('', $this->violationStore->get(1111));
}
public function test_count(): void
{
$violation = new Violation(
'App\Controller\Shop',
'should have name end with Controller'
);
$this->violationStore->add($violation);
self::assertEquals(2, $this->violationStore->count());
}
public function test_get_iterable(): void
{
$violation = new Violation(
'App\Controller\Shop',
'should have name end with Controller'
);
$this->violationStore->add($violation);
$iterable = $this->violationStore->getIterator();
self::assertEquals([
$this->violation,
$violation,
], iterator_to_array($iterable));
}
public function test_get_array(): void
{
$violation1 = new Violation(
'App\Controller\Shop',
'should have name end with Controller'
);
$violation2 = new Violation(
'App\Controller\Shop',
'should implement AbstractController'
);
$this->violationStore->add($violation1);
$this->violationStore->add($violation2);
self::assertEquals([
$this->violation,
$violation1,
$violation2,
], $this->violationStore->toArray());
}
public function test_remove_violations_from_violations(): void
{
$violation1 = new Violation(
'App\Controller\Shop',
'should have name end with Controller'
);
$this->violationStore->add($violation1);
$violation2 = new Violation(
'App\Controller\Shop',
'should implement AbstractController'
);
$this->violationStore->add($violation2);
self::assertCount(3, $this->violationStore->toArray());
$violationsBaseline = new Violations();
$violationsBaseline->add($this->violation);
$this->violationStore->remove($violationsBaseline);
self::assertCount(2, $this->violationStore->toArray());
self::assertEquals([
$violation1,
$violation2,
], $this->violationStore->toArray());
}
public function test_sort(): void
{
$violationStore = new Violations();
$violation1 = new Violation(
'App\Controller\Shop',
'AAA',
20
);
$violation2 = new Violation(
'App\Controller\Shop',
'BBB',
10
);
$violation3 = new Violation(
'App\Controller\Shop',
'AAA',
10
);
$violation4 = new Violation(
'App\Controller\Abc',
'CCC',
30
);
$violationStore->add($violation1);
$violationStore->add($violation2);
$violationStore->add($violation3);
$violationStore->add($violation4);
self::assertEquals([
$violation1,
$violation2,
$violation3,
$violation4,
], $violationStore->toArray());
$violationStore->sort();
self::assertSame([
$violation4, // fqcn is most important
$violation3, // then line number
$violation2, // then error message
$violation1,
], $violationStore->toArray());
}
public function test_remove_violations_matches_when_rule_description_changes(): void
{
$violations = new Violations();
$violations->add(new Violation(
'App\Foo',
'depends on App\Bar, but should depend only on classes in one of these namespaces: App\Domain, App\Shared',
10
));
$baseline = new Violations();
$baseline->add(new Violation(
'App\Foo',
'depends on App\Bar, but should depend only on classes in one of these namespaces: App\Domain',
10
));
$violations->remove($baseline);
self::assertCount(0, $violations);
}
public function test_remove_violations_matches_when_rule_description_changes_ignore_linenumber(): void
{
$violations = new Violations();
$violations->add(new Violation(
'App\Foo',
'depends on App\Bar, but should depend only on classes in one of these namespaces: App\Domain, App\Shared',
15
));
$baseline = new Violations();
$baseline->add(new Violation(
'App\Foo',
'depends on App\Bar, but should depend only on classes in one of these namespaces: App\Domain',
10
));
$violations->remove($baseline, true);
self::assertCount(0, $violations);
}
public function test_remove_violations_does_not_match_different_dependency(): void
{
$violations = new Violations();
$violations->add(new Violation(
'App\Foo',
'depends on App\Baz, but should depend only on classes in one of these namespaces: App\Domain',
10
));
$baseline = new Violations();
$baseline->add(new Violation(
'App\Foo',
'depends on App\Bar, but should depend only on classes in one of these namespaces: App\Domain',
10
));
$violations->remove($baseline);
self::assertCount(1, $violations);
}
public function test_remove_violations_still_works_for_self_explanatory_messages(): void
{
$violations = new Violations();
$violations->add(new Violation(
'App\Foo',
'should be final because we want immutability'
));
$baseline = new Violations();
$baseline->add(new Violation(
'App\Foo',
'should be final because we want immutability'
));
$violations->remove($baseline);
self::assertCount(0, $violations);
}
public function test_remove_violations_from_violations_ignore_linenumber(): void
{
$violation1 = new Violation(
'App\Controller\Shop',
'should have name end with Controller',
42
);
$this->violationStore->add($violation1);
$violation2 = new Violation(
'App\Controller\Shop',
'should implement AbstractController',
21
);
$this->violationStore->add($violation2);
$violation3 = new Violation(
'App\Controller\Shop',
'should have name end with Controller',
5
);
$this->violationStore->add($violation3);
self::assertCount(4, $this->violationStore->toArray());
$violationsBaseline = new Violations();
$violationsBaseline->add(new Violation(
'App\Controller\Shop',
'should have name end with Controller',
21
));
$this->violationStore->remove($violationsBaseline, true);
self::assertCount(3, $this->violationStore->toArray());
self::assertEquals([
$this->violation,
$violation2,
$violation3,
], $this->violationStore->toArray());
}
}