|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Pitchart\Phlunit\Checks; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\Assert; |
|
7
|
|
|
use PHPUnit\Framework\Constraint\LogicalNot; |
|
8
|
|
|
use Pitchart\Phlunit\Checks\Converter\ToDateTime; |
|
9
|
|
|
use Pitchart\Phlunit\Checks\Converter\ToInteger; |
|
10
|
|
|
use Pitchart\Phlunit\Checks\Converter\ToJson; |
|
11
|
|
|
use Pitchart\Phlunit\Checks\Converter\ToXml; |
|
12
|
|
|
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck; |
|
13
|
|
|
use Pitchart\Phlunit\Checks\Mixin\FluentChecks; |
|
14
|
|
|
use Pitchart\Phlunit\Checks\Mixin\TypeCheck; |
|
|
|
|
|
|
15
|
|
|
use Pitchart\Phlunit\Checks\Mixin\WithMessage; |
|
16
|
|
|
use Pitchart\Phlunit\Constraint\String\EndsWith; |
|
17
|
|
|
use Pitchart\Phlunit\Constraint\String\IsBlank; |
|
18
|
|
|
use Pitchart\Phlunit\Constraint\String\IsDigits; |
|
19
|
|
|
use Pitchart\Phlunit\Constraint\String\IsHexadecimal; |
|
20
|
|
|
use Pitchart\Phlunit\Constraint\String\IsLetters; |
|
21
|
|
|
use Pitchart\Phlunit\Constraint\String\StartsWith; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class StringCheck |
|
25
|
|
|
* |
|
26
|
|
|
* @package Pitchart\Phlunit\Checks |
|
27
|
|
|
* |
|
28
|
|
|
* @author Julien VITTE <[email protected]> |
|
29
|
|
|
* |
|
30
|
|
|
* @template-implements FluentCheck<string> |
|
31
|
|
|
*/ |
|
32
|
|
|
class StringCheck implements FluentCheck |
|
33
|
|
|
{ |
|
34
|
|
|
use TypeCheck, FluentChecks, WithMessage, ConstraintCheck, |
|
35
|
|
|
ToDateTime, ToInteger, ToJson, ToXml; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $value; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool |
|
44
|
|
|
*/ |
|
45
|
|
|
private $ignoreCase = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* GenericCheck constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param $value |
|
51
|
|
|
*/ |
|
52
|
97 |
|
public function __construct(string $value) |
|
53
|
|
|
{ |
|
54
|
97 |
|
$this->value = $value; |
|
55
|
97 |
|
} |
|
56
|
|
|
|
|
57
|
34 |
|
public function isEqualTo(string $expected): self |
|
58
|
|
|
{ |
|
59
|
34 |
|
if ($this->ignoreCase) { |
|
60
|
1 |
|
Assert::assertEqualsIgnoringCase($expected, $this->value, $this->message); |
|
61
|
|
|
} else { |
|
62
|
33 |
|
Assert::assertSame($expected, $this->value, $this->message); |
|
63
|
|
|
} |
|
64
|
34 |
|
$this->resetMessage(); |
|
65
|
34 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
public function isNotEqualTo(string $expected): self |
|
69
|
|
|
{ |
|
70
|
3 |
|
if ($this->ignoreCase) { |
|
71
|
1 |
|
Assert::assertNotEqualsIgnoringCase($expected, $this->value, $this->message); |
|
72
|
|
|
} else { |
|
73
|
2 |
|
Assert::assertNotEquals($expected, $this->value, $this->message); |
|
74
|
|
|
} |
|
75
|
3 |
|
$this->resetMessage(); |
|
76
|
3 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
public function isEmpty(): self |
|
80
|
|
|
{ |
|
81
|
2 |
|
Assert::assertEmpty($this->value, $this->message); |
|
82
|
2 |
|
$this->resetMessage(); |
|
83
|
2 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
public function isNotEmpty(): self |
|
87
|
|
|
{ |
|
88
|
2 |
|
Assert::assertNotEmpty($this->value, $this->message); |
|
89
|
2 |
|
$this->resetMessage(); |
|
90
|
2 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
public function isBlank(): self |
|
94
|
|
|
{ |
|
95
|
4 |
|
Assert::assertThat($this->value, new IsBlank(), $this->message); |
|
96
|
4 |
|
$this->resetMessage(); |
|
97
|
4 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function isNotBlank(): self |
|
101
|
|
|
{ |
|
102
|
2 |
|
Assert::assertThat($this->value, new LogicalNot(new IsBlank()), $this->message); |
|
103
|
2 |
|
$this->resetMessage(); |
|
104
|
2 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
public function isGreaterThan(string $expected): self |
|
108
|
|
|
{ |
|
109
|
2 |
|
Assert::assertGreaterThan($expected, $this->value, $this->message); |
|
110
|
2 |
|
$this->resetMessage(); |
|
111
|
2 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
public function isGreaterThanOrEqualTo(string $expected): self |
|
115
|
|
|
{ |
|
116
|
2 |
|
Assert::assertGreaterThanOrEqual($expected, $this->value, $this->message); |
|
117
|
2 |
|
$this->resetMessage(); |
|
118
|
2 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
public function isLessThan(string $expected): self |
|
122
|
|
|
{ |
|
123
|
2 |
|
Assert::assertLessThan($expected, $this->value, $this->message); |
|
124
|
2 |
|
$this->resetMessage(); |
|
125
|
2 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
2 |
|
public function isLessThanOrEqualTo(string $expected): self |
|
129
|
|
|
{ |
|
130
|
2 |
|
Assert::assertLessThanOrEqual($expected, $this->value, $this->message); |
|
131
|
2 |
|
$this->resetMessage(); |
|
132
|
2 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
public function contains(string $expected): self |
|
136
|
|
|
{ |
|
137
|
3 |
|
if ($this->ignoreCase) { |
|
138
|
1 |
|
Assert::assertStringContainsStringIgnoringCase($expected, $this->value, $this->message); |
|
139
|
|
|
} else { |
|
140
|
2 |
|
Assert::assertStringContainsString($expected, $this->value, $this->message); |
|
141
|
|
|
} |
|
142
|
3 |
|
$this->resetMessage(); |
|
143
|
3 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
4 |
|
public function startsWith(string $expected): self |
|
147
|
|
|
{ |
|
148
|
4 |
|
if ($this->ignoreCase) { |
|
149
|
2 |
|
Assert::assertThat($this->value, new StartsWith($expected), $this->message); |
|
150
|
|
|
} else { |
|
151
|
3 |
|
Assert::assertStringStartsWith($expected, $this->value, $this->message); |
|
152
|
|
|
} |
|
153
|
4 |
|
$this->resetMessage(); |
|
154
|
4 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
3 |
|
public function endsWith(string $expected): self |
|
158
|
|
|
{ |
|
159
|
3 |
|
if ($this->ignoreCase) { |
|
160
|
1 |
|
Assert::assertThat($this->value, new EndsWith($expected), $this->message); |
|
161
|
|
|
} else { |
|
162
|
2 |
|
Assert::assertStringEndsWith($expected, $this->value, $this->message); |
|
163
|
|
|
} |
|
164
|
3 |
|
$this->resetMessage(); |
|
165
|
3 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
2 |
|
public function isDigits(): self |
|
169
|
|
|
{ |
|
170
|
2 |
|
Assert::assertThat($this->value, new IsDigits(), $this->message); |
|
171
|
2 |
|
$this->resetMessage(); |
|
172
|
2 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
2 |
|
public function isLetters(): self |
|
176
|
|
|
{ |
|
177
|
2 |
|
Assert::assertThat($this->value, new IsLetters(), $this->message); |
|
178
|
2 |
|
$this->resetMessage(); |
|
179
|
2 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
2 |
|
public function isWhitespaces(): self |
|
183
|
|
|
{ |
|
184
|
2 |
|
return $this->isBlank(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
public function isHexadecimal(): self |
|
188
|
|
|
{ |
|
189
|
1 |
|
Assert::assertThat($this->value, new IsHexadecimal(), $this->message); |
|
190
|
1 |
|
$this->resetMessage(); |
|
191
|
1 |
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
1 |
|
public function matches(string $pattern): self |
|
195
|
|
|
{ |
|
196
|
1 |
|
Assert::assertRegExp($pattern, $this->value, $this->message); |
|
|
|
|
|
|
197
|
1 |
|
$this->resetMessage(); |
|
198
|
1 |
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
3 |
|
public function ignoringCase(): self |
|
202
|
|
|
{ |
|
203
|
3 |
|
$this->ignoreCase = true; |
|
204
|
3 |
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
2 |
|
public function respectingCase(): self |
|
208
|
|
|
{ |
|
209
|
2 |
|
$this->ignoreCase = false; |
|
210
|
2 |
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
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: