1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<?php
/**
* Tests for the \PHP_CodeSniffer\Ruleset class.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHPUnit\Framework\TestCase;
class RuleInclusionTest extends TestCase
{
/**
* The Ruleset object.
*
* @var \PHP_CodeSniffer\Ruleset
*/
protected static $ruleset;
/**
* Path to the ruleset file.
*
* @var string
*/
private static $standard = '';
/**
* The original content of the ruleset.
*
* @var string
*/
private static $contents = '';
/**
* Initialize the test.
*
* @return void
*/
public function setUp(): void
{
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
// PEAR installs test and sniff files into different locations
// so these tests will not pass as they directly reference files
// by relative location.
$this->markTestSkipped('Test cannot run from a PEAR install');
}
}//end setUp()
/**
* Initialize the config and ruleset objects based on the `RuleInclusionTest.xml` ruleset file.
*
* @return void
*/
public static function setUpBeforeClass(): void
{
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
// This test will be skipped.
return;
}
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
self::$standard = $standard;
// On-the-fly adjust the ruleset test file to be able to test
// sniffs included with relative paths.
$contents = file_get_contents($standard);
self::$contents = $contents;
$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));
$newPath = $repoRootDir;
if (DIRECTORY_SEPARATOR === '\\') {
$newPath = str_replace('\\', '/', $repoRootDir);
}
$adjusted = str_replace('%path_root_dir%', $newPath, $contents);
if (file_put_contents($standard, $adjusted) === false) {
self::markTestSkipped('On the fly ruleset adjustment failed');
}
$config = new Config(["--standard=$standard"]);
self::$ruleset = new Ruleset($config);
}//end setUpBeforeClass()
/**
* Reset ruleset file.
*
* @return void
*/
public function tearDown(): void
{
file_put_contents(self::$standard, self::$contents);
}//end tearDown()
/**
* Test that sniffs are registered.
*
* @return void
*/
public function testHasSniffCodes()
{
$this->assertObjectHasAttribute('sniffCodes', self::$ruleset);
$this->assertCount(14, self::$ruleset->sniffCodes);
}//end testHasSniffCodes()
/**
* Test that sniffs are correctly registered, independently on the syntax used to include the sniff.
*
* @param string $key Expected array key.
* @param string $value Expected array value.
*
* @dataProvider dataRegisteredSniffCodes
*
* @return void
*/
public function testRegisteredSniffCodes($key, $value)
{
$this->assertArrayHasKey($key, self::$ruleset->sniffCodes);
$this->assertSame($value, self::$ruleset->sniffCodes[$key]);
}//end testRegisteredSniffCodes()
/**
* Data provider.
*
* @see self::testRegisteredSniffCodes()
*
* @return array
*/
public function dataRegisteredSniffCodes()
{
return [
[
'PSR1.Classes.ClassDeclaration',
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff',
],
[
'PSR1.Files.SideEffects',
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Files\SideEffectsSniff',
],
[
'PSR1.Methods.CamelCapsMethodName',
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Methods\CamelCapsMethodNameSniff',
],
[
'Generic.PHP.DisallowAlternativePHPTags',
'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DisallowAlternativePHPTagsSniff',
],
[
'Generic.PHP.DisallowShortOpenTag',
'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DisallowShortOpenTagSniff',
],
[
'Generic.Files.ByteOrderMark',
'PHP_CodeSniffer\Standards\Generic\Sniffs\Files\ByteOrderMarkSniff',
],
[
'Squiz.Classes.ValidClassName',
'PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ValidClassNameSniff',
],
[
'Generic.NamingConventions.UpperCaseConstantName',
'PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\UpperCaseConstantNameSniff',
],
[
'Zend.NamingConventions.ValidVariableName',
'PHP_CodeSniffer\Standards\Zend\Sniffs\NamingConventions\ValidVariableNameSniff',
],
[
'Generic.Arrays.ArrayIndent',
'PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff',
],
[
'Generic.Metrics.CyclomaticComplexity',
'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\CyclomaticComplexitySniff',
],
[
'Generic.Files.LineLength',
'PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff',
],
[
'Generic.NamingConventions.CamelCapsFunctionName',
'PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff',
],
[
'Generic.Metrics.NestingLevel',
'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\NestingLevelSniff',
],
];
}//end dataRegisteredSniffCodes()
/**
* Test that setting properties for standards, categories, sniffs works for all supported rule
* inclusion methods.
*
* @param string $sniffClass The name of the sniff class.
* @param string $propertyName The name of the changed property.
* @param mixed $expectedValue The value expected for the property.
*
* @dataProvider dataSettingProperties
*
* @return void
*/
public function testSettingProperties($sniffClass, $propertyName, $expectedValue)
{
$this->assertObjectHasAttribute('sniffs', self::$ruleset);
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs);
$this->assertObjectHasAttribute($propertyName, self::$ruleset->sniffs[$sniffClass]);
$actualValue = self::$ruleset->sniffs[$sniffClass]->$propertyName;
$this->assertSame($expectedValue, $actualValue);
}//end testSettingProperties()
/**
* Data provider.
*
* @see self::testSettingProperties()
*
* @return array
*/
public function dataSettingProperties()
{
return [
'ClassDeclarationSniff' => [
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff',
'setforallsniffs',
true,
],
'SideEffectsSniff' => [
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Files\SideEffectsSniff',
'setforallsniffs',
true,
],
'ValidVariableNameSniff' => [
'PHP_CodeSniffer\Standards\Zend\Sniffs\NamingConventions\ValidVariableNameSniff',
'setforallincategory',
true,
],
'ArrayIndentSniff' => [
'PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff',
'indent',
'2',
],
'LineLengthSniff' => [
'PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff',
'lineLimit',
'10',
],
'CamelCapsFunctionNameSniff' => [
'PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff',
'strict',
false,
],
'NestingLevelSniff-nestingLevel' => [
'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\NestingLevelSniff',
'nestingLevel',
'2',
],
'NestingLevelSniff-setforsniffsinincludedruleset' => [
'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\NestingLevelSniff',
'setforsniffsinincludedruleset',
true,
],
// Testing that setting a property at error code level does *not* work.
'CyclomaticComplexitySniff' => [
'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\CyclomaticComplexitySniff',
'complexity',
10,
],
];
}//end dataSettingProperties()
}//end class
|