File: AnonClassParenthesisOwnerTest.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 (158 lines) | stat: -rw-r--r-- 6,417 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
<?php
/**
 * Tests the adding of the "parenthesis" keys to an anonymous class token.
 *
 * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
 * @copyright 2019 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\PHP;

use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;

final class AnonClassParenthesisOwnerTest extends AbstractTokenizerTestCase
{


    /**
     * Test that anonymous class tokens without parenthesis do not get assigned a parenthesis owner.
     *
     * @param string $testMarker The comment which prefaces the target token in the test file.
     *
     * @dataProvider dataAnonClassNoParentheses
     * @covers       PHP_CodeSniffer\Tokenizers\PHP::processAdditional
     *
     * @return void
     */
    public function testAnonClassNoParentheses($testMarker)
    {
        $tokens = $this->phpcsFile->getTokens();

        $anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
        $this->assertFalse(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
        $this->assertFalse(array_key_exists('parenthesis_opener', $tokens[$anonClass]));
        $this->assertFalse(array_key_exists('parenthesis_closer', $tokens[$anonClass]));

    }//end testAnonClassNoParentheses()


    /**
     * Test that the next open/close parenthesis after an anonymous class without parenthesis
     * do not get assigned the anonymous class as a parenthesis owner.
     *
     * @param string $testMarker The comment which prefaces the target token in the test file.
     *
     * @dataProvider dataAnonClassNoParentheses
     * @covers       PHP_CodeSniffer\Tokenizers\PHP::processAdditional
     *
     * @return void
     */
    public function testAnonClassNoParenthesesNextOpenClose($testMarker)
    {
        $tokens   = $this->phpcsFile->getTokens();
        $function = $this->getTargetToken($testMarker, T_FUNCTION);

        $opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
        $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]));
        $this->assertSame($function, $tokens[$opener]['parenthesis_owner']);

        $closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);
        $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]));
        $this->assertSame($function, $tokens[$closer]['parenthesis_owner']);

    }//end testAnonClassNoParenthesesNextOpenClose()


    /**
     * Data provider.
     *
     * @see testAnonClassNoParentheses()
     * @see testAnonClassNoParenthesesNextOpenClose()
     *
     * @return array<string, array<string, string>>
     */
    public static function dataAnonClassNoParentheses()
    {
        return [
            'plain'                                              => [
                'testMarker' => '/* testNoParentheses */',
            ],
            'readonly'                                           => [
                'testMarker' => '/* testReadonlyNoParentheses */',
            ],
            'declaration contains comments and extra whitespace' => [
                'testMarker' => '/* testNoParenthesesAndEmptyTokens */',
            ],
        ];

    }//end dataAnonClassNoParentheses()


    /**
     * Test that anonymous class tokens with parenthesis get assigned a parenthesis owner,
     * opener and closer; and that the opener/closer get the anonymous class assigned as owner.
     *
     * @param string $testMarker The comment which prefaces the target token in the test file.
     *
     * @dataProvider dataAnonClassWithParentheses
     * @covers       PHP_CodeSniffer\Tokenizers\PHP::processAdditional
     *
     * @return void
     */
    public function testAnonClassWithParentheses($testMarker)
    {
        $tokens    = $this->phpcsFile->getTokens();
        $anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
        $opener    = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
        $closer    = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);

        $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
        $this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$anonClass]));
        $this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$anonClass]));
        $this->assertSame($anonClass, $tokens[$anonClass]['parenthesis_owner']);
        $this->assertSame($opener, $tokens[$anonClass]['parenthesis_opener']);
        $this->assertSame($closer, $tokens[$anonClass]['parenthesis_closer']);

        $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]));
        $this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$opener]));
        $this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$opener]));
        $this->assertSame($anonClass, $tokens[$opener]['parenthesis_owner']);
        $this->assertSame($opener, $tokens[$opener]['parenthesis_opener']);
        $this->assertSame($closer, $tokens[$opener]['parenthesis_closer']);

        $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]));
        $this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$closer]));
        $this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$closer]));
        $this->assertSame($anonClass, $tokens[$closer]['parenthesis_owner']);
        $this->assertSame($opener, $tokens[$closer]['parenthesis_opener']);
        $this->assertSame($closer, $tokens[$closer]['parenthesis_closer']);

    }//end testAnonClassWithParentheses()


    /**
     * Data provider.
     *
     * @see testAnonClassWithParentheses()
     *
     * @return array<string, array<string, string>>
     */
    public static function dataAnonClassWithParentheses()
    {
        return [
            'plain'                                              => [
                'testMarker' => '/* testWithParentheses */',
            ],
            'readonly'                                           => [
                'testMarker' => '/* testReadonlyWithParentheses */',
            ],
            'declaration contains comments and extra whitespace' => [
                'testMarker' => '/* testWithParenthesesAndEmptyTokens */',
            ],
        ];

    }//end dataAnonClassWithParentheses()


}//end class