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

EmailLexerTests::testLexerParsesMultipleSpaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
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)
0 ignored issues
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...
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()
0 ignored issues
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...
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)
0 ignored issues
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...
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$hex was never initialized. Although not strictly required by PHP, it is generally a good practice to add $hex = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
75
            $ret = chr($hex[0]);
76
        } elseif ($code_point < 0x800) {
77
            $hex[0] = 0x1C0 | $code_point >> 6;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$hex was never initialized. Although not strictly required by PHP, it is generally a good practice to add $hex = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$hex was never initialized. Although not strictly required by PHP, it is generally a good practice to add $hex = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$hex was never initialized. Although not strictly required by PHP, it is generally a good practice to add $hex = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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()
0 ignored issues
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...
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()
0 ignored issues
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...
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