File: ReplaceTabsInTokenMiscTest.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 (124 lines) | stat: -rw-r--r-- 4,379 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
<?php
/**
 * Tests the tab replacement logic.
 *
 * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
 * @copyright 2024 PHPCSStandards and contributors
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Tests\Core\Tokenizers\Tokenizer;

use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

/**
 * Miscellaneous tests for tab replacement.
 *
 * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::replaceTabsInToken
 */
final class ReplaceTabsInTokenMiscTest extends TestCase
{


    /**
     * Test that when no tab width is set or passed, the tab width will be set to 1.
     *
     * @return void
     */
    public function testTabWidthNotSet()
    {
        $config  = new ConfigDouble();
        $ruleset = new Ruleset($config);

        $content   = <<<EOD
<?php
		echo 'foo';
EOD;
        $phpcsFile = new DummyFile($content, $ruleset, $config);
        $phpcsFile->parse();

        $tokens = $phpcsFile->getTokens();
        $target = $phpcsFile->findNext(T_WHITESPACE, 0);

        // Verify initial state.
        $this->assertTrue(is_int($target), 'Target token was not found');
        $this->assertSame('		', $tokens[$target]['content'], 'Content after initial parsing does not contain tabs');
        $this->assertSame(2, $tokens[$target]['length'], 'Length after initial parsing is not as expected');
        $this->assertArrayNotHasKey('orig_content', $tokens[$target], "Key 'orig_content' found in the initial token array.");

        $phpcsFile->tokenizer->replaceTabsInToken($tokens[$target]);

        // Verify tab replacement.
        $this->assertSame('  ', $tokens[$target]['content'], 'Content after tab replacement is not as expected');
        $this->assertSame(2, $tokens[$target]['length'], 'Length after tab replacement is not as expected');
        $this->assertArrayHasKey('orig_content', $tokens[$target], "Key 'orig_content' not found in the token array.");

    }//end testTabWidthNotSet()


    /**
     * Test that the length calculation handles text in non-ascii encodings correctly.
     *
     * @requires extension iconv
     *
     * @return void
     */
    public function testLengthSettingRespectsEncoding()
    {
        $config           = new ConfigDouble();
        $config->tabWidth = 4;
        $ruleset          = new Ruleset($config);

        $content   = <<<EOD
<?php
echo 'пасха		пасха';
EOD;
        $phpcsFile = new DummyFile($content, $ruleset, $config);
        $phpcsFile->parse();

        $tokens = $phpcsFile->getTokens();
        $target = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, 0);

        $this->assertTrue(is_int($target), 'Target token was not found');
        $this->assertSame("'пасха     пасха'", $tokens[$target]['content'], 'Content is not as expected');
        $this->assertSame(17, $tokens[$target]['length'], 'Length is not as expected');
        $this->assertArrayHasKey('orig_content', $tokens[$target], "Key 'orig_content' not found in the token array.");
        $this->assertSame("'пасха		пасха'", $tokens[$target]['orig_content'], 'Orig_content is not as expected');

    }//end testLengthSettingRespectsEncoding()


    /**
     * Test that the length calculation falls back to byte length if iconv detects an illegal character.
     *
     * @requires extension iconv
     *
     * @return void
     */
    public function testLengthSettingFallsBackToBytesWhenTextContainsIllegalChars()
    {
        $config           = new ConfigDouble();
        $config->tabWidth = 4;
        $ruleset          = new Ruleset($config);

        $content   = <<<EOD
<?php
echo "aa\xC3\xC3	\xC3\xB8aa";
EOD;
        $phpcsFile = new DummyFile($content, $ruleset, $config);
        $phpcsFile->parse();

        $tokens = $phpcsFile->getTokens();
        $target = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, 0);

        $this->assertTrue(is_int($target), 'Target token was not found');
        $this->assertSame(11, $tokens[$target]['length'], 'Length is not as expected');
        $this->assertArrayHasKey('orig_content', $tokens[$target], "Key 'orig_content' not found in the token array.");

    }//end testLengthSettingFallsBackToBytesWhenTextContainsIllegalChars()


}//end class