-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneratorCheck.php
244 lines (215 loc) · 5.76 KB
/
GeneratorCheck.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the phpcheck package.
*
* ©Marlin Forbes <marlinf@datashaman.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Datashaman\PHPCheck\Checks;
use DateTime;
class GeneratorCheck
{
/**
* @param string $char {@gen characters()}
*/
public function checkCharacters(string $char): bool
{
$ord = \mb_ord($char);
return \mb_strlen($char) === 1
&& $ord >= \Datashaman\PHPCheck\UNICODE_MIN
&& $ord <= \Datashaman\PHPCheck\UNICODE_MAX
&& !\array_filter(
\Datashaman\PHPCheck\UNICODE_EXCLUDE,
function ($interval) use ($ord) {
return $ord >= $interval[0] && $ord <= $interval[1];
}
);
}
public function checkStrings(string $string): bool
{
return \mb_strlen($string) <= 30;
}
/**
* @param string $string {@gen ascii()}
*/
public function checkAscii(string $string): bool
{
return \mb_strlen($string) <= 30
&& (bool) \array_filter(
$this->mbSplit($string),
function ($character) {
$ord = \mb_ord($character);
return $ord >= 0 && $ord <= 0x7F;
}
);
}
/**
* @coverTable "Values" [[true, 49], [false, 49]]
* @maxSuccess 10000
* @tabulate "Values" [$bin]
*/
public function checkBooleans(bool $bin): bool
{
return true;
}
/**
* @param bool $bin {@gen booleans(75)}
*
* @coverTable "Values" [[true, 74], [false, 24]]
* @maxSuccess 10000
* @tabulate "Values" [$bin]
*/
public function checkBooleansWithPercentage(bool $bin): bool
{
return true;
}
/**
* @param string $char {@gen characters(32, 126)}
*/
public function checkCharactersWithNumbers(string $char): bool
{
$ord = \mb_ord($char);
return $ord >= 32 && $ord <= 126;
}
/**
* @param string $char {@gen characters(" ", "~")}
*/
public function checkCharactersWithStrings(string $char): bool
{
return $char >= ' ' && $char <= '~';
}
/**
* @param int $int {@gen elements([1,2,3])}
*/
public function checkChoose(int $int): bool
{
return $int >= 1 && $int <= 3;
}
/**
* @param DateTime $value {@gen dates()}
*/
public function checkDates(DateTime $value): bool
{
return $value->format('H:i:s') === '00:00:00';
}
/**
* @param DateTime $value {@gen datetimes()}
*/
public function checkNaiveDateTimes(DateTime $value): bool
{
return $value->format('H:i:s') !== '00:00:00'
&& $value->getOffset() === 0;
}
/**
* @param DateTime $value {@gen datetimes("0001-01-01", "9999-12-31", timezones())}
*/
public function checkDateTimesWithTimezones(DateTime $value): bool
{
return $value->format('H:i:s') !== '00:00:00'
&& $value->getTimezone() !== null;
}
/**
* @maxSuccess 5
*/
public function checkMaxSuccess(): bool
{
static $checks = 0;
$checks++;
return $checks <= 5;
}
/**
* @param float $float {@gen floats(0, 5)}
*/
public function checkFloats(float $float): bool
{
return $float >= 0 && $float <= 5;
}
/**
* @param array $array {@gen listOf(choose(0, 10))}
*/
public function checkListOfInt(array $array): bool
{
return !\count($array)
|| (bool) \array_filter(
$array,
function ($item) {
return \is_int($item) && $item >= 0 && $item <= 10;
}
);
}
/**
* @param mixed $value {@gen oneof([choose(0, 10), choose("a", "z")])}
*/
public function checkOneof($value): bool
{
return $value >= 'a' && $value <= 'z'
|| $value >= 0 && $value <= 10;
}
/**
* @param array $list {@gen vectorOf(5, choose(0, 10))}
*/
public function checkVectorOfInts(array $list): bool
{
return \count($list) === 5
&& (bool) \array_filter(
$list,
function ($item) {
return \is_int($item) && $item >= 0 && $item <= 10;
}
);
}
/**
* @param string $str {@gen faker("email")}
*/
public function checkFakerWithoutArgs(string $str): bool
{
return \filter_var($str, \FILTER_VALIDATE_EMAIL) !== false;
}
/**
* @param int $int {@gen faker("numberBetween", 5, 5)}
*/
public function checkFakerWithArgs(int $int): bool
{
return $int === 5;
}
/**
* @param int $int {@gen variant("123", choose(0, 1000000))}
*/
public function checkVariant(int $int): bool
{
return $int === 857674;
}
/**
* @param string $zone {@gen timezones()}
*/
public function checkTimezones(string $zone): bool
{
return \in_array($zone, \timezone_identifiers_list());
}
/**
* @param int $i {@gen choose(1, 5)}
*
* @coverTable "Values" [[1, 18], [2, 18], [3, 18], [4, 18], [5, 18]]
* @maxSuccess 10000
* @tabulate "Values" [$i]
*/
public function checkTabulate(int $i): bool
{
return $i >= 1 && $i <= 5;
}
protected function mbSplit(string $str)
{
if (($arr = \preg_split('/(?<!^)(?!$)/u', $str)) !== false) {
return \array_filter(
$arr,
function ($char) {
return $char !== '';
}
);
}
return [];
}
}