File: RecurseScopeMapWithNamespaceOperatorTest.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 (98 lines) | stat: -rw-r--r-- 4,410 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
<?php
/**
 * Tests the scope opener/closers are set correctly when the namespace keyword is used as an operator.
 *
 * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
 * @copyright 2020 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Tests\Core\Tokenizers\Tokenizer;

use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;

final class RecurseScopeMapWithNamespaceOperatorTest extends AbstractTokenizerTestCase
{


    /**
     * Test that the scope opener/closers are set correctly when the namespace keyword is encountered as an operator.
     *
     * @param string            $testMarker The comment which prefaces the target tokens in the test file.
     * @param array<int|string> $tokenTypes The token type to search for.
     * @param array<int|string> $open       Optional. The token type for the scope opener.
     * @param array<int|string> $close      Optional. The token type for the scope closer.
     *
     * @dataProvider dataScopeSetting
     * @covers       PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
     *
     * @return void
     */
    public function testScopeSetting($testMarker, $tokenTypes, $open=T_OPEN_CURLY_BRACKET, $close=T_CLOSE_CURLY_BRACKET)
    {
        $tokens = $this->phpcsFile->getTokens();

        $target = $this->getTargetToken($testMarker, $tokenTypes);
        $opener = $this->getTargetToken($testMarker, $open);
        $closer = $this->getTargetToken($testMarker, $close);

        $this->assertArrayHasKey('scope_opener', $tokens[$target], 'Scope opener missing');
        $this->assertArrayHasKey('scope_closer', $tokens[$target], 'Scope closer missing');
        $this->assertSame($opener, $tokens[$target]['scope_opener'], 'Scope opener not same');
        $this->assertSame($closer, $tokens[$target]['scope_closer'], 'Scope closer not same');

        $this->assertArrayHasKey('scope_opener', $tokens[$opener], 'Scope opener missing for open curly');
        $this->assertArrayHasKey('scope_closer', $tokens[$opener], 'Scope closer missing for open curly');
        $this->assertSame($opener, $tokens[$opener]['scope_opener'], 'Scope opener not same for open curly');
        $this->assertSame($closer, $tokens[$opener]['scope_closer'], 'Scope closer not same for open curly');

        $this->assertArrayHasKey('scope_opener', $tokens[$closer], 'Scope opener missing for close curly');
        $this->assertArrayHasKey('scope_closer', $tokens[$closer], 'Scope closer missing for close curly');
        $this->assertSame($opener, $tokens[$closer]['scope_opener'], 'Scope opener not same for close curly');
        $this->assertSame($closer, $tokens[$closer]['scope_closer'], 'Scope closer not same for close curly');

    }//end testScopeSetting()


    /**
     * Data provider.
     *
     * @see testScopeSetting()
     *
     * @return array<string, array<string, string|array<int|string>>>
     */
    public static function dataScopeSetting()
    {
        return [
            'class which extends namespace relative name'           => [
                'testMarker' => '/* testClassExtends */',
                'tokenTypes' => [T_CLASS],
            ],
            'class which implements namespace relative name'        => [
                'testMarker' => '/* testClassImplements */',
                'tokenTypes' => [T_ANON_CLASS],
            ],
            'interface which extend namespace relative name'        => [
                'testMarker' => '/* testInterfaceExtends */',
                'tokenTypes' => [T_INTERFACE],
            ],
            'namespace relative name in function return type'       => [
                'testMarker' => '/* testFunctionReturnType */',
                'tokenTypes' => [T_FUNCTION],
            ],
            'namespace relative name in closure return type'        => [
                'testMarker' => '/* testClosureReturnType */',
                'tokenTypes' => [T_CLOSURE],
            ],
            'namespace relative name in arrow function return type' => [
                'testMarker' => '/* testArrowFunctionReturnType */',
                'tokenTypes' => [T_FN],
                'open'       => [T_FN_ARROW],
                'close'      => [T_SEMICOLON],
            ],
        ];

    }//end dataScopeSetting()


}//end class