1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\Tests\EmailValidator; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
|
7
|
|
|
class EmailLexerTests extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function testLexerExtendsLib() |
11
|
|
|
{ |
12
|
|
|
$lexer = new EmailLexer(); |
13
|
|
|
$this->assertInstanceOf('Doctrine\Common\Lexer\AbstractLexer', $lexer); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @dataProvider getTokens |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function testLexerTokens($str, $token) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$lexer = new EmailLexer(); |
23
|
|
|
$lexer->setInput($str); |
24
|
|
|
$lexer->moveNext(); |
25
|
|
|
$lexer->moveNext(); |
26
|
|
|
$this->assertEquals($token, $lexer->token['type']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function testLexerParsesMultipleSpaces() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$lexer = new EmailLexer(); |
32
|
|
|
$lexer->setInput(' '); |
33
|
|
|
$lexer->moveNext(); |
34
|
|
|
$lexer->moveNext(); |
35
|
|
|
$this->assertEquals(EmailLexer::S_SP, $lexer->token['type']); |
36
|
|
|
$lexer->moveNext(); |
37
|
|
|
$this->assertEquals(EmailLexer::S_SP, $lexer->token['type']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @dataProvider invalidUTF8CharsProvider |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function testLexerParsesInvalidUTF8($char) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$lexer = new EmailLexer(); |
46
|
|
|
$lexer->setInput($char); |
47
|
|
|
$lexer->moveNext(); |
48
|
|
|
$lexer->moveNext(); |
49
|
|
|
|
50
|
|
|
$this->assertEquals(EmailLexer::INVALID, $lexer->token['type']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function invalidUTF8CharsProvider() |
54
|
|
|
{ |
55
|
|
|
$chars = array(); |
56
|
|
|
for ($i = 0; $i < 0x100; ++$i) { |
57
|
|
|
$c = $this->utf8Chr($i); |
58
|
|
|
if (preg_match('/(?=\p{Cc})(?=[^\t\n\n\r])/u', $c) && !preg_match('/\x{0000}/u', $c)) { |
59
|
|
|
$chars[] = array($c); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $chars; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function utf8Chr($code_point) |
67
|
|
|
{ |
68
|
|
|
|
69
|
|
|
if ($code_point < 0 || 0x10FFFF < $code_point || (0xD800 <= $code_point && $code_point <= 0xDFFF)) { |
70
|
|
|
return ''; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($code_point < 0x80) { |
74
|
|
|
$hex[0] = $code_point; |
|
|
|
|
75
|
|
|
$ret = chr($hex[0]); |
76
|
|
|
} elseif ($code_point < 0x800) { |
77
|
|
|
$hex[0] = 0x1C0 | $code_point >> 6; |
|
|
|
|
78
|
|
|
$hex[1] = 0x80 | $code_point & 0x3F; |
79
|
|
|
$ret = chr($hex[0]).chr($hex[1]); |
80
|
|
|
} elseif ($code_point < 0x10000) { |
81
|
|
|
$hex[0] = 0xE0 | $code_point >> 12; |
|
|
|
|
82
|
|
|
$hex[1] = 0x80 | $code_point >> 6 & 0x3F; |
83
|
|
|
$hex[2] = 0x80 | $code_point & 0x3F; |
84
|
|
|
$ret = chr($hex[0]).chr($hex[1]).chr($hex[2]); |
85
|
|
|
} else { |
86
|
|
|
$hex[0] = 0xF0 | $code_point >> 18; |
|
|
|
|
87
|
|
|
$hex[1] = 0x80 | $code_point >> 12 & 0x3F; |
88
|
|
|
$hex[2] = 0x80 | $code_point >> 6 & 0x3F; |
89
|
|
|
$hex[3] = 0x80 | $code_point & 0x3F; |
90
|
|
|
$ret = chr($hex[0]).chr($hex[1]).chr($hex[2]).chr($hex[3]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $ret; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testLexerForTab() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$lexer = new EmailLexer(); |
99
|
|
|
$lexer->setInput("foo\tbar"); |
100
|
|
|
$lexer->moveNext(); |
101
|
|
|
$lexer->skipUntil(EmailLexer::S_HTAB); |
102
|
|
|
$lexer->moveNext(); |
103
|
|
|
$this->assertEquals(EmailLexer::S_HTAB, $lexer->token['type']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function testLexerForUTF8() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$lexer = new EmailLexer(); |
109
|
|
|
$lexer->setInput("áÇ@bar.com"); |
110
|
|
|
$lexer->moveNext(); |
111
|
|
|
$lexer->moveNext(); |
112
|
|
|
$this->assertEquals(EmailLexer::GENERIC, $lexer->token['type']); |
113
|
|
|
$lexer->moveNext(); |
114
|
|
|
$this->assertEquals(EmailLexer::GENERIC, $lexer->token['type']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testLexerSearchToken() |
118
|
|
|
{ |
119
|
|
|
$lexer = new EmailLexer(); |
120
|
|
|
$lexer->setInput("foo\tbar"); |
121
|
|
|
$lexer->moveNext(); |
122
|
|
|
$this->assertTrue($lexer->find(EmailLexer::S_HTAB)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getTokens() |
126
|
|
|
{ |
127
|
|
|
return array( |
128
|
|
|
array("foo", EmailLexer::GENERIC), |
129
|
|
|
array("\r", EmailLexer::S_CR), |
130
|
|
|
array("\t", EmailLexer::S_HTAB), |
131
|
|
|
array("\r\n", EmailLexer::CRLF), |
132
|
|
|
array("\n", EmailLexer::S_LF), |
133
|
|
|
array(" ", EmailLexer::S_SP), |
134
|
|
|
array("@", EmailLexer::S_AT), |
135
|
|
|
array("IPv6", EmailLexer::S_IPV6TAG), |
136
|
|
|
array("::", EmailLexer::S_DOUBLECOLON), |
137
|
|
|
array(":", EmailLexer::S_COLON), |
138
|
|
|
array(".", EmailLexer::S_DOT), |
139
|
|
|
array("\"", EmailLexer::S_DQUOTE), |
140
|
|
|
array("-", EmailLexer::S_HYPHEN), |
141
|
|
|
array("\\", EmailLexer::S_BACKSLASH), |
142
|
|
|
array("/", EmailLexer::S_SLASH), |
143
|
|
|
array("(", EmailLexer::S_OPENPARENTHESIS), |
144
|
|
|
array(")", EmailLexer::S_CLOSEPARENTHESIS), |
145
|
|
|
array('<', EmailLexer::S_LOWERTHAN), |
146
|
|
|
array('>', EmailLexer::S_GREATERTHAN), |
147
|
|
|
array('[', EmailLexer::S_OPENBRACKET), |
148
|
|
|
array(']', EmailLexer::S_CLOSEBRACKET), |
149
|
|
|
array(';', EmailLexer::S_SEMICOLON), |
150
|
|
|
array(',', EmailLexer::S_COMMA), |
151
|
|
|
array('<', EmailLexer::S_LOWERTHAN), |
152
|
|
|
array('>', EmailLexer::S_GREATERTHAN), |
153
|
|
|
array('{', EmailLexer::S_OPENQBRACKET), |
154
|
|
|
array('}', EmailLexer::S_CLOSEQBRACKET), |
155
|
|
|
array('', EmailLexer::S_EMPTY), |
156
|
|
|
array(chr(31), EmailLexer::INVALID), |
157
|
|
|
array(chr(226), EmailLexer::GENERIC), |
158
|
|
|
array(chr(0), EmailLexer::C_NUL) |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.