|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit test class for BigBite Coding Standard. |
| 4 | + * |
| 5 | + * @package BigBiteCS\BigBite |
| 6 | + * @link https://github.com/bigbite/phpcs-config |
| 7 | + * @license https://opensource.org/licenses/MIT MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace BigBiteCS\BigBite\Tests\Files; |
| 11 | + |
| 12 | +use BigBiteCS\BigBite\Tests\AbstractSniffUnitTest; |
| 13 | + |
| 14 | +/** |
| 15 | + * Unit test class for the FileName sniff. |
| 16 | + * |
| 17 | + * @package BigBiteCS\BigBite |
| 18 | + */ |
| 19 | +class FileNameUnitTest extends AbstractSniffUnitTest { |
| 20 | + |
| 21 | + /** |
| 22 | + * Error files with the expected nr of errors. |
| 23 | + * |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + private $expected_results = array( |
| 27 | + |
| 28 | + /* |
| 29 | + * In /FileNameUnitTests. |
| 30 | + */ |
| 31 | + |
| 32 | + // File names generic. |
| 33 | + 'some_file.inc' => 1, |
| 34 | + 'SomeFile.inc' => 1, |
| 35 | + 'some-File.inc' => 1, |
| 36 | + 'SomeView.inc' => 1, |
| 37 | + |
| 38 | + // Class file names. |
| 39 | + 'my-class.inc' => 1, |
| 40 | + 'my-abstract-class.inc' => 1, |
| 41 | + 'class-different-class.inc' => 1, |
| 42 | + 'abstract-class-different-class.inc' => 1, |
| 43 | + 'ClassMyClass.inc' => 2, |
| 44 | + 'AbstractClassMyClass.inc' => 2, |
| 45 | + |
| 46 | + // Theme specific exceptions in a non-theme context. |
| 47 | + 'single-my_post_type.inc' => 1, |
| 48 | + 'taxonomy-post_format-post-format-audio.inc' => 1, |
| 49 | + |
| 50 | + /* |
| 51 | + * In /FileNameUnitTests/NonStrictClassNames. |
| 52 | + */ |
| 53 | + |
| 54 | + // Non-strict class names still have to comply with lowercase hyphenated. |
| 55 | + 'ClassNonStrictClass.inc' => 1, |
| 56 | + 'AbstractClassNonStrictClass.inc' => 1, |
| 57 | + |
| 58 | + /* |
| 59 | + * In /FileNameUnitTests/PHPCSAnnotations. |
| 60 | + */ |
| 61 | + |
| 62 | + // Non-strict class names still have to comply with lowercase hyphenated. |
| 63 | + 'blanket-disable.inc' => 0, |
| 64 | + 'non-relevant-disable.inc' => 1, |
| 65 | + 'partial-file-disable.inc' => 1, |
| 66 | + 'rule-disable.inc' => 0, |
| 67 | + 'wordpress-disable.inc' => 0, |
| 68 | + |
| 69 | + /* |
| 70 | + * In /FileNameUnitTests/TestFiles. |
| 71 | + */ |
| 72 | + 'test-sample-phpunit.inc' => 0, |
| 73 | + 'test-sample-phpunit6.inc' => 0, |
| 74 | + 'test-sample-wpunit.inc' => 0, |
| 75 | + 'test-sample-custom-unit.inc' => 0, |
| 76 | + 'test-sample-namespaced-declaration-1.inc' => 0, |
| 77 | + 'test-sample-namespaced-declaration-2.inc' => 1, // Namespaced vs non-namespaced. |
| 78 | + 'test-sample-namespaced-declaration-3.inc' => 1, // Wrong namespace. |
| 79 | + 'test-sample-namespaced-declaration-4.inc' => 1, // Non-namespaced vs namespaced. |
| 80 | + 'test-sample-global-namespace-extends-1.inc' => 0, // Prefixed user input. |
| 81 | + 'test-sample-global-namespace-extends-2.inc' => 1, // Non-namespaced vs namespaced. |
| 82 | + 'test-sample-extends-with-use.inc' => 0, |
| 83 | + 'test-sample-namespaced-extends-1.inc' => 0, |
| 84 | + 'test-sample-namespaced-extends-2.inc' => 1, // Wrong namespace. |
| 85 | + 'test-sample-namespaced-extends-3.inc' => 1, // Namespaced vs non-namespaced. |
| 86 | + 'test-sample-namespaced-extends-4.inc' => 1, // Non-namespaced vs namespaced. |
| 87 | + 'test-sample-namespaced-extends-5.inc' => 0, |
| 88 | + |
| 89 | + /* |
| 90 | + * In /FileNameUnitTests/ThemeExceptions. |
| 91 | + */ |
| 92 | + |
| 93 | + // Files in a theme context. |
| 94 | + 'front_page.inc' => 1, |
| 95 | + 'FrontPage.inc' => 1, |
| 96 | + 'author-nice_name.inc' => 1, |
| 97 | + |
| 98 | + /* |
| 99 | + * In /FileNameUnitTests/wp-includes. |
| 100 | + */ |
| 101 | + |
| 102 | + // Files containing template tags. |
| 103 | + 'general.inc' => 1, |
| 104 | + |
| 105 | + /* |
| 106 | + * In /. |
| 107 | + */ |
| 108 | + |
| 109 | + // Fall-back file in case glob() fails. |
| 110 | + 'FileNameUnitTest.inc' => 1, |
| 111 | + ); |
| 112 | + |
| 113 | + /** |
| 114 | + * Get a list of all test files to check. |
| 115 | + * |
| 116 | + * @param string $testFileBase The base path that the unit tests files will have. |
| 117 | + * |
| 118 | + * @return string[] |
| 119 | + */ |
| 120 | + protected function getTestFiles( $testFileBase ) { |
| 121 | + $sep = \DIRECTORY_SEPARATOR; |
| 122 | + $test_files = glob( dirname( $testFileBase ) . $sep . 'FileNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', \GLOB_BRACE ); |
| 123 | + |
| 124 | + if ( ! empty( $test_files ) ) { |
| 125 | + return $test_files; |
| 126 | + } |
| 127 | + |
| 128 | + return array( $testFileBase . '.inc' ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns the lines where errors should occur. |
| 133 | + * |
| 134 | + * @param string $testFile The name of the file being tested. |
| 135 | + * @return array <int line number> => <int number of errors> |
| 136 | + */ |
| 137 | + public function getErrorList( $testFile = '' ) { |
| 138 | + if ( isset( $this->expected_results[ $testFile ] ) ) { |
| 139 | + return array( |
| 140 | + 1 => $this->expected_results[ $testFile ], |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + return array(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns the lines where warnings should occur. |
| 149 | + * |
| 150 | + * @return array <int line number> => <int number of warnings> |
| 151 | + */ |
| 152 | + public function getWarningList() { |
| 153 | + return array(); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments