|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the tokenization of goto declarations and statements. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2020 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Config; |
| 13 | +use PHP_CodeSniffer\Ruleset; |
| 14 | +use PHP_CodeSniffer\Files\DummyFile; |
| 15 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 16 | + |
| 17 | +/** |
| 18 | + * Heredoc/nowdoc closer token test. |
| 19 | + * |
| 20 | + * @requires PHP 7.3 |
| 21 | + */ |
| 22 | +class HeredocNowdocCloserTest extends AbstractMethodUnitTest |
| 23 | +{ |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. |
| 28 | + * |
| 29 | + * {@internal This is a near duplicate of the original method. Only difference is that |
| 30 | + * tab replacement is enabled for this test.} |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public static function setUpBeforeClass() |
| 35 | + { |
| 36 | + $config = new Config(); |
| 37 | + $config->standards = ['PSR1']; |
| 38 | + $config->tabWidth = 4; |
| 39 | + |
| 40 | + $ruleset = new Ruleset($config); |
| 41 | + |
| 42 | + // Default to a file with the same name as the test class. Extension is property based. |
| 43 | + $relativeCN = str_replace(__NAMESPACE__, '', get_called_class()); |
| 44 | + $relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN); |
| 45 | + $pathToTestFile = realpath(__DIR__).$relativePath.'.'.static::$fileExtension; |
| 46 | + |
| 47 | + // Make sure the file gets parsed correctly based on the file type. |
| 48 | + $contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL; |
| 49 | + $contents .= file_get_contents($pathToTestFile); |
| 50 | + |
| 51 | + self::$phpcsFile = new DummyFile($contents, $ruleset, $config); |
| 52 | + self::$phpcsFile->process(); |
| 53 | + |
| 54 | + }//end setUpBeforeClass() |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * Verify that leading (indent) whitespace in a heredoc/nowdoc closer token get the tab replacement treatment. |
| 59 | + * |
| 60 | + * @param string $testMarker The comment prefacing the target token. |
| 61 | + * @param array $expected Expectations for the token array. |
| 62 | + * |
| 63 | + * @dataProvider dataHeredocNowdocCloserTabReplacement |
| 64 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function testHeredocNowdocCloserTabReplacement($testMarker, $expected) |
| 69 | + { |
| 70 | + $tokens = self::$phpcsFile->getTokens(); |
| 71 | + |
| 72 | + $closer = $this->getTargetToken($testMarker, [T_END_HEREDOC, T_END_NOWDOC]); |
| 73 | + |
| 74 | + foreach ($expected as $key => $value) { |
| 75 | + if ($key === 'orig_content' && $value === null) { |
| 76 | + $this->assertArrayNotHasKey($key, $tokens[$closer], "Unexpected 'orig_content' key found in the token array."); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + $this->assertArrayHasKey($key, $tokens[$closer], "Key $key not found in the token array."); |
| 81 | + $this->assertSame($value, $tokens[$closer][$key], "Value for key $key does not match expectation."); |
| 82 | + } |
| 83 | + |
| 84 | + }//end testHeredocNowdocCloserTabReplacement() |
| 85 | + |
| 86 | + |
| 87 | + /** |
| 88 | + * Data provider. |
| 89 | + * |
| 90 | + * @see testHeredocNowdocCloserTabReplacement() |
| 91 | + * |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function dataHeredocNowdocCloserTabReplacement() |
| 95 | + { |
| 96 | + return [ |
| 97 | + [ |
| 98 | + 'testMarker' => '/* testHeredocCloserNoIndent */', |
| 99 | + 'expected' => [ |
| 100 | + 'length' => 3, |
| 101 | + 'content' => 'EOD', |
| 102 | + 'orig_content' => null, |
| 103 | + ], |
| 104 | + ], |
| 105 | + [ |
| 106 | + 'testMarker' => '/* testNowdocCloserNoIndent */', |
| 107 | + 'expected' => [ |
| 108 | + 'length' => 3, |
| 109 | + 'content' => 'EOD', |
| 110 | + 'orig_content' => null, |
| 111 | + ], |
| 112 | + ], |
| 113 | + [ |
| 114 | + 'testMarker' => '/* testHeredocCloserSpaceIndent */', |
| 115 | + 'expected' => [ |
| 116 | + 'length' => 7, |
| 117 | + 'content' => ' END', |
| 118 | + 'orig_content' => null, |
| 119 | + ], |
| 120 | + ], |
| 121 | + [ |
| 122 | + 'testMarker' => '/* testNowdocCloserSpaceIndent */', |
| 123 | + 'expected' => [ |
| 124 | + 'length' => 8, |
| 125 | + 'content' => ' END', |
| 126 | + 'orig_content' => null, |
| 127 | + ], |
| 128 | + ], |
| 129 | + [ |
| 130 | + 'testMarker' => '/* testHeredocCloserTabIndent */', |
| 131 | + 'expected' => [ |
| 132 | + 'length' => 8, |
| 133 | + 'content' => ' END', |
| 134 | + 'orig_content' => ' END', |
| 135 | + ], |
| 136 | + ], |
| 137 | + [ |
| 138 | + 'testMarker' => '/* testNowdocCloserTabIndent */', |
| 139 | + 'expected' => [ |
| 140 | + 'length' => 7, |
| 141 | + 'content' => ' END', |
| 142 | + 'orig_content' => ' END', |
| 143 | + ], |
| 144 | + ], |
| 145 | + ]; |
| 146 | + |
| 147 | + }//end dataHeredocNowdocCloserTabReplacement() |
| 148 | + |
| 149 | + |
| 150 | +}//end class |
0 commit comments