-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathAndxTest.php
More file actions
127 lines (108 loc) · 3.97 KB
/
AndxTest.php
File metadata and controls
127 lines (108 loc) · 3.97 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
<?php
declare(strict_types=1);
namespace Arkitect\Tests\Unit\Expressions\Boolean;
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\ClassDescriptionBuilder;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\Boolean\Andx;
use Arkitect\Expression\ForClasses\Extend;
use Arkitect\Expression\ForClasses\Implement;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;
class AndxTest extends TestCase
{
public function test_it_should_return_no_violation_when_empty(): void
{
$and = new Andx();
$classDescription = (new ClassDescriptionBuilder())
->setClassName('My\Class')
->setExtends('My\BaseClass', 10)
->build();
$violations = new Violations();
$and->evaluate($classDescription, $violations, 'because');
self::assertEquals(0, $violations->count());
}
public function test_it_should_pass_the_rule(): void
{
$interface = 'interface';
$class = 'SomeClass';
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[FullyQualifiedClassName::fromString($interface)],
FullyQualifiedClassName::fromString($class),
false,
false,
false,
false,
false
);
$implementConstraint = new Implement($interface);
$extendsConstraint = new Extend($class);
$andConstraint = new Andx($implementConstraint, $extendsConstraint);
$because = 'reasons';
$violations = new Violations();
$andConstraint->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
public function test_it_should_pass_the_rule_when_and_is_empty(): void
{
$interface = 'interface';
$class = 'SomeClass';
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[FullyQualifiedClassName::fromString($interface)],
FullyQualifiedClassName::fromString($class),
false,
false,
false,
false,
false
);
$andConstraint = new Andx();
$because = 'reasons';
$violations = new Violations();
$andConstraint->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
}
public function test_it_should_not_pass_the_rule(): void
{
$interface = 'SomeInterface';
$class = 'SomeClass';
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[FullyQualifiedClassName::fromString($interface)],
null,
false,
false,
false,
false,
false
);
$implementConstraint = new Implement($interface);
$extendsConstraint = new Extend($class);
$andConstraint = new Andx($implementConstraint, $extendsConstraint);
$because = 'reasons';
$violationError = $andConstraint->describe($classDescription, $because)->toString();
$violations = new Violations();
$andConstraint->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());
$this->assertEquals(
"(\nshould implement SomeInterface because reasons\nAND\nshould extend SomeClass because reasons\n) because reasons",
$violationError
);
$this->assertEquals(
"The class 'HappyIsland' violated the expression\n"
."should extend SomeClass\n"
."from the rule\n"
."(\n"
."should implement SomeInterface because reasons\n"
."AND\n"
."should extend SomeClass because reasons\n"
.') because reasons',
$violations->get(0)->getError()
);
}
}