Passed
Push — master ( 89de50...6aa9d5 )
by DeGracia
46s
created

Test::perfectScore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
use DeGraciaMathieu\Riddler\Formats;
4
use DeGraciaMathieu\Riddler\Password;
5
use DeGraciaMathieu\Riddler\Occurrences;
6
use DeGraciaMathieu\Riddler\Dictionaries;
7
8
class Test extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    /** @test */
11
    public function emptyPassword()
12
    {
13
        $pw = new Password();
14
15
        $str = $pw->generate();
16
17
        $this->assertEmpty($str);
18
    }
19
20
    /** @test */
21 View Code Duplication
    public function letterPasswordOnly()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        $pw = new Password();
24
25
        $pw->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(10));
26
27
        $str = $pw->generate();
28
29
        $this->assertNotEmpty($str);
30
31
        $this->assertRegExp('/[a-z]/', $str);
32
    }
33
34
    /** @test */
35
    public function alphanumericPasswordOnly()
36
    {
37
        $pw = new Password();
38
39
        $pw->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(5));
40
        $pw->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(5));
41
        $pw->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(5));
42
43
        $str = $pw->generate();
44
45
        $this->assertNotEmpty($str);
46
47
        $this->assertEquals(15, mb_strlen($str));
48
49
        $this->assertRegExp('/[a-zA-Z0-9]/', $str);
50
    }
51
52
    /** @test */
53 View Code Duplication
    public function accentedLetterOnly()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $pw = new Password();
56
57
        $accentedLetter = new Dictionaries\AccentedLetter();
58
59
        $pw->addCriteria($accentedLetter, new Occurrences\Strict(5));
60
61
        $str = $pw->generate();
62
63
        $this->assertNotEmpty($str);
64
65
        $this->assertEquals(5, mb_strlen($str));
66
67
        $this->assertRegExp('/[' . implode($accentedLetter->handle()) . ']{5}/', $str);
68
    }
69
70
    /** @test */
71 View Code Duplication
    public function accentedUppercaseLetterOnly()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $pw = new Password();
74
75
        $accentedUppercaseLetter = new Dictionaries\AccentedUppercaseLetter();
76
77
        $pw->addCriteria($accentedUppercaseLetter, new Occurrences\Strict(5));
78
79
        $str = $pw->generate();
80
81
        $this->assertNotEmpty($str);
82
83
        $this->assertEquals(5, mb_strlen($str));
84
85
        $this->assertRegExp('/[' . implode($accentedUppercaseLetter->handle()) . ']{5}/', $str);
86
    }
87
88
    /** @test */
89
    public function specialCharacterOnly()
90
    {
91
        $pw = new Password();
92
93
        $specialCharacter = new Dictionaries\SpecialCharacter();
94
95
        $pw->addCriteria($specialCharacter, new Occurrences\Strict(5));
96
97
        $str = $pw->generate();
98
99
        $this->assertNotEmpty($str);
100
101
        $this->assertEquals(5, mb_strlen($str));
102
103
        $chars = array_map(function ($c) {
104
            return '\\' . $c;
105
        }, $specialCharacter->handle());
106
107
        $this->assertRegExp('/[' . implode($chars) . ']{5}/', $str);
108
    }
109
110
    /** @test */
111
    public function longDigitFormatOnly()
112
    {
113
        $pw = new Password();
114
115
        $pw->addFormat(new Formats\LongDigit());
116
117
        $str = $pw->generate();
118
119
        $this->assertNotEmpty($str);
120
121
        $this->assertRegExp('/\d{30}/', $str);
122
    }
123
124
    /** @test */
125 View Code Duplication
    public function smallAlphanumericFormatOnly()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $pw = new Password();
128
129
        $pw->addFormat(new Formats\SmallAlphanumeric());
130
131
        $str = $pw->generate();
132
133
        $this->assertNotEmpty($str);
134
135
        $this->assertEquals(15, mb_strlen($str));
136
137
        $this->assertRegExp('/[a-zA-Z0-9]/', $str);
138
    }
139
140
    /** @test */
141 View Code Duplication
    public function strongAlphanumericFormatOnly()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $pw = new Password();
144
145
        $pw->addFormat(new Formats\StrongAlphanumeric());
146
147
        $str = $pw->generate();
148
149
        $this->assertNotEmpty($str);
150
151
        $this->assertEquals(30, mb_strlen($str));
152
153
        $this->assertRegExp('/[a-zA-Z0-9]/', $str);
154
    }
155
156
    /** @test */
157
    public function mixedStrongFormatOnly()
158
    {
159
        $pw = new Password();
160
161
        $pw->addFormat(new Formats\MixedStrong());
162
163
        $str = $pw->generate();
164
165
        $this->assertNotEmpty($str);
166
167
        // $this->assertEquals(30, mb_strlen($str));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
168
169
        // $this->assertRegExp('/[a-zA-Z0-9]/', $str);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
170
    }
171
172
    /** @test */
173
    public function mixedComplexFormatOnly()
174
    {
175
        $pw = new Password();
176
177
        $pw->addFormat(new Formats\MixedComplex());
178
179
        $str = $pw->generate();
180
181
        $this->assertNotEmpty($str);
182
183
        // $this->assertEquals(30, mb_strlen($str));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
184
185
        // $this->assertRegExp('/[a-zA-Z0-9]/', $str);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
    }
187
188
189
    /** @test */
190 View Code Duplication
    public function digitPasswordOnly()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $pw = new Password();
193
194
        $pw->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(10));
195
196
        $str = $pw->generate();
197
198
        $this->assertNotEmpty($str);
199
200
        $this->assertRegExp('/\d{10}/', $str);
201
    }
202
203
    /** @test */
204
    public function strictOccurrences()
205
    {
206
        $dictionary = ['a', 'b', 'c', 'd'];
207
208
        $strict = new Occurrences\Strict(5);
209
        $result = $strict->parse($dictionary);
210
211
        $this->assertTrue(is_array($result));
212
        $this->assertEquals(5, count($result));
213
        $this->assertContains($result[0], $dictionary);
214
        $this->assertContains($result[1], $dictionary);
215
        $this->assertContains($result[2], $dictionary);
216
        $this->assertContains($result[3], $dictionary);
217
    }
218
219
    /** @test */
220
    public function betweenOccurrences()
221
    {
222
        $dictionary = ['a', 'b', 'c', 'd'];
223
224
        $strict = new Occurrences\Between(3, 5);
225
        $result = $strict->parse($dictionary);
226
227
        $this->assertTrue(is_array($result));
228
        $this->assertTrue(count($result) >= 3 && count($result) <= 5);
229
        $this->assertContains($result[0], $dictionary);
230
        $this->assertContains($result[1], $dictionary);
231
        $this->assertContains($result[2], $dictionary);
232
    }
233
234
    /** @test */
235
    public function perfectScore()
236
    {
237
        $string = '1a4bcT@Y';
238
239
        $password = new Password();
240
241
        $password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(2));
242
        $password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(3));
243
        $password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(2));
244
        $password->addCriteria(new Dictionaries\SpecialCharacter(), new Occurrences\Strict(1));
245
246
        $this->assertEquals($password->score($string), 100);
247
    }
248
}
249