GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TokenTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 338
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 189
dl 0
loc 338
rs 10
c 3
b 0
f 0
wmc 17

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testToDebugString() 0 3 1
A provideToDebugString() 0 16 1
A testIsEof() 0 7 1
A testEscapeString() 0 24 1
A providerAsXyzError() 0 23 1
A testMakeStringNonEscapes() 0 28 1
A testEscapeStringNull() 0 3 1
A testEq() 0 10 1
A testAsXyz() 0 4 1
A testMakeIdentifier() 0 26 1
A testEscapeIdentifier() 0 4 1
A providerEscapeIdentifier() 0 12 1
A testMakeStringEscapes() 0 29 1
A providerAsXyz() 0 51 1
A testMakeStringQuotes() 0 16 2
A testAsXyzError() 0 3 1
1
<?php
2
3
namespace Graze\Morphism\Parse;
4
5
use Exception;
6
use Graze\Morphism\Test\Parse\TestCase;
7
8
class TokenTest extends TestCase
9
{
10
    public function testIsEof()
11
    {
12
        $eof = new Token(Token::EOF);
13
        $this->assertTrue($eof->isEof());
14
15
        $notEof = new Token(Token::NUMBER, '123');
16
        $this->assertFalse($notEof->isEof());
17
    }
18
19
    public function testEq()
20
    {
21
        $token = new Token(Token::STRING, 'test');
22
23
        $this->assertTrue($token->eq(Token::STRING, 'test'));
24
        $this->assertTrue($token->eq(Token::STRING, 'Test'));
25
        $this->assertTrue($token->eq(Token::STRING, 'TEST'));
26
27
        $this->assertFalse($token->eq(Token::STRING, 'testtest'));
28
        $this->assertFalse($token->eq(Token::NUMBER, 'test'));
29
    }
30
31
    public function testMakeStringEscapes()
32
    {
33
        $sq = "'";
34
        $bs = "\\";
35
36
        $escaped = Token::fromString(
37
            "bcd{$bs}{$bs}" .
38
            "cde{$bs}0" .
39
            "def{$bs}b" .
40
            "efg{$bs}n" .
41
            "fgh{$bs}r" .
42
            "ghi{$bs}t" .
43
            "hij{$bs}z" .
44
            "xyz",
45
            $sq
46
        );
47
        $this->assertTrue(
48
            $escaped->eq(
49
                Token::STRING,
50
                "bcd{$bs}" .
51
                "cde" . chr(0) .
52
                "def" . chr(8) .
53
                "efg" . chr(10) .
54
                "fgh" . chr(13) .
55
                "ghi" . chr(9) .
56
                "hij" . chr(26) .
57
                "xyz"
58
            ),
59
            "all escape sequences are correctly processed"
60
        );
61
    }
62
63
    public function testMakeStringNonEscapes()
64
    {
65
        $sq = "'";
66
        $dq = '"';
67
        $bs = "\\";
68
69
        $unescaped = Token::fromString(
70
            "ijk{$bs}a" .
71
            "jkl{$bs}f" .
72
            "klm{$bs}${sq}" .
73
            "lmn{$bs}${dq}" .
74
            "mno{$bs}?" .
75
            "nop{$bs}176" .
76
            "opq{$bs}x7e" .
77
            "xyz",
78
            $sq
79
        );
80
        $this->assertTrue(
81
            $unescaped->eq(
82
                Token::STRING,
83
                "ijk" . "a" .
84
                "jkl" . "f" .
85
                "klm" . "${sq}" .
86
                "lmn" . "${dq}" .
87
                "mno" . "?" .
88
                "nop" . "176" .
89
                "opq" . "x7e" .
90
                "xyz"
91
            )
92
        );
93
    }
94
95
    public function testMakeStringQuotes()
96
    {
97
        $sq = "'";
98
99
        foreach ([
100
            ""              => "",
101
            "{$sq}${sq}"    => "{$sq}",
102
            "abc{$sq}${sq}" => "abc{$sq}",
103
            "{$sq}${sq}abc" => "{$sq}abc",
104
        ] as $arg => $result) {
105
            $token = Token::fromString($arg, $sq);
106
            $this->assertTrue($token->eq(Token::STRING, $result));
107
        }
108
109
        $token = Token::fromString("{$sq}${sq}", $sq);
110
        $this->assertTrue($token->eq(Token::STRING, "{$sq}"));
111
    }
112
113
    public function testMakeIdentifier()
114
    {
115
        $bs = "\\";
116
117
        $token = Token::fromIdentifier(
118
            "abc``def" .
119
            "{$bs}0" .
120
            "{$bs}b" .
121
            "{$bs}n" .
122
            "{$bs}r" .
123
            "{$bs}t" .
124
            "{$bs}z" .
125
            ""
126
        );
127
128
        $this->assertTrue(
129
            $token->eq(
130
                Token::IDENTIFIER,
131
                'abc`def' .
132
                "{$bs}0" .
133
                "{$bs}b" .
134
                "{$bs}n" .
135
                "{$bs}r" .
136
                "{$bs}t" .
137
                "{$bs}z" .
138
                ""
139
            )
140
        );
141
    }
142
143
    public function testEscapeStringNull()
144
    {
145
        $this->assertSame('NULL', Token::escapeString(null));
146
    }
147
148
    public function testEscapeString()
149
    {
150
        $bs = "\\";
151
        $sq = "'";
152
        $nul = chr(0);
153
        $lf = chr(10);
154
        $cr = chr(13);
155
156
        $this->assertSame(
157
            "{$sq}" .
158
            "abc''" .
159
            "def{$bs}0" .
160
            "ghi{$bs}n" .
161
            "jkl{$bs}r" .
162
            "mno{$bs}{$bs}" .
163
            "pqr" .
164
            "{$sq}",
165
            Token::escapeString(
166
                "abc{$sq}" .
167
                "def{$nul}" .
168
                "ghi{$lf}" .
169
                "jkl{$cr}" .
170
                "mno{$bs}" .
171
                "pqr"
172
            )
173
        );
174
    }
175
176
    /**
177
     * @dataProvider providerEscapeIdentifier
178
     * @param bool   $quoteNames
179
     * @param string $arg
180
     * @param string $expected
181
     */
182
    public function testEscapeIdentifier($quoteNames, $arg, $expected)
183
    {
184
        Token::setQuoteNames($quoteNames);
185
        $this->assertSame($expected, Token::escapeIdentifier($arg));
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    public function providerEscapeIdentifier()
192
    {
193
        return [
194
            [true,  '',        '``'        ],
195
            [true,  'a',       '`a`'       ],
196
            [true,  'abc def', '`abc def`' ],
197
            [true,  'abc`def', '`abc``def`'],
198
199
            [false, '',        ''          ],
200
            [false, 'a',       'a'         ],
201
            [false, 'abc def', 'abc def'   ],
202
            [false, 'abc`def', 'abc`def'   ],
203
        ];
204
    }
205
206
    /**
207
     * @dataProvider providerAsXyz
208
     * @param string $method
209
     * @param string $type
210
     * @param string $arg
211
     * @param mixed  $expected
212
     */
213
    public function testAsXyz($method, $type, $arg, $expected)
214
    {
215
        $token = new Token($type, $arg);
216
        $this->assertSame($expected, $token->$method());
217
    }
218
219
    /**
220
     * @return array
221
     */
222
    public function providerAsXyz()
223
    {
224
        return [
225
            ['asString',   Token::STRING, 'abc',                 'abc'    ],
226
            ['asString',   Token::STRING, '',                    ''       ],
227
228
            ['asString',   Token::NUMBER, '123',                 '123'    ],
229
            ['asString',   Token::NUMBER, '0123',                '123'    ],
230
            ['asString',   Token::NUMBER, '-0123',               '-123'   ],
231
            ['asString',   Token::NUMBER, '1.2300',              '1.2300' ],
232
            ['asString',   Token::NUMBER, '1.234567890123',      '1.234567890123' ],
233
            ['asString',   Token::NUMBER, '-1.23',               '-1.23'  ],
234
            ['asString',   Token::NUMBER, '+1.23',               '1.23'   ],
235
            ['asString',   Token::NUMBER, '.23',                 '0.23'   ],
236
            ['asString',   Token::NUMBER, '',                    '0'      ],
237
238
        // TODO - work is needed on Token::asString to make these tests parse:
239
        //  ['asString',   Token::NUMBER, '1.234e1',             '12.34'  ],
240
        //  ['asString',   Token::NUMBER, '1.234000e15',         '1.234e15' ],
241
        //  ['asString',   Token::NUMBER, '0.999999e15',         '999999000000000' ],
242
        //  ['asString',   Token::NUMBER, '-1.234000e15',        '-1.234e15' ],
243
        //  ['asString',   Token::NUMBER, '-0.999999e15',        '-999999000000000'],
244
        //  ['asString',   Token::NUMBER, '1.0001e-15',          '0.0000000000000010001' ],
245
        //  ['asString',   Token::NUMBER, '0.999e-16',           '9.99e-16' ],
246
        //  ['asString',   Token::NUMBER, '-1.0001e-15',         '-0.0000000000000010001' ],
247
        //  ['asString',   Token::NUMBER, '-0.999e-16',          '-9.99e-16' ],
248
249
            ['asString',   Token::HEX,    '68656c6c6f21',        'hello!' ],
250
            ['asString',   Token::BIN,    '0111111000100011',    '~#'     ],
251
252
            ['asNumber',   Token::STRING, '123',                 123   ],
253
            ['asNumber',   Token::NUMBER, '456',                 456   ],
254
            ['asNumber',   Token::HEX,    'fffe',                65534 ],
255
            ['asNumber',   Token::BIN,    '10100101',            165   ],
256
257
            ['asDate',     Token::STRING, '0',                   '0000-00-00'],
258
            ['asDate',     Token::STRING, '0000-00-00',          '0000-00-00'],
259
            ['asDate',     Token::STRING, '1970-08-12',          '1970-08-12'],
260
            ['asDate',     Token::STRING, '2001-12-31',          '2001-12-31'],
261
262
            ['asTime',     Token::STRING, '0',                   '00:00:00'  ],
263
            ['asTime',     Token::STRING, '00:00:00',            '00:00:00'  ],
264
            ['asTime',     Token::STRING, '17:45:59',            '17:45:59'  ],
265
266
            ['asDateTime', Token::STRING, '0',                   '0000-00-00 00:00:00'],
267
            ['asDateTime', Token::STRING, '0000-00-00',          '0000-00-00 00:00:00'],
268
            ['asDateTime', Token::STRING, '1970-08-12',          '1970-08-12 00:00:00'],
269
            ['asDateTime', Token::STRING, '2001-12-31',          '2001-12-31 00:00:00'],
270
            ['asDateTime', Token::STRING, '0000-00-00 00:00:00', '0000-00-00 00:00:00'],
271
            ['asDateTime', Token::STRING, '1970-08-12 13:34:45', '1970-08-12 13:34:45'],
272
            ['asDateTime', Token::STRING, '2001-12-31 23:59:59', '2001-12-31 23:59:59'],
273
        ];
274
    }
275
276
    /**
277
     * @dataProvider providerAsXyzError
278
     * @expectedException Exception
279
     * @param string $method
280
     * @param string $type
281
     * @param string $arg
282
     */
283
    public function testAsXyzError($method, $type, $arg)
284
    {
285
        (new Token($type, $arg))->$method();
286
    }
287
288
    /**
289
     * @return array
290
     */
291
    public function providerAsXyzError()
292
    {
293
        return [
294
            ['asDate',     Token::STRING, 'abc'       ],
295
            ['asDate',     Token::STRING, '1970'      ],
296
            ['asDate',     Token::STRING, '19700812'  ],
297
            ['asDate',     Token::STRING, '1970/08/12'],
298
299
            ['asTime',     Token::STRING, 'abc'       ],
300
            ['asTime',     Token::STRING, '000000'    ],
301
            ['asTime',     Token::STRING, '1745'      ],
302
            ['asTime',     Token::STRING, '174559'    ],
303
304
            ['asDateTime', Token::STRING, 'abc'           ],
305
            ['asDateTime', Token::STRING, '19700812'      ],
306
            ['asDateTime', Token::STRING, '1970/08/12'    ],
307
            ['asDateTime', Token::STRING, '197008120000'  ],
308
            ['asDateTime', Token::STRING, '19700812000000'],
309
310
            ['asString',   Token::SYMBOL, 'abc'       ],
311
312
            ['asNumber',   Token::STRING, 'abc'       ],
313
            ['asNumber',   Token::SYMBOL, 'abc'       ],
314
        ];
315
    }
316
317
    /**
318
     * @param Token $token
319
     * @param string $expected
320
     * @dataProvider provideToDebugString
321
     */
322
    public function testToDebugString(Token $token, $expected)
323
    {
324
        $this->assertEquals($expected, $token->toDebugString());
325
    }
326
327
    /**
328
     * @return array
329
     */
330
    public function provideToDebugString()
331
    {
332
        $text = 'foo';
333
334
        return [
335
            // Token, Expected output
336
            [new Token(Token::BIN, $text), 'bin[foo]'],
337
            [new Token(Token::COMMENT, $text), 'comment[foo]'],
338
            [new Token(Token::CONDITIONAL_END, $text), 'conditional-end[foo]'],
339
            [new Token(Token::CONDITIONAL_START, $text), 'conditional-start[foo]'],
340
            [new Token(Token::EOF, $text), 'EOF[foo]'],
341
            [new Token(Token::HEX, $text), 'hex[foo]'],
342
            [new Token(Token::IDENTIFIER, $text), 'identifier[foo]'],
343
            [new Token(Token::NUMBER, $text), 'number[foo]'],
344
            [new Token(Token::SYMBOL, $text), 'symbol[foo]'],
345
            [new Token(Token::WHITESPACE, $text), 'whitespace[foo]'],
346
        ];
347
    }
348
}
349