Skip to content

Commit 86e363c

Browse files
committed
Ruleset Tests: work round removal of assertObjectHasAttribute()
The `assertObjectHasAttribute()` method was deprecated in PHPUnit 9.6.x and removed in PHPUnit 10.0.0 without replacement. Note: PHPUnit 10.1.0 adds the assertion back again, but under a different name `assertObjectHasProperty()`. While only a deprecation warning is shown on PHPUnit 9.6.x and the tests will still pass, I'm electing to replace the assertion anyway with code which emulates what PHPUnit would assert.
1 parent b4edac2 commit 86e363c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Config;
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHPUnit\Framework\TestCase;
15+
use ReflectionObject;
1516

1617
class RuleInclusionTest extends TestCase
1718
{
@@ -336,7 +337,10 @@ public function dataRegisteredSniffCodes()
336337
public function testSettingProperties($sniffClass, $propertyName, $expectedValue)
337338
{
338339
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs);
339-
$this->assertObjectHasAttribute($propertyName, self::$ruleset->sniffs[$sniffClass]);
340+
341+
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
342+
$errorMsg = sprintf('Property %s does not exist on sniff class %s', $propertyName, $sniffClass);
343+
$this->assertTrue($hasProperty, $errorMsg);
340344

341345
$actualValue = self::$ruleset->sniffs[$sniffClass]->$propertyName;
342346
$this->assertSame($expectedValue, $actualValue);
@@ -427,7 +431,10 @@ public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFail
427431
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
428432

429433
$sniffObject = self::$ruleset->sniffs[$sniffClass];
430-
$this->assertObjectNotHasAttribute($propertyName, $sniffObject, 'Property '.$propertyName.' registered for sniff '.$sniffClass.' which does not support it');
434+
435+
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
436+
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
437+
$this->assertFalse($hasProperty, $errorMsg);
431438

432439
}//end testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
433440

tests/Core/Ruleset/SetSniffPropertyTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Config;
1313
use PHP_CodeSniffer\Ruleset;
1414
use PHPUnit\Framework\TestCase;
15+
use ReflectionObject;
1516

1617
/**
1718
* These tests specifically focus on the changes made to work around the PHP 8.2 dynamic properties deprecation.
@@ -118,8 +119,10 @@ public function testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
118119
// Test that the property doesn't get set for the one sniff which doesn't support the property.
119120
$sniffClass = 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\ValidDefaultValueSniff';
120121
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
121-
$sniffObject = $ruleset->sniffs[$sniffClass];
122-
$this->assertObjectNotHasAttribute($propertyName, $sniffObject, 'Property registered for sniff '.$sniffClass.' which does not support it');
122+
123+
$hasProperty = (new ReflectionObject($ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
124+
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
125+
$this->assertFalse($hasProperty, $errorMsg);
123126

124127
}//end testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
125128

0 commit comments

Comments
 (0)