|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Base class to use when testing utility methods. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 6 | + * @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved. |
| 7 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Config; |
| 13 | +use PHP_CodeSniffer\Ruleset; |
| 14 | +use PHP_CodeSniffer\Files\DummyFile; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +abstract class AbstractMethodUnitTest extends TestCase |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * The file extension of the test case file (without leading dot). |
| 22 | + * |
| 23 | + * This allows child classes to overrule the default `inc` with, for instance, |
| 24 | + * `js` or `css` when applicable. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected static $fileExtension = 'inc'; |
| 29 | + |
| 30 | + /** |
| 31 | + * The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file. |
| 32 | + * |
| 33 | + * @var \PHP_CodeSniffer\Files\File |
| 34 | + */ |
| 35 | + protected static $phpcsFile; |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file. |
| 40 | + * |
| 41 | + * The test case file for a unit test class has to be in the same directory |
| 42 | + * directory and use the same file name as the test class, using the .inc extension. |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public static function setUpBeforeClass() |
| 47 | + { |
| 48 | + $config = new Config(); |
| 49 | + $config->standards = ['PSR1']; |
| 50 | + |
| 51 | + $ruleset = new Ruleset($config); |
| 52 | + |
| 53 | + // Default to a file with the same name as the test class. Extension is property based. |
| 54 | + $relativeCN = str_replace(__NAMESPACE__, '', get_called_class()); |
| 55 | + $relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN); |
| 56 | + $pathToTestFile = realpath(__DIR__).$relativePath.'.'.static::$fileExtension; |
| 57 | + |
| 58 | + // Make sure the file gets parsed correctly based on the file type. |
| 59 | + $contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL; |
| 60 | + $contents .= file_get_contents($pathToTestFile); |
| 61 | + |
| 62 | + self::$phpcsFile = new DummyFile($contents, $ruleset, $config); |
| 63 | + self::$phpcsFile->process(); |
| 64 | + |
| 65 | + }//end setUpBeforeClass() |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * Clean up after finished test. |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public static function tearDownAfterClass() |
| 74 | + { |
| 75 | + self::$phpcsFile = null; |
| 76 | + |
| 77 | + }//end tearDownAfterClass() |
| 78 | + |
| 79 | + |
| 80 | +}//end class |
0 commit comments