|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Base class to use when testing parts of the tokenizer. |
| 4 | + * |
| 5 | + * This is a near duplicate of the AbstractMethodUnitTest class, with the |
| 6 | + * difference being that it allows for recording code coverage for tokenizer tests. |
| 7 | + * |
| 8 | + * @author Juliette Reinders Folmer <[email protected]> |
| 9 | + * @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved. |
| 10 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer; |
| 14 | + |
| 15 | +use PHP_CodeSniffer\Ruleset; |
| 16 | +use PHP_CodeSniffer\Files\DummyFile; |
| 17 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 18 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +abstract class AbstractTokenizerTestCase extends TestCase |
| 22 | +{ |
| 23 | + |
| 24 | + /** |
| 25 | + * The file extension of the test case file (without leading dot). |
| 26 | + * |
| 27 | + * This allows child classes to overrule the default `inc` with, for instance, |
| 28 | + * `js` or `css` when applicable. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $fileExtension = 'inc'; |
| 33 | + |
| 34 | + /** |
| 35 | + * The tab width setting to use when tokenizing the file. |
| 36 | + * |
| 37 | + * This allows for test case files to use a different tab width than the default. |
| 38 | + * |
| 39 | + * @var integer |
| 40 | + */ |
| 41 | + protected $tabWidth = 4; |
| 42 | + |
| 43 | + /** |
| 44 | + * The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file. |
| 45 | + * |
| 46 | + * @var \PHP_CodeSniffer\Files\File |
| 47 | + */ |
| 48 | + protected $phpcsFile; |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. |
| 53 | + * |
| 54 | + * The test case file for a unit test class has to be in the same directory |
| 55 | + * directory and use the same file name as the test class, using the .inc extension. |
| 56 | + * |
| 57 | + * @before |
| 58 | + * |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + protected function initializeFile() |
| 62 | + { |
| 63 | + if (isset($this->phpcsFile) === false) { |
| 64 | + $config = new ConfigDouble(); |
| 65 | + // Also set a tab-width to enable testing tab-replaced vs `orig_content`. |
| 66 | + $config->tabWidth = $this->tabWidth; |
| 67 | + |
| 68 | + $ruleset = new Ruleset($config); |
| 69 | + |
| 70 | + // Default to a file with the same name as the test class. Extension is property based. |
| 71 | + $relativeCN = str_replace(__NAMESPACE__, '', get_called_class()); |
| 72 | + $relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN); |
| 73 | + $pathToTestFile = realpath(__DIR__).$relativePath.'.'.$this->fileExtension; |
| 74 | + |
| 75 | + // Make sure the file gets parsed correctly based on the file type. |
| 76 | + $contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL; |
| 77 | + $contents .= file_get_contents($pathToTestFile); |
| 78 | + |
| 79 | + $this->phpcsFile = new DummyFile($contents, $ruleset, $config); |
| 80 | + $this->phpcsFile->process(); |
| 81 | + } |
| 82 | + |
| 83 | + }//end initializeFile() |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the token pointer for a target token based on a specific comment found on the line before. |
| 88 | + * |
| 89 | + * Note: the test delimiter comment MUST start with "/* test" to allow this function to |
| 90 | + * distinguish between comments used *in* a test and test delimiters. |
| 91 | + * |
| 92 | + * @param string $commentString The delimiter comment to look for. |
| 93 | + * @param int|string|array $tokenType The type of token(s) to look for. |
| 94 | + * @param string $tokenContent Optional. The token content for the target token. |
| 95 | + * |
| 96 | + * @return int |
| 97 | + */ |
| 98 | + protected function getTargetToken($commentString, $tokenType, $tokenContent=null) |
| 99 | + { |
| 100 | + return AbstractMethodUnitTest::getTargetTokenFromFile($this->phpcsFile, $commentString, $tokenType, $tokenContent); |
| 101 | + |
| 102 | + }//end getTargetToken() |
| 103 | + |
| 104 | + |
| 105 | +}//end class |
0 commit comments