1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
use DeGraciaMathieu\Riddler\Formats;
|
4
|
|
|
use DeGraciaMathieu\Riddler\Criteria;
|
5
|
|
|
use DeGraciaMathieu\Riddler\Password;
|
6
|
|
|
use DeGraciaMathieu\Riddler\Occurrences;
|
7
|
|
|
use DeGraciaMathieu\Riddler\Dictionaries;
|
8
|
|
|
|
9
|
|
|
class Test extends \PHPUnit\Framework\TestCase
|
|
|
|
|
10
|
|
|
{
|
11
|
|
|
/** @test */
|
12
|
|
|
public function emptyPassword()
|
13
|
|
|
{
|
14
|
|
|
$pw = new Password();
|
15
|
|
|
|
16
|
|
|
$str = $pw->generate();
|
17
|
|
|
|
18
|
|
|
$this->assertEmpty($str);
|
19
|
|
|
}
|
20
|
|
|
|
21
|
|
|
/** @test */
|
22
|
|
View Code Duplication |
public function letterPasswordOnly()
|
|
|
|
|
23
|
|
|
{
|
24
|
|
|
$pw = new Password();
|
25
|
|
|
|
26
|
|
|
$pw->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(10));
|
27
|
|
|
|
28
|
|
|
$str = $pw->generate();
|
29
|
|
|
|
30
|
|
|
$this->assertNotEmpty($str);
|
31
|
|
|
|
32
|
|
|
$this->assertRegExp('/[a-z]/', $str);
|
33
|
|
|
}
|
34
|
|
|
|
35
|
|
|
/** @test */
|
36
|
|
|
public function alphanumericPasswordOnly()
|
37
|
|
|
{
|
38
|
|
|
$pw = new Password();
|
39
|
|
|
|
40
|
|
|
$pw->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(5));
|
41
|
|
|
$pw->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(5));
|
42
|
|
|
$pw->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(5));
|
43
|
|
|
|
44
|
|
|
$str = $pw->generate();
|
45
|
|
|
|
46
|
|
|
$this->assertNotEmpty($str);
|
47
|
|
|
|
48
|
|
|
$this->assertEquals(15, mb_strlen($str));
|
49
|
|
|
|
50
|
|
|
$this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
/** @test */
|
54
|
|
View Code Duplication |
public function accentedLetterOnly()
|
|
|
|
|
55
|
|
|
{
|
56
|
|
|
$pw = new Password();
|
57
|
|
|
|
58
|
|
|
$accentedLetter = new Dictionaries\AccentedLetter();
|
59
|
|
|
|
60
|
|
|
$pw->addCriteria($accentedLetter, new Occurrences\Strict(5));
|
61
|
|
|
|
62
|
|
|
$str = $pw->generate();
|
63
|
|
|
|
64
|
|
|
$this->assertNotEmpty($str);
|
65
|
|
|
|
66
|
|
|
$this->assertEquals(5, mb_strlen($str));
|
67
|
|
|
|
68
|
|
|
$this->assertRegExp('/[' . implode($accentedLetter->handle()) . ']{5}/', $str);
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
/** @test */
|
72
|
|
View Code Duplication |
public function accentedUppercaseLetterOnly()
|
|
|
|
|
73
|
|
|
{
|
74
|
|
|
$pw = new Password();
|
75
|
|
|
|
76
|
|
|
$accentedUppercaseLetter = new Dictionaries\AccentedUppercaseLetter();
|
77
|
|
|
|
78
|
|
|
$pw->addCriteria($accentedUppercaseLetter, new Occurrences\Strict(5));
|
79
|
|
|
|
80
|
|
|
$str = $pw->generate();
|
81
|
|
|
|
82
|
|
|
$this->assertNotEmpty($str);
|
83
|
|
|
|
84
|
|
|
$this->assertEquals(5, mb_strlen($str));
|
85
|
|
|
|
86
|
|
|
$this->assertRegExp('/[' . implode($accentedUppercaseLetter->handle()) . ']{5}/', $str);
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
/** @test */
|
90
|
|
|
public function specialCharacterOnly()
|
91
|
|
|
{
|
92
|
|
|
$pw = new Password();
|
93
|
|
|
|
94
|
|
|
$specialCharacter = new Dictionaries\SpecialCharacter();
|
95
|
|
|
|
96
|
|
|
$pw->addCriteria($specialCharacter, new Occurrences\Strict(5));
|
97
|
|
|
|
98
|
|
|
$str = $pw->generate();
|
99
|
|
|
|
100
|
|
|
$this->assertNotEmpty($str);
|
101
|
|
|
|
102
|
|
|
$this->assertEquals(5, mb_strlen($str));
|
103
|
|
|
|
104
|
|
|
$chars = array_map(function ($c) {
|
105
|
|
|
return '\\' . $c;
|
106
|
|
|
}, $specialCharacter->handle());
|
107
|
|
|
|
108
|
|
|
$this->assertRegExp('/[' . implode($chars) . ']{5}/', $str);
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
/** @test */
|
112
|
|
|
public function longDigitFormatOnly()
|
113
|
|
|
{
|
114
|
|
|
$pw = new Password();
|
115
|
|
|
|
116
|
|
|
$pw->addFormat(new Formats\LongDigit());
|
117
|
|
|
|
118
|
|
|
$str = $pw->generate();
|
119
|
|
|
|
120
|
|
|
$this->assertNotEmpty($str);
|
121
|
|
|
|
122
|
|
|
$this->assertRegExp('/\d{30}/', $str);
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
/** @test */
|
126
|
|
View Code Duplication |
public function smallAlphanumericFormatOnly()
|
|
|
|
|
127
|
|
|
{
|
128
|
|
|
$pw = new Password();
|
129
|
|
|
|
130
|
|
|
$pw->addFormat(new Formats\SmallAlphanumeric());
|
131
|
|
|
|
132
|
|
|
$str = $pw->generate();
|
133
|
|
|
|
134
|
|
|
$this->assertNotEmpty($str);
|
135
|
|
|
|
136
|
|
|
$this->assertEquals(15, mb_strlen($str));
|
137
|
|
|
|
138
|
|
|
$this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
/** @test */
|
142
|
|
View Code Duplication |
public function strongAlphanumericFormatOnly()
|
|
|
|
|
143
|
|
|
{
|
144
|
|
|
$pw = new Password();
|
145
|
|
|
|
146
|
|
|
$pw->addFormat(new Formats\StrongAlphanumeric());
|
147
|
|
|
|
148
|
|
|
$str = $pw->generate();
|
149
|
|
|
|
150
|
|
|
$this->assertNotEmpty($str);
|
151
|
|
|
|
152
|
|
|
$this->assertEquals(30, mb_strlen($str));
|
153
|
|
|
|
154
|
|
|
$this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
155
|
|
|
}
|
156
|
|
|
|
157
|
|
|
/** @test */
|
158
|
|
|
public function mixedStrongFormatOnly()
|
159
|
|
|
{
|
160
|
|
|
$pw = new Password();
|
161
|
|
|
|
162
|
|
|
$pw->addFormat(new Formats\MixedStrong());
|
163
|
|
|
|
164
|
|
|
$str = $pw->generate();
|
165
|
|
|
|
166
|
|
|
$this->assertNotEmpty($str);
|
167
|
|
|
|
168
|
|
|
// $this->assertEquals(30, mb_strlen($str));
|
|
|
|
|
169
|
|
|
|
170
|
|
|
// $this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
|
|
|
|
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
/** @test */
|
174
|
|
|
public function mixedComplexFormatOnly()
|
175
|
|
|
{
|
176
|
|
|
$pw = new Password();
|
177
|
|
|
|
178
|
|
|
$pw->addFormat(new Formats\MixedComplex());
|
179
|
|
|
|
180
|
|
|
$str = $pw->generate();
|
181
|
|
|
|
182
|
|
|
$this->assertNotEmpty($str);
|
183
|
|
|
|
184
|
|
|
// $this->assertEquals(30, mb_strlen($str));
|
|
|
|
|
185
|
|
|
|
186
|
|
|
// $this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
|
|
|
|
187
|
|
|
}
|
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** @test */
|
191
|
|
View Code Duplication |
public function digitPasswordOnly()
|
|
|
|
|
192
|
|
|
{
|
193
|
|
|
$pw = new Password();
|
194
|
|
|
|
195
|
|
|
$pw->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(10));
|
196
|
|
|
|
197
|
|
|
$str = $pw->generate();
|
198
|
|
|
|
199
|
|
|
$this->assertNotEmpty($str);
|
200
|
|
|
|
201
|
|
|
$this->assertRegExp('/\d{10}/', $str);
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
/** @test */
|
205
|
|
|
public function strictOccurrences()
|
206
|
|
|
{
|
207
|
|
|
$dictionary = new Dictionaries\Letter();
|
208
|
|
|
|
209
|
|
|
$occurrence = new Occurrences\Strict(5);
|
210
|
|
|
|
211
|
|
|
$criteria = new Criteria($dictionary, $occurrence);
|
212
|
|
|
|
213
|
|
|
$result = $criteria->build();
|
214
|
|
|
|
215
|
|
|
$this->assertTrue(is_array($result));
|
216
|
|
|
$this->assertEquals(5, count($result));
|
217
|
|
|
$this->assertContains($result[0], $dictionary->handle());
|
218
|
|
|
$this->assertContains($result[1], $dictionary->handle());
|
219
|
|
|
$this->assertContains($result[2], $dictionary->handle());
|
220
|
|
|
$this->assertContains($result[3], $dictionary->handle());
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
/** @test */
|
224
|
|
|
public function betweenOccurrences()
|
225
|
|
|
{
|
226
|
|
|
$dictionary = new Dictionaries\Letter();
|
227
|
|
|
|
228
|
|
|
$occurrence = new Occurrences\Between(3, 5);
|
229
|
|
|
|
230
|
|
|
$criteria = new Criteria($dictionary, $occurrence);
|
231
|
|
|
|
232
|
|
|
$result = $criteria->build();
|
233
|
|
|
|
234
|
|
|
$this->assertTrue(is_array($result));
|
235
|
|
|
$this->assertTrue(count($result) >= 3 && count($result) <= 5);
|
236
|
|
|
$this->assertContains($result[0], $dictionary->handle());
|
237
|
|
|
$this->assertContains($result[1], $dictionary->handle());
|
238
|
|
|
$this->assertContains($result[2], $dictionary->handle());
|
239
|
|
|
}
|
240
|
|
|
|
241
|
|
|
/** @test */
|
242
|
|
|
public function perfectScore()
|
243
|
|
|
{
|
244
|
|
|
$string = '1a4bcT@Y';
|
245
|
|
|
|
246
|
|
|
$password = new Password();
|
247
|
|
|
|
248
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(2));
|
249
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(3));
|
250
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(2));
|
251
|
|
|
$password->addCriteria(new Dictionaries\SpecialCharacter(), new Occurrences\Strict(1));
|
252
|
|
|
|
253
|
|
|
$this->assertEquals($password->score($string), 100);
|
254
|
|
|
}
|
255
|
|
|
}
|
256
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.