|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Valkyrja Framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Valkyrja\Test\Assert\Asserters; |
|
15
|
|
|
|
|
16
|
|
|
use Valkyrja\Test\Assert\Assert as Contract; |
|
17
|
|
|
use Valkyrja\Test\Assert\Asserter; |
|
|
|
|
|
|
18
|
|
|
use Valkyrja\Test\Assert\Asserters\Asserter as AbstractAsserter; |
|
19
|
|
|
use Valkyrja\Test\Assert\Asserters\Compare as CompareAsserter; |
|
20
|
|
|
use Valkyrja\Test\Assert\Asserters\Exceptions as ExceptionsAsserter; |
|
21
|
|
|
use Valkyrja\Test\Assert\Asserters\Str as StrAsserter; |
|
22
|
|
|
use Valkyrja\Test\Assert\Compare; |
|
|
|
|
|
|
23
|
|
|
use Valkyrja\Test\Assert\Enums\AsserterName; |
|
24
|
|
|
use Valkyrja\Test\Assert\Enums\ResultType; |
|
25
|
|
|
use Valkyrja\Test\Assert\Exceptions; |
|
|
|
|
|
|
26
|
|
|
use Valkyrja\Test\Assert\Str; |
|
|
|
|
|
|
27
|
|
|
use Valkyrja\Type\Support\Enum; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class Assert. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Melech Mizrachi |
|
33
|
|
|
*/ |
|
34
|
|
|
class Assert extends AbstractAsserter implements Contract |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Asserter instances. |
|
38
|
|
|
* |
|
39
|
|
|
* @var Asserter[] |
|
40
|
|
|
*/ |
|
41
|
|
|
protected array $asserterInstances = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array<string, class-string<Asserter>> $asserters |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
protected array $asserters = [], |
|
48
|
|
|
) { |
|
49
|
|
|
$this->asserters = array_merge( |
|
50
|
|
|
[ |
|
51
|
|
|
AsserterName::compare->name => CompareAsserter::class, |
|
|
|
|
|
|
52
|
|
|
AsserterName::exceptions->name => ExceptionsAsserter::class, |
|
53
|
|
|
AsserterName::string->name => StrAsserter::class, |
|
54
|
|
|
], |
|
55
|
|
|
$asserters |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritDoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function compare(): Compare |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->__call(AsserterName::compare->name, []); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritDoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function exceptions(): Exceptions |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->__call(AsserterName::exceptions->name, []); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritDoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function string(): Str |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->__call(AsserterName::string->name, []); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritDoc |
|
85
|
|
|
*/ |
|
86
|
|
|
public function withAsserters(array $asserters): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->asserters = array_merge($this->asserters, $asserters); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritDoc |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getAssertions(): array |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->getAllAsserterResults(ResultType::assertions); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritDoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getErrors(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->getAllAsserterResults(ResultType::errors); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @inheritDoc |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getSuccesses(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->getAllAsserterResults(ResultType::successes); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritDoc |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getWarnings(): array |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getAllAsserterResults(ResultType::warnings); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inheritDoc |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __get(string $name): mixed |
|
127
|
|
|
{ |
|
128
|
|
|
if (Enum::isValidName(ResultType::class, $name)) { |
|
129
|
|
|
return match ($name) { |
|
130
|
|
|
ResultType::assertions->name => $this->getAssertions(), |
|
|
|
|
|
|
131
|
|
|
ResultType::errors->name => $this->getErrors(), |
|
132
|
|
|
ResultType::successes->name => $this->getSuccesses(), |
|
133
|
|
|
ResultType::warnings->name => $this->getWarnings(), |
|
134
|
|
|
}; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->__call($name, []); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritDoc |
|
142
|
|
|
*/ |
|
143
|
|
|
public function __call(string $name, array $arguments): mixed |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->asserterInstances[$name] ??= new $this->asserters[$name](); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get all the asserters' results by type. |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function getAllAsserterResults(ResultType $type): array |
|
152
|
|
|
{ |
|
153
|
|
|
$results = $this->{$type->name}; |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
foreach ($this->asserterInstances as $asserter) { |
|
156
|
|
|
$results[] = $asserter->{$type->name}; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return array_merge(...$results); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: