|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Util\Common::getSniffCode() method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Util\Common; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Util\Common; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for the \PHP_CodeSniffer\Util\Common::getSniffCode() method. |
| 17 | + * |
| 18 | + * @covers \PHP_CodeSniffer\Util\Common::getSniffCode |
| 19 | + */ |
| 20 | +final class GetSniffCodeTest extends TestCase |
| 21 | +{ |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Test receiving an expected exception when the $sniffClass parameter is not passed a string value or is passed an empty string. |
| 26 | + * |
| 27 | + * @param string $input NOT a fully qualified sniff class name. |
| 28 | + * |
| 29 | + * @dataProvider dataGetSniffCodeThrowsExceptionOnInvalidInput |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function testGetSniffCodeThrowsExceptionOnInvalidInput($input) |
| 34 | + { |
| 35 | + $exception = 'InvalidArgumentException'; |
| 36 | + $message = 'The $sniffClass parameter must be a non-empty string'; |
| 37 | + |
| 38 | + if (method_exists($this, 'expectException') === true) { |
| 39 | + // PHPUnit 5+. |
| 40 | + $this->expectException($exception); |
| 41 | + $this->expectExceptionMessage($message); |
| 42 | + } else { |
| 43 | + // PHPUnit 4. |
| 44 | + $this->setExpectedException($exception, $message); |
| 45 | + } |
| 46 | + |
| 47 | + Common::getSniffCode($input); |
| 48 | + |
| 49 | + }//end testGetSniffCodeThrowsExceptionOnInvalidInput() |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Data provider. |
| 54 | + * |
| 55 | + * @see testGetSniffCodeThrowsExceptionOnInvalidInput() |
| 56 | + * |
| 57 | + * @return array<string, array<string>> |
| 58 | + */ |
| 59 | + public static function dataGetSniffCodeThrowsExceptionOnInvalidInput() |
| 60 | + { |
| 61 | + return [ |
| 62 | + 'Class name is not a string' => [true], |
| 63 | + 'Class name is empty' => [''], |
| 64 | + ]; |
| 65 | + |
| 66 | + }//end dataGetSniffCodeThrowsExceptionOnInvalidInput() |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * Test receiving an expected exception when the $sniffClass parameter is not passed a value which |
| 71 | + * could be a fully qualified sniff(test) class name. |
| 72 | + * |
| 73 | + * @param string $input String input which can not be a fully qualified sniff(test) class name. |
| 74 | + * |
| 75 | + * @dataProvider dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass |
| 76 | + * |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass($input) |
| 80 | + { |
| 81 | + $exception = 'InvalidArgumentException'; |
| 82 | + $message = 'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received:'; |
| 83 | + |
| 84 | + if (method_exists($this, 'expectException') === true) { |
| 85 | + // PHPUnit 5+. |
| 86 | + $this->expectException($exception); |
| 87 | + $this->expectExceptionMessage($message); |
| 88 | + } else { |
| 89 | + // PHPUnit 4. |
| 90 | + $this->setExpectedException($exception, $message); |
| 91 | + } |
| 92 | + |
| 93 | + Common::getSniffCode($input); |
| 94 | + |
| 95 | + }//end testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass() |
| 96 | + |
| 97 | + |
| 98 | + /** |
| 99 | + * Data provider. |
| 100 | + * |
| 101 | + * @see testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass() |
| 102 | + * |
| 103 | + * @return array<string, array<string>> |
| 104 | + */ |
| 105 | + public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass() |
| 106 | + { |
| 107 | + return [ |
| 108 | + 'Unqualified class name' => ['ClassName'], |
| 109 | + 'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'], |
| 110 | + 'Fully qualified class name, doesn\'t end on Sniff or UnitTest' => ['Fully\\Sniffs\\Qualified\\ClassName'], |
| 111 | + ]; |
| 112 | + |
| 113 | + }//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass() |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * Test transforming a sniff class name to a sniff code. |
| 118 | + * |
| 119 | + * @param string $fqnClass A fully qualified sniff class name. |
| 120 | + * @param string $expected Expected function output. |
| 121 | + * |
| 122 | + * @dataProvider dataGetSniffCode |
| 123 | + * |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function testGetSniffCode($fqnClass, $expected) |
| 127 | + { |
| 128 | + $this->assertSame($expected, Common::getSniffCode($fqnClass)); |
| 129 | + |
| 130 | + }//end testGetSniffCode() |
| 131 | + |
| 132 | + |
| 133 | + /** |
| 134 | + * Data provider. |
| 135 | + * |
| 136 | + * @see testGetSniffCode() |
| 137 | + * |
| 138 | + * @return array<string, array<string, string>> |
| 139 | + */ |
| 140 | + public static function dataGetSniffCode() |
| 141 | + { |
| 142 | + return [ |
| 143 | + 'PHPCS native sniff' => [ |
| 144 | + 'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Arrays\\ArrayIndentSniff', |
| 145 | + 'expected' => 'Generic.Arrays.ArrayIndent', |
| 146 | + ], |
| 147 | + 'Class is a PHPCS native test class' => [ |
| 148 | + 'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Tests\\Arrays\\ArrayIndentUnitTest', |
| 149 | + 'expected' => 'Generic.Arrays.ArrayIndent', |
| 150 | + ], |
| 151 | + 'Sniff in external standard without namespace prefix' => [ |
| 152 | + 'fqnClass' => 'MyStandard\\Sniffs\\PHP\\MyNameSniff', |
| 153 | + 'expected' => 'MyStandard.PHP.MyName', |
| 154 | + ], |
| 155 | + 'Test in external standard without namespace prefix' => [ |
| 156 | + 'fqnClass' => 'MyStandard\\Tests\\PHP\\MyNameSniff', |
| 157 | + 'expected' => 'MyStandard.PHP.MyName', |
| 158 | + ], |
| 159 | + 'Sniff in external standard with namespace prefix' => [ |
| 160 | + 'fqnClass' => 'Vendor\\Package\\MyStandard\\Sniffs\\Category\\AnalyzeMeSniff', |
| 161 | + 'expected' => 'MyStandard.Category.AnalyzeMe', |
| 162 | + ], |
| 163 | + 'Test in external standard with namespace prefix' => [ |
| 164 | + 'fqnClass' => 'Vendor\\Package\\MyStandard\\Tests\\Category\\AnalyzeMeUnitTest', |
| 165 | + 'expected' => 'MyStandard.Category.AnalyzeMe', |
| 166 | + ], |
| 167 | + ]; |
| 168 | + |
| 169 | + }//end dataGetSniffCode() |
| 170 | + |
| 171 | + |
| 172 | +}//end class |
0 commit comments