Skip to content

Commit 09af586

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 0e26bf8 commit 09af586

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
{
@@ -358,7 +359,10 @@ public function dataRegisteredSniffCodes()
358359
public function testSettingProperties($sniffClass, $propertyName, $expectedValue)
359360
{
360361
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs);
361-
$this->assertObjectHasAttribute($propertyName, self::$ruleset->sniffs[$sniffClass]);
362+
363+
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
364+
$errorMsg = sprintf('Property %s does not exist on sniff class %s', $propertyName, $sniffClass);
365+
$this->assertTrue($hasProperty, $errorMsg);
362366

363367
$actualValue = self::$ruleset->sniffs[$sniffClass]->$propertyName;
364368
$this->assertSame($expectedValue, $actualValue);
@@ -449,7 +453,10 @@ public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFail
449453
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
450454

451455
$sniffObject = self::$ruleset->sniffs[$sniffClass];
452-
$this->assertObjectNotHasAttribute($propertyName, $sniffObject, 'Property '.$propertyName.' registered for sniff '.$sniffClass.' which does not support it');
456+
457+
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
458+
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
459+
$this->assertFalse($hasProperty, $errorMsg);
453460

454461
}//end testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
455462

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.
@@ -135,8 +136,10 @@ public function testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
135136
// Test that the property doesn't get set for the one sniff which doesn't support the property.
136137
$sniffClass = 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\ValidDefaultValueSniff';
137138
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
138-
$sniffObject = $ruleset->sniffs[$sniffClass];
139-
$this->assertObjectNotHasAttribute($propertyName, $sniffObject, 'Property registered for sniff '.$sniffClass.' which does not support it');
139+
140+
$hasProperty = (new ReflectionObject($ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
141+
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
142+
$this->assertFalse($hasProperty, $errorMsg);
140143

141144
}//end testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
142145

0 commit comments

Comments
 (0)