File: GetTokensAsStringTest.php

package info (click to toggle)
php-codesniffer 3.11.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,772 kB
  • sloc: php: 84,771; pascal: 10,061; xml: 6,832; javascript: 2,096; sh: 11; makefile: 4
file content (334 lines) | stat: -rw-r--r-- 10,942 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
 * Tests for the \PHP_CodeSniffer\Files\File::getTokensAsString method.
 *
 * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
 * @copyright 2022-2024 PHPCSStandards Contributors
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTestCase;

/**
 * Tests for the \PHP_CodeSniffer\Files\File:getTokensAsString method.
 *
 * @covers \PHP_CodeSniffer\Files\File::getTokensAsString
 */
final class GetTokensAsStringTest extends AbstractMethodUnitTestCase
{


    /**
     * Test passing a non-existent token pointer.
     *
     * @return void
     */
    public function testNonExistentToken()
    {
        $this->expectRunTimeException('The $start position for getTokensAsString() must exist in the token stack');

        self::$phpcsFile->getTokensAsString(100000, 10);

    }//end testNonExistentToken()


    /**
     * Test passing a non integer `$start`, like the result of a failed $phpcsFile->findNext().
     *
     * @return void
     */
    public function testNonIntegerStart()
    {
        $this->expectRunTimeException('The $start position for getTokensAsString() must exist in the token stack');

        self::$phpcsFile->getTokensAsString(false, 10);

    }//end testNonIntegerStart()


    /**
     * Test passing a non integer `$length`.
     *
     * @return void
     */
    public function testNonIntegerLength()
    {
        $result = self::$phpcsFile->getTokensAsString(10, false);
        $this->assertSame('', $result);

        $result = self::$phpcsFile->getTokensAsString(10, 1.5);
        $this->assertSame('', $result);

    }//end testNonIntegerLength()


    /**
     * Test passing a zero or negative `$length`.
     *
     * @return void
     */
    public function testLengthEqualToOrLessThanZero()
    {
        $result = self::$phpcsFile->getTokensAsString(10, -10);
        $this->assertSame('', $result);

        $result = self::$phpcsFile->getTokensAsString(10, 0);
        $this->assertSame('', $result);

    }//end testLengthEqualToOrLessThanZero()


    /**
     * Test passing a `$length` beyond the end of the file.
     *
     * @return void
     */
    public function testLengthBeyondEndOfFile()
    {
        $semicolon = $this->getTargetToken('/* testEndOfFile */', T_SEMICOLON);
        $result    = self::$phpcsFile->getTokensAsString($semicolon, 20);
        $this->assertSame(
            ';
',
            $result
        );

    }//end testLengthBeyondEndOfFile()


    /**
     * Test getting a token set as a string.
     *
     * @param string     $testMarker     The comment which prefaces the target token in the test file.
     * @param int|string $startTokenType The type of token(s) to look for for the start of the string.
     * @param int        $length         Token length to get.
     * @param string     $expected       The expected function return value.
     *
     * @dataProvider dataGetTokensAsString
     *
     * @return void
     */
    public function testGetTokensAsString($testMarker, $startTokenType, $length, $expected)
    {
        $start  = $this->getTargetToken($testMarker, $startTokenType);
        $result = self::$phpcsFile->getTokensAsString($start, $length);
        $this->assertSame($expected, $result);

    }//end testGetTokensAsString()


    /**
     * Data provider.
     *
     * @see testGetTokensAsString() For the array format.
     *
     * @return array<string, array<string, string|int>>
     */
    public static function dataGetTokensAsString()
    {
        return [
            'length-0'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 0,
                'expected'       => '',
            ],
            'length-1'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 1,
                'expected'       => '1',
            ],
            'length-2'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 2,
                'expected'       => '1 ',
            ],
            'length-3'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 3,
                'expected'       => '1 +',
            ],
            'length-4'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 4,
                'expected'       => '1 + ',
            ],
            'length-5'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 5,
                'expected'       => '1 + 2',
            ],
            'length-6'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 6,
                'expected'       => '1 + 2 ',
            ],
            'length-7'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 7,
                'expected'       => '1 + 2 +',
            ],
            'length-8'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 8,
                'expected'       => '1 + 2 +
',
            ],
            'length-9'          => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 9,
                'expected'       => '1 + 2 +
        ',
            ],
            'length-10'         => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 10,
                'expected'       => '1 + 2 +
        // Comment.
',
            ],
            'length-11'         => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 11,
                'expected'       => '1 + 2 +
        // Comment.
        ',
            ],
            'length-12'         => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 12,
                'expected'       => '1 + 2 +
        // Comment.
        3',
            ],
            'length-13'         => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 13,
                'expected'       => '1 + 2 +
        // Comment.
        3 ',
            ],
            'length-14'         => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 14,
                'expected'       => '1 + 2 +
        // Comment.
        3 +',
            ],
            'length-34'         => [
                'testMarker'     => '/* testCalculation */',
                'startTokenType' => T_LNUMBER,
                'length'         => 34,
                'expected'       => '1 + 2 +
        // Comment.
        3 + 4
        + 5 + 6 + 7 > 20;',
            ],
            'namespace'         => [
                'testMarker'     => '/* testNamespace */',
                'startTokenType' => T_NAMESPACE,
                'length'         => 8,
                'expected'       => 'namespace Foo\Bar\Baz;',
            ],
            'use-with-comments' => [
                'testMarker'     => '/* testUseWithComments */',
                'startTokenType' => T_USE,
                'length'         => 17,
                'expected'       => 'use Foo /*comment*/ \ Bar
    // phpcs:ignore Stnd.Cat.Sniff --    For reasons.
    \ Bah;',
            ],
            'echo-with-tabs'    => [
                'testMarker'     => '/* testEchoWithTabs */',
                'startTokenType' => T_ECHO,
                'length'         => 13,
                'expected'       => 'echo \'foo\',
    \'bar\'   ,
        \'baz\';',
            ],
            'end-of-file'       => [
                'testMarker'     => '/* testEndOfFile */',
                'startTokenType' => T_ECHO,
                'length'         => 4,
                'expected'       => 'echo   $foo;',
            ],
        ];

    }//end dataGetTokensAsString()


    /**
     * Test getting a token set as a string with the original, non tab-replaced content.
     *
     * @param string     $testMarker     The comment which prefaces the target token in the test file.
     * @param int|string $startTokenType The type of token(s) to look for for the start of the string.
     * @param int        $length         Token length to get.
     * @param string     $expected       The expected function return value.
     *
     * @dataProvider dataGetOrigContent
     *
     * @return void
     */
    public function testGetOrigContent($testMarker, $startTokenType, $length, $expected)
    {
        $start  = $this->getTargetToken($testMarker, $startTokenType);
        $result = self::$phpcsFile->getTokensAsString($start, $length, true);
        $this->assertSame($expected, $result);

    }//end testGetOrigContent()


    /**
     * Data provider.
     *
     * @see testGetOrigContent() For the array format.
     *
     * @return array<string, array<string, string|int>>
     */
    public static function dataGetOrigContent()
    {
        return [
            'use-with-comments' => [
                'testMarker'     => '/* testUseWithComments */',
                'startTokenType' => T_USE,
                'length'         => 17,
                'expected'       => 'use Foo /*comment*/ \ Bar
	// phpcs:ignore Stnd.Cat.Sniff --	 For reasons.
	\ Bah;',
            ],
            'echo-with-tabs'    => [
                'testMarker'     => '/* testEchoWithTabs */',
                'startTokenType' => T_ECHO,
                'length'         => 13,
                'expected'       => 'echo \'foo\',
	\'bar\'	,
		\'baz\';',
            ],
            'end-of-file'       => [
                'testMarker'     => '/* testEndOfFile */',
                'startTokenType' => T_ECHO,
                'length'         => 4,
                'expected'       => 'echo   $foo;',
            ],
        ];

    }//end dataGetOrigContent()


}//end class