Passed
Branch moving_tests_around (1c37ee)
by Eduardo Gulias
04:42 queued 01:17
created

RFCValidationTest::getInvalidEmails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 51
rs 9.4109
cc 1
eloc 48
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Egulias\Tests\EmailValidator\Validation;
4
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Validation\RFCValidation;
7
use Egulias\EmailValidator\Exception\AtextAfterCFWS;
8
use Egulias\EmailValidator\Exception\ConsecutiveAt;
9
use Egulias\EmailValidator\Exception\ConsecutiveDot;
10
use Egulias\EmailValidator\Exception\CRNoLF;
11
use Egulias\EmailValidator\Exception\DomainHyphened;
12
use Egulias\EmailValidator\Exception\DotAtEnd;
13
use Egulias\EmailValidator\Exception\DotAtStart;
14
use Egulias\EmailValidator\Exception\ExpectingATEXT;
15
use Egulias\EmailValidator\Exception\ExpectingDTEXT;
16
use Egulias\EmailValidator\Exception\NoDomainPart;
17
use Egulias\EmailValidator\Exception\NoLocalPart;
18
use Egulias\EmailValidator\Exception\UnclosedComment;
19
use Egulias\EmailValidator\Exception\UnclosedQuotedString;
20
use Egulias\EmailValidator\Exception\UnopenedComment;
21
use Egulias\EmailValidator\Warning\AddressLiteral;
22
use Egulias\EmailValidator\Warning\CFWSNearAt;
23
use Egulias\EmailValidator\Warning\CFWSWithFWS;
24
use Egulias\EmailValidator\Warning\Comment;
25
use Egulias\EmailValidator\Warning\DomainLiteral;
26
use Egulias\EmailValidator\Warning\DomainTooLong;
27
use Egulias\EmailValidator\Warning\IPV6BadChar;
28
use Egulias\EmailValidator\Warning\IPV6ColonEnd;
29
use Egulias\EmailValidator\Warning\IPV6ColonStart;
30
use Egulias\EmailValidator\Warning\IPV6Deprecated;
31
use Egulias\EmailValidator\Warning\IPV6DoubleColon;
32
use Egulias\EmailValidator\Warning\IPV6GroupCount;
33
use Egulias\EmailValidator\Warning\IPV6MaxGroups;
34
use Egulias\EmailValidator\Warning\LabelTooLong;
35
use Egulias\EmailValidator\Warning\LocalTooLong;
36
use Egulias\EmailValidator\Warning\ObsoleteDTEXT;
37
use Egulias\EmailValidator\Warning\QuotedString;
38
39
class RFCValidationTest extends \PHPUnit_Framework_TestCase
40
{
41
    /**
42
     * @var RFCValidation
43
     */
44
    protected $validator;
45
46
    /**
47
     * @var EmailLexer
48
     */
49
    protected $lexer;
50
    
51
    protected function setUp()
52
    {
53
        $this->validator = new RFCValidation();
54
        $this->lexer = new EmailLexer();
55
    }
56
57
    protected function tearDown()
58
    {
59
        $this->validator = null;
60
    }
61
62
    /**
63
     * @dataProvider getValidEmails
64
     */
65
    public function testValidEmails($email)
66
    {
67
        $this->assertTrue($this->validator->isValid($email, $this->lexer));
68
    }
69
70
    public function getValidEmails()
71
    {
72
        return array(
73
            ['â@iana.org'],
74
            ['[email protected]'],
75
            ['[email protected]'],
76
            ['[email protected]'],
77
            ['example@localhost'],
78
            ['fab\'[email protected]'],
79
            ['fab\ [email protected]'],
80
            ['example((example))@fakedfake.co.uk'],
81
            ['example@faked(fake).co.uk'],
82
            ['[email protected]'],
83
            ['инфо@письмо.рф'],
84
            ['"username"@example.com'],
85
            ['"user,name"@example.com'],
86
            ['"user name"@example.com'],
87
            ['"user@name"@example.com'],
88
            ['"user\"name"@example.com'],
89
            ['"\a"@iana.org'],
90
            ['"test\ test"@iana.org'],
91
            ['""@iana.org'],
92
            ['"\""@iana.org'],
93
            ['müller@möller.de'],
94
            ['test@email*'],
95
            ['test@email!'],
96
            ['test@email&'],
97
            ['test@email^'],
98
            ['test@email%'],
99
            ['test@email$'],
100
        );
101
    }
102
103
    public function testInvalidUTF8Email()
104
    {
105
        $email     = "\x80\x81\x82@\x83\x84\x85.\x86\x87\x88";
106
        $this->assertFalse($this->validator->isValid($email, $this->lexer));
107
    }
108
    
109
    /**
110
     * @dataProvider getInvalidEmails
111
     */
112
    public function testInvalidEmails($email)
113
    {
114
        $this->assertFalse($this->validator->isValid($email, $this->lexer));
115
    }
116
117
    public function getInvalidEmails()
118
    {
119
        return [
120
            ['[email protected] test'],
121
            ['user  [email protected]'],
122
            ['user   [email protected]'],
123
            ['[email protected]'],
124
            ['example@[email protected]'],
125
            ['([email protected]]'],
126
            ['example(example][email protected]'],
127
            ['.example@localhost'],
128
            ['ex\ample@localhost'],
129
            ['example@local\host'],
130
            ['example@localhost.'],
131
            ['user [email protected]'],
132
            ['username@ example . com'],
133
            ['example@(fake].com'],
134
            ['example@(fake.com'],
135
            ['username@example,com'],
136
            ['usern,[email protected]'],
137
            ['user[na][email protected]'],
138
            ['"""@iana.org'],
139
            ['"\"@iana.org'],
140
            ['"test"[email protected]'],
141
            ['"test""test"@iana.org'],
142
            ['"test"."test"@iana.org'],
143
            ['"test"[email protected]'],
144
            ['"test"' . chr(0) . '@iana.org'],
145
            ['"test\"@iana.org'],
146
            [chr(226) . '@iana.org'],
147
            ['test@' . chr(226) . '.org'],
148
            ['\r\[email protected]'],
149
            ['\r\n [email protected]'],
150
            ['\r\n \r\[email protected]'],
151
            ['\r\n \r\[email protected]'],
152
            ['\r\n \r\n [email protected]'],
153
            ['[email protected] \r\n'],
154
            ['[email protected] \r\n '],
155
            ['[email protected] \r\n \r\n'],
156
            ['[email protected] \r\n\r\n'],
157
            ['[email protected]  \r\n\r\n '],
158
            ['test@iana/icann.org'],
159
            ['test@foo;bar.com'],
160
            ['test;[email protected]'],
161
            ['[email protected]'],
162
            ['email.email@email."'],
163
            ['test@email>'],
164
            ['test@email<'],
165
            ['test@email{'],
166
        ];
167
    }
168
169
    /**
170
     * @dataProvider getInvalidEmailsWithErrors
171
     */
172
    public function testInvalidEmailsWithErrorsCheck($error, $email)
173
    {
174
        $this->assertFalse($this->validator->isValid($email, $this->lexer));
175
        $this->assertEquals($error, $this->validator->getError());
176
    }
177
178
    public function getInvalidEmailsWithErrors()
179
    {
180
        return [
181
            [new NoLocalPart(), '@example.co.uk'],
182
            [new NoDomainPart(), 'example@'],
183
            [new DomainHyphened(), '[email protected]'],
184
            [new DomainHyphened(), 'example@example-'],
185
            [new ConsecutiveAt(), 'example@@example.co.uk'],
186
            [new ConsecutiveDot(), '[email protected]'],
187
            [new ConsecutiveDot(), '[email protected]'],
188
            [new ExpectingATEXT(), '<example_example>@example.fr'],
189
            [new DotAtStart(), '.example@localhost'],
190
            [new DotAtStart(), '[email protected]'],
191
            [new DotAtEnd(), 'example@localhost.'],
192
            [new DotAtEnd(), '[email protected]'],
193
            [new UnclosedComment(), '(example@localhost'],
194
            [new UnclosedQuotedString(), '"example@localhost'],
195
            [new ExpectingATEXT(), 'exa"mple@localhost'],
196
            [new UnclosedComment(), '(example@localhost'],
197
            [new UnopenedComment(), 'comment)example@localhost'],
198
            [new UnopenedComment(), 'example(comment))@localhost'],
199
            [new UnopenedComment(), 'example@comment)localhost'],
200
            [new UnopenedComment(), 'example@localhost(comment))'],
201
            [new UnopenedComment(), 'example@(comment))example.com'],
202
            //This was the original. But atext is not allowed after \n
203
            //array(EmailValidator::ERR_EXPECTING_ATEXT, "exampl\[email protected]"),
204
            [new AtextAfterCFWS(), "exampl\[email protected]"],
205
            [new ExpectingDTEXT(), "example@[[]"],
206
            [new AtextAfterCFWS(), "exampl\[email protected]"],
207
            [new CRNoLF(), "example@exa\rmple.co.uk"],
208
            [new CRNoLF(), "example@[\r]"],
209
            [new CRNoLF(), "exam\[email protected]"],
210
        ];
211
    }
212
213
    /**
214
     * @dataProvider getInvalidEmailsWithWarnings
215
     */
216
    public function testInvalidEmailsWithWarningsCheck($expectedWarnings, $email)
217
    {
218
        $this->assertTrue($this->validator->isValid($email, $this->lexer));
219
        $warnings = $this->validator->getWarnings();
220
        $this->assertTrue(
221
            count($warnings) === count($expectedWarnings),
222
            "Expected: " . implode(",", $expectedWarnings) . " and got " . implode(",", $warnings)
223
        );
224
225
        foreach ($warnings as $warning) {
226
            $this->assertTrue(isset($expectedWarnings[$warning->code()]));
227
        }
228
    }
229
    
230
    public function getInvalidEmailsWithWarnings()
231
    {
232
        return [
233
            [[CFWSNearAt::CODE], 'example @invalid.example.com'],
234
            [[CFWSNearAt::CODE], 'example@ invalid.example.com'],
235
            [[Comment::CODE], '[email protected](examplecomment).com'],
236
            [[Comment::CODE,CFWSNearAt::CODE], 'example(examplecomment)@invalid.example.com'],
237
            [[QuotedString::CODE, CFWSWithFWS::CODE,], "\"\t\"@invalid.example.com"],
238
            [[QuotedString::CODE, CFWSWithFWS::CODE,], "\"\r\"@invalid.example.com"],
239
            [[AddressLiteral::CODE,], 'example@[127.0.0.1]'],
240
            [[AddressLiteral::CODE,], 'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]'],
241
            [[AddressLiteral::CODE, IPV6Deprecated::CODE], 'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370::]'],
242
            [[AddressLiteral::CODE, IPV6MaxGroups::CODE,], 'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334::]'],
243
            [[AddressLiteral::CODE, IPV6DoubleColon::CODE,], 'example@[IPv6:1::1::1]'],
244
            [[ObsoleteDTEXT::CODE, DomainLiteral::CODE,], "example@[\n]"],
245
            [[DomainLiteral::CODE,], 'example@[::1]'],
246
            [[DomainLiteral::CODE,], 'example@[::123.45.67.178]'],
247
            [
248
                [IPV6ColonStart::CODE, AddressLiteral::CODE, IPV6GroupCount::CODE,],
249
                'example@[IPv6::2001:0db8:85a3:0000:0000:8a2e:0370:7334]'
250
            ],
251
            [
252
                [AddressLiteral::CODE, IPV6BadChar::CODE,],
253
                'example@[IPv6:z001:0db8:85a3:0000:0000:8a2e:0370:7334]'
254
            ],
255
            [
256
                [AddressLiteral::CODE, IPV6ColonEnd::CODE,],
257
                'example@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:]'
258
            ],
259
            [[QuotedString::CODE,], '"example"@invalid.example.com'],
260
            [
261
                [LocalTooLong::CODE,],
262
                'too_long_localpart_too_long_localpart_too_long_localpart_too_long_localpart@invalid.example.com'
263
            ],
264
            [
265
                [LabelTooLong::CODE,],
266
                'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart.co.uk'
267
            ],
268
            [
269
                [DomainTooLong::CODE, LabelTooLong::CODE,],
270
                'example2@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
271
                'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
272
                'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'
273
            ],
274
            [
275
                [DomainTooLong::CODE, LabelTooLong::CODE,],
276
                'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
277
                'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
278
                'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpar'
279
            ],
280
        ];
281
    }
282
}
283