Failed Conditions
Push — travis-php7.4 ( 6887c5 )
by Michael
13:05
created

DocLexerTest::testMarkerAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\Annotations;
4
5
use Doctrine\Annotations\DocLexer;
6
use PHPUnit\Framework\TestCase;
7
8
class DocLexerTest extends TestCase
9
{
10
    public function testMarkerAnnotation()
11
    {
12
        $lexer = new DocLexer;
13
14
        $lexer->setInput('@Name');
15
        self::assertNull($lexer->token);
16
        self::assertNull($lexer->lookahead);
17
18
        self::assertTrue($lexer->moveNext());
19
        self::assertNull($lexer->token);
20
        self::assertEquals('@', $lexer->lookahead['value']);
21
22
        self::assertTrue($lexer->moveNext());
23
        self::assertEquals('@', $lexer->token['value']);
24
        self::assertEquals('Name', $lexer->lookahead['value']);
25
26
        self::assertFalse($lexer->moveNext());
27
    }
28
29
    public function testScannerTokenizesDocBlockWhitConstants()
30
    {
31
        $lexer      = new DocLexer();
32
        $docblock   = '@AnnotationWithConstants(PHP_EOL, ClassWithConstants::SOME_VALUE, ClassWithConstants::CONSTANT_, ClassWithConstants::CONST_ANT3, \Doctrine\Tests\Annotations\Fixtures\InterfaceWithConstants::SOME_VALUE)';
33
34
        $tokens = [
35
            [
36
                'value'     => '@',
37
                'position'  => 0,
38
                'type'      => DocLexer::T_AT,
39
            ],
40
            [
41
                'value'     => 'AnnotationWithConstants',
42
                'position'  => 1,
43
                'type'      => DocLexer::T_IDENTIFIER,
44
            ],
45
            [
46
                'value'     => '(',
47
                'position'  => 24,
48
                'type'      => DocLexer::T_OPEN_PARENTHESIS,
49
            ],
50
            [
51
                'value'     => 'PHP_EOL',
52
                'position'  => 25,
53
                'type'      => DocLexer::T_IDENTIFIER,
54
            ],
55
            [
56
                'value'     => ',',
57
                'position'  => 32,
58
                'type'      => DocLexer::T_COMMA,
59
            ],
60
            [
61
                'value'     => 'ClassWithConstants::SOME_VALUE',
62
                'position'  => 34,
63
                'type'      => DocLexer::T_IDENTIFIER,
64
            ],
65
            [
66
                'value'     => ',',
67
                'position'  => 64,
68
                'type'      => DocLexer::T_COMMA,
69
            ],
70
            [
71
                'value'     => 'ClassWithConstants::CONSTANT_',
72
                'position'  => 66,
73
                'type'      => DocLexer::T_IDENTIFIER,
74
            ],
75
            [
76
                'value'     => ',',
77
                'position'  => 95,
78
                'type'      => DocLexer::T_COMMA,
79
            ],
80
            [
81
                'value'     => 'ClassWithConstants::CONST_ANT3',
82
                'position'  => 97,
83
                'type'      => DocLexer::T_IDENTIFIER,
84
            ],
85
            [
86
                'value'     => ',',
87
                'position'  => 127,
88
                'type'      => DocLexer::T_COMMA,
89
            ],
90
            [
91
                'value'     => '\\Doctrine\\Tests\\Annotations\\Fixtures\\InterfaceWithConstants::SOME_VALUE',
92
                'position'  => 129,
93
                'type'      => DocLexer::T_IDENTIFIER,
94
            ],
95
            [
96
                'value'     => ')',
97
                'position'  => 200,
98
                'type'      => DocLexer::T_CLOSE_PARENTHESIS,
99
            ]
100
        ];
101
102
        $lexer->setInput($docblock);
103
104
        foreach ($tokens as $expected) {
105
            $lexer->moveNext();
106
            $lookahead = $lexer->lookahead;
107
            self::assertEquals($expected['value'],     $lookahead['value']);
108
            self::assertEquals($expected['type'],      $lookahead['type']);
109
            self::assertEquals($expected['position'],  $lookahead['position']);
110
        }
111
112
        self::assertFalse($lexer->moveNext());
113
    }
114
115
116
    public function testScannerTokenizesDocBlockWhitInvalidIdentifier()
117
    {
118
        $lexer      = new DocLexer();
119
        $docblock   = '@Foo\3.42';
120
121
        $tokens = [
122
            [
123
                'value'     => '@',
124
                'position'  => 0,
125
                'type'      => DocLexer::T_AT,
126
            ],
127
            [
128
                'value'     => 'Foo',
129
                'position'  => 1,
130
                'type'      => DocLexer::T_IDENTIFIER,
131
            ],
132
            [
133
                'value'     => '\\',
134
                'position'  => 4,
135
                'type'      => DocLexer::T_NAMESPACE_SEPARATOR,
136
            ],
137
            [
138
                'value'     => 3.42,
139
                'position'  => 5,
140
                'type'      => DocLexer::T_FLOAT,
141
            ]
142
        ];
143
144
        $lexer->setInput($docblock);
145
146
        foreach ($tokens as $expected) {
147
            $lexer->moveNext();
148
            $lookahead = $lexer->lookahead;
149
            self::assertEquals($expected['value'],     $lookahead['value']);
150
            self::assertEquals($expected['type'],      $lookahead['type']);
151
            self::assertEquals($expected['position'],  $lookahead['position']);
152
        }
153
154
        self::assertFalse($lexer->moveNext());
155
    }
156
157
    /**
158
     * @group 44
159
     */
160
    public function testWithinDoubleQuotesVeryVeryLongStringWillNotOverflowPregSplitStackLimit()
161
    {
162
        $lexer = new DocLexer();
163
164
        $lexer->setInput('"' . str_repeat('.', 20240) . '"');
165
166
        self::assertInternalType('array', $lexer->glimpse());
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

166
        /** @scrutinizer ignore-deprecated */ self::assertInternalType('array', $lexer->glimpse());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
167
    }
168
169
    /**
170
     * @group 44
171
     */
172
    public function testRecognizesDoubleQuotesEscapeSequence()
173
    {
174
        $lexer    = new DocLexer();
175
        $docblock = '@Foo("""' . "\n" . '""")';
176
177
        $tokens = [
178
            [
179
                'value'     => '@',
180
                'position'  => 0,
181
                'type'      => DocLexer::T_AT,
182
            ],
183
            [
184
                'value'     => 'Foo',
185
                'position'  => 1,
186
                'type'      => DocLexer::T_IDENTIFIER,
187
            ],
188
            [
189
                'value'     => '(',
190
                'position'  => 4,
191
                'type'      => DocLexer::T_OPEN_PARENTHESIS,
192
            ],
193
            [
194
                'value'     => "\"\n\"",
195
                'position'  => 5,
196
                'type'      => DocLexer::T_STRING,
197
            ],
198
            [
199
                'value'     => ')',
200
                'position'  => 12,
201
                'type'      => DocLexer::T_CLOSE_PARENTHESIS,
202
            ],
203
        ];
204
205
        $lexer->setInput($docblock);
206
207
        foreach ($tokens as $expected) {
208
            $lexer->moveNext();
209
            $lookahead = $lexer->lookahead;
210
            self::assertEquals($expected['value'],    $lookahead['value']);
211
            self::assertEquals($expected['type'],     $lookahead['type']);
212
            self::assertEquals($expected['position'], $lookahead['position']);
213
        }
214
215
        self::assertFalse($lexer->moveNext());
216
    }
217
}
218