File: EmailLexerTest.php

package info (click to toggle)
php-email-validator 4.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 760 kB
  • sloc: php: 3,534; makefile: 19; xml: 18
file content (202 lines) | stat: -rw-r--r-- 6,581 bytes parent folder | download
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
<?php

namespace Egulias\EmailValidator\Tests\EmailValidator;

use Egulias\EmailValidator\EmailLexer;
use PHPUnit\Framework\TestCase;

class EmailLexerTest extends TestCase
{

    public function testLexerExtendsLib()
    {
        $lexer = new EmailLexer();
        $this->assertInstanceOf('Doctrine\Common\Lexer\AbstractLexer', $lexer);
    }

    /**
     *  @dataProvider getTokens
     *
     */
    public function testLexerTokens($str, $token)
    {
        $lexer = new EmailLexer();
        $lexer->setInput($str);
        $lexer->moveNext();
        $lexer->moveNext();
        $this->assertEquals($token, $lexer->current->type);
    }

    public function testLexerParsesMultipleSpaces()
    {
        $lexer = new EmailLexer();
        $lexer->setInput('  ');
        $lexer->moveNext();
        $lexer->moveNext();
        $this->assertEquals(EmailLexer::S_SP, $lexer->current->type);
        $lexer->moveNext();
        $this->assertEquals(EmailLexer::S_SP, $lexer->current->type);
    }

    /**
     * @dataProvider invalidUTF8CharsProvider
     */
    public function testLexerParsesInvalidUTF8($char)
    {
        $lexer = new EmailLexer();
        $lexer->setInput($char);
        $lexer->moveNext();
        $lexer->moveNext();

        $this->assertEquals(EmailLexer::INVALID, $lexer->current->type);
    }

    public static function invalidUTF8CharsProvider()
    {
        $chars = array();
        for ($i = 0; $i < 0x100; ++$i) {
            $c = self::utf8Chr($i);
            if (preg_match('/(?=\p{Cc})(?=[^\t\n\n\r])/u', $c) && !preg_match('/\x{0000}/u', $c)) {
                $chars[] = array($c);
            }
        }

        return $chars;
    }

    protected static function utf8Chr($code_point)
    {

        if ($code_point < 0 || 0x10FFFF < $code_point || (0xD800 <= $code_point && $code_point <= 0xDFFF)) {
            return '';
        }

        if ($code_point < 0x80) {
            $hex[0] = $code_point;
            $ret = chr($hex[0]);
        } elseif ($code_point < 0x800) {
            $hex[0] = 0x1C0 | $code_point >> 6;
            $hex[1] = 0x80  | $code_point & 0x3F;
            $ret = chr($hex[0]) . chr($hex[1]);
        } elseif ($code_point < 0x10000) {
            $hex[0] = 0xE0 | $code_point >> 12;
            $hex[1] = 0x80 | $code_point >> 6 & 0x3F;
            $hex[2] = 0x80 | $code_point & 0x3F;
            $ret = chr($hex[0]) . chr($hex[1]) . chr($hex[2]);
        } else {
            $hex[0] = 0xF0 | $code_point >> 18;
            $hex[1] = 0x80 | $code_point >> 12 & 0x3F;
            $hex[2] = 0x80 | $code_point >> 6 & 0x3F;
            $hex[3] = 0x80 | $code_point  & 0x3F;
            $ret = chr($hex[0]) . chr($hex[1]) . chr($hex[2]) . chr($hex[3]);
        }

        return $ret;
    }

    public function testLexerForTab()
    {
        $lexer = new EmailLexer();
        $lexer->setInput("foo\tbar");
        $lexer->moveNext();
        $lexer->skipUntil(EmailLexer::S_HTAB);
        $lexer->moveNext();
        $this->assertEquals(EmailLexer::S_HTAB, $lexer->current->type);
    }

    public function testLexerForUTF8()
    {
        $lexer = new EmailLexer();
        $lexer->setInput("áÇ@bar.com");
        $lexer->moveNext();
        $lexer->moveNext();
        $this->assertEquals(EmailLexer::GENERIC, $lexer->current->type);
        $lexer->moveNext();
        $this->assertEquals(EmailLexer::GENERIC, $lexer->current->type);
    }

    public function testLexerSearchToken()
    {
        $lexer = new EmailLexer();
        $lexer->setInput("foo\tbar");
        $lexer->moveNext();
        $this->assertTrue($lexer->find(EmailLexer::S_HTAB));
    }

    public static function getTokens()
    {
        return array(
            array("foo", EmailLexer::GENERIC),
            array("\r", EmailLexer::S_CR),
            array("\t", EmailLexer::S_HTAB),
            array("\r\n", EmailLexer::CRLF),
            array("\n", EmailLexer::S_LF),
            array(" ", EmailLexer::S_SP),
            array("@", EmailLexer::S_AT),
            array("IPv6", EmailLexer::S_IPV6TAG),
            array("::", EmailLexer::S_DOUBLECOLON),
            array(":", EmailLexer::S_COLON),
            array(".", EmailLexer::S_DOT),
            array("\"", EmailLexer::S_DQUOTE),
            array("`", EmailLexer::S_BACKTICK),
            array("'", EmailLexer::S_SQUOTE),
            array("-", EmailLexer::S_HYPHEN),
            array("\\", EmailLexer::S_BACKSLASH),
            array("/", EmailLexer::S_SLASH),
            array("(", EmailLexer::S_OPENPARENTHESIS),
            array(")", EmailLexer::S_CLOSEPARENTHESIS),
            array('<', EmailLexer::S_LOWERTHAN),
            array('>', EmailLexer::S_GREATERTHAN),
            array('[', EmailLexer::S_OPENBRACKET),
            array(']', EmailLexer::S_CLOSEBRACKET),
            array(';', EmailLexer::S_SEMICOLON),
            array(',', EmailLexer::S_COMMA),
            array('<', EmailLexer::S_LOWERTHAN),
            array('>', EmailLexer::S_GREATERTHAN),
            array('{', EmailLexer::S_OPENCURLYBRACES),
            array('}', EmailLexer::S_CLOSECURLYBRACES),
            array('|', EmailLexer::S_PIPE),
            array('~', EmailLexer::S_TILDE),
            array('=', EmailLexer::S_EQUAL),
            array('+', EmailLexer::S_PLUS),
            array('¿', EmailLexer::INVERT_QUESTIONMARK),
            array('?', EmailLexer::QUESTIONMARK),
            array('#', EmailLexer::NUMBER_SIGN),
            array('¡', EmailLexer::INVERT_EXCLAMATION),
            array('_', EmailLexer::S_UNDERSCORE),
            array('',  EmailLexer::S_EMPTY),
            array(chr(31),  EmailLexer::INVALID),
            array(chr(226),  EmailLexer::GENERIC),
            array(chr(0),  EmailLexer::C_NUL)
        );
    }

    public function testRecordIsOffAtStart()
    {
        $lexer = new EmailLexer();
        $lexer->setInput('foo-bar');
        $lexer->moveNext();
        $this->assertEquals('', $lexer->getAccumulatedValues());
    }

    public function testRecord()
    {
        $lexer = new EmailLexer();
        $lexer->setInput('foo-bar');
        $lexer->startRecording();
        $lexer->moveNext();
        $lexer->moveNext();
        $this->assertEquals('foo', $lexer->getAccumulatedValues());
    }

    public function testRecordAndClear()
    {
        $lexer = new EmailLexer();
        $lexer->setInput('foo-bar');
        $lexer->startRecording();
        $lexer->moveNext();
        $lexer->moveNext();
        $lexer->clearRecorded();
        $this->assertEquals('', $lexer->getAccumulatedValues());
    }
}