From f4f22b6e3e1ce45a1a98c04f87da08a56817b993 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 23 Feb 2024 09:52:34 +0100 Subject: [PATCH] Drop support for the `AssertAttributeHelper` This "helper" was only intended as a temporary measure to buy people some more time to refactor their tests. --- README.md | 35 ----- phpunitpolyfills-autoload.php | 1 - src/Helpers/AssertAttributeHelper.php | 70 ---------- src/TestCases/TestCasePHPUnitGte8.php | 2 - src/TestCases/TestCasePHPUnitLte7.php | 2 - src/TestCases/XTestCase.php | 2 - tests/Helpers/AssertAttributesHelperTest.php | 122 ------------------ .../Helpers/Fixtures/ClassWithProperties.php | 47 ------- 8 files changed, 281 deletions(-) delete mode 100644 src/Helpers/AssertAttributeHelper.php delete mode 100644 tests/Helpers/AssertAttributesHelperTest.php delete mode 100644 tests/Helpers/Fixtures/ClassWithProperties.php diff --git a/README.md b/README.md index d77816e..47a5098 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,6 @@ Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit - [Use with PHPUnit < 7.5.0](#use-with-phpunit--750) * [Features](#features) - [Polyfill traits](#polyfill-traits) - - [Helper traits](#helper-traits) - [TestCases](#testcases) - [TestListener](#testlistener) * [Frequently Asked Questions](#frequently-asked-questions) @@ -426,38 +425,6 @@ These methods were later backported to the PHPUnit 9 branch and included in the [`Assert::assertObjectNotHasProperty()`]: https://docs.phpunit.de/en/main/assertions.html#assertObjectHasProperty -### Helper traits - -#### `Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper` - -Helper to work around the removal of the `assertAttribute*()` methods. - -The `assertAttribute*()` methods were deprecated in PHPUnit 8.0.0 and removed in PHPUnit 9.0.0. - -Public properties can still be tested by accessing them directly: -```php -$this->assertSame( 'value', $obj->propertyName ); -``` - -Protected and private properties can no longer be tested using PHPUnit native functionality. -The reasoning for the removal of these assertion methods is that _private and protected properties are an implementation detail and should not be tested directly, but via methods in the class_. - -It is strongly recommended to refactor your tests, and if needs be, your classes to adhere to this. - -However, if for some reason the value of `protected` or `private` properties still needs to be tested, this helper can be used to get access to their value and attributes. - -The trait contains two helper methods: -* `public static getProperty( object $classInstance, string $propertyName ) : ReflectionProperty` -* `public static getPropertyValue( object $classInstance, string $propertyName ) : mixed` - -```php -// Test the value of a protected or private property. -$this->assertSame( 'value', $this->getPropertyValue( $objInstance, $propertyName ) ); - -// Retrieve a ReflectionProperty object to test other details of the property. -self::assertSame( $propertyName, self::getProperty( $objInstance, $propertyName )->getName() ); -``` - ### TestCases PHPUnit 8.0.0 introduced a `void` return type declaration to the ["fixture" methods] - `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. @@ -655,11 +622,9 @@ For frequently used, removed PHPUnit functionality, "helpers" may be provided. T | PHPUnit | Removed | Issue | Remarks | | ------- | --------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | 9.0.0 | `assertArraySubset()` | [#1][issue #1] | The [`dms/phpunit-arraysubset-asserts`](https://packagist.org/packages/dms/phpunit-arraysubset-asserts) package polyfills this functionality.
As of [version 0.3.0](https://github.com/rdohms/phpunit-arraysubset-asserts/releases/tag/v0.3.0) this package can be installed in combination with PHP 5.4 - current and PHPUnit 4.8.36/5.7.21 - current.
Alternatively, tests can be refactored using the patterns outlined in [issue #1]. | -| 9.0.0 | `assertAttribute*()` | [#2][issue #2] | Refactor the tests to not directly test private/protected properties.
As an interim solution, the [`Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper`](#yoastphpunitpolyfillshelpersassertattributehelper) trait is available. | | 10.0.0 | `expectDeprecation*()` et al | [#186][issue #186] | A [custom polyfill approach tutorial](https://github.com/Yoast/PHPUnit-Polyfills/issues/186#issuecomment-2334326687) is available. Alternatively, tests can be refactored to skip running the `expectDeprecation*()` et al or skip the test completely on PHPUnit 10, while still running them on <= PHPUnit 9. | [issue #1]: https://github.com/Yoast/PHPUnit-Polyfills/issues/1 -[issue #2]: https://github.com/Yoast/PHPUnit-Polyfills/issues/2 [issue #186]: https://github.com/Yoast/PHPUnit-Polyfills/issues/186 ### Q: Can this library be used when the tests are being run via a PHPUnit Phar file ? diff --git a/phpunitpolyfills-autoload.php b/phpunitpolyfills-autoload.php index f8ca1bb..7b6aac6 100644 --- a/phpunitpolyfills-autoload.php +++ b/phpunitpolyfills-autoload.php @@ -93,7 +93,6 @@ public static function load( $className ) { /* * Handles: * - Yoast\PHPUnitPolyfills\Exceptions\InvalidComparisonMethodException - * - Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper * - Yoast\PHPUnitPolyfills\Helpers\ResourceHelper * - Yoast\PHPUnitPolyfills\TestCases\XTestCase * - Yoast\PHPUnitPolyfills\TestListeners\TestListenerSnakeCaseMethods diff --git a/src/Helpers/AssertAttributeHelper.php b/src/Helpers/AssertAttributeHelper.php deleted file mode 100644 index ab33b2e..0000000 --- a/src/Helpers/AssertAttributeHelper.php +++ /dev/null @@ -1,70 +0,0 @@ -assertSame( 'value', $obj->propertyName ); - * ``` - * - * Protected and private properties can no longer be tested using PHPUnit native - * functionality. - * The reasoning for the removal of these assertion methods is that _private and - * protected properties are an implementation detail and should not be tested - * directly, but via methods in the class_. - * - * It is strongly recommended to refactor your tests, and if needs be, your classes - * to adhere to this. - * - * However, if for some reason the value of protected or private properties still - * needs to be tested, this helper can be used to get access to their value. - * ```php - * $this->assertSame( 'value', $this->getPropertyValue( $obj, $propertyName ) ); - * ``` - * - * @since 0.2.0 - */ -trait AssertAttributeHelper { - - /** - * Retrieve a private or protected property in an object. - * - * @param object $objInstance The object. - * @param string $propertyName The name of property to retrieve. - * - * @return ReflectionProperty - * - * @throws ReflectionException When a non-existent property is requested. - */ - final public static function getProperty( $objInstance, $propertyName ) { - $reflect = new ReflectionObject( $objInstance ); - $property = $reflect->getProperty( $propertyName ); - $property->setAccessible( true ); - - return $property; - } - - /** - * Retrieve the current value of a private or protected property in an object. - * - * @param object $objInstance The object. - * @param string $propertyName The name of property for which to retrieve the value. - * - * @return mixed - */ - final public static function getPropertyValue( $objInstance, $propertyName ) { - $property = static::getProperty( $objInstance, $propertyName ); - - return $property->getValue( $objInstance ); - } -} diff --git a/src/TestCases/TestCasePHPUnitGte8.php b/src/TestCases/TestCasePHPUnitGte8.php index 7d89e8a..2bc12e8 100644 --- a/src/TestCases/TestCasePHPUnitGte8.php +++ b/src/TestCases/TestCasePHPUnitGte8.php @@ -3,7 +3,6 @@ namespace Yoast\PHPUnitPolyfills\TestCases; use PHPUnit\Framework\TestCase as PHPUnit_TestCase; -use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\AssertIgnoringLineEndings; @@ -25,7 +24,6 @@ */ abstract class TestCase extends PHPUnit_TestCase { - use AssertAttributeHelper; use AssertClosedResource; use AssertFileEqualsSpecializations; use AssertIgnoringLineEndings; diff --git a/src/TestCases/TestCasePHPUnitLte7.php b/src/TestCases/TestCasePHPUnitLte7.php index b1a6464..a662511 100644 --- a/src/TestCases/TestCasePHPUnitLte7.php +++ b/src/TestCases/TestCasePHPUnitLte7.php @@ -3,7 +3,6 @@ namespace Yoast\PHPUnitPolyfills\TestCases; use PHPUnit\Framework\TestCase as PHPUnit_TestCase; -use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations; @@ -28,7 +27,6 @@ */ abstract class TestCase extends PHPUnit_TestCase { - use AssertAttributeHelper; use AssertClosedResource; use AssertEqualsSpecializations; use AssertFileEqualsSpecializations; diff --git a/src/TestCases/XTestCase.php b/src/TestCases/XTestCase.php index acde81b..ff90f6a 100644 --- a/src/TestCases/XTestCase.php +++ b/src/TestCases/XTestCase.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\Attributes\Before; use PHPUnit\Framework\Attributes\BeforeClass; use PHPUnit\Framework\TestCase as PHPUnit_TestCase; -use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations; @@ -34,7 +33,6 @@ */ abstract class XTestCase extends PHPUnit_TestCase { - use AssertAttributeHelper; use AssertClosedResource; use AssertEqualsSpecializations; use AssertFileEqualsSpecializations; diff --git a/tests/Helpers/AssertAttributesHelperTest.php b/tests/Helpers/AssertAttributesHelperTest.php deleted file mode 100644 index cf383e1..0000000 --- a/tests/Helpers/AssertAttributesHelperTest.php +++ /dev/null @@ -1,122 +0,0 @@ -instance = new ClassWithProperties(); - } - - /** - * Test retrieving information on the public property in its original state. - * - * @return void - */ - public function testOriginalStatePublicProperty() { - $this->assertNull( $this->instance->public_prop ); - $this->assertNull( $this->getPropertyValue( $this->instance, 'public_prop' ) ); - $this->assertTrue( $this->getProperty( $this->instance, 'public_prop' )->isDefault() ); - } - - /** - * Test retrieving information on the protected property in its original state. - * - * @return void - */ - public function testOriginalStateProtectedProperty() { - $this->assertNull( self::getPropertyValue( $this->instance, 'protected_prop' ) ); - $this->assertTrue( $this->getProperty( $this->instance, 'protected_prop' )->isDefault() ); - } - - /** - * Test retrieving information on the private property in its original state. - * - * @return void - */ - public function testOriginalStatePrivateProperty() { - $this->assertFalse( $this->getPropertyValue( $this->instance, 'private_prop' ) ); - $this->assertTrue( static::getProperty( $this->instance, 'private_prop' )->isDefault() ); - } - - /** - * Test receiving an exception for a non-existent dynamic property. - * - * @return void - */ - public function testOriginalStateDynamicProperty() { - $this->expectException( ReflectionException::class ); - - $this->getPropertyValue( $this->instance, 'dynamic' ); - } - - /** - * Test retrieving information on the public property once it has been set. - * - * @return void - */ - public function testPropertyValueOnceSetPublicProperty() { - $this->instance->setProperties(); - - $this->assertSame( 'public', $this->instance->public_prop ); - $this->assertSame( 'public', $this->getPropertyValue( $this->instance, 'public_prop' ) ); - } - - /** - * Test retrieving information on the protected property once it has been set. - * - * @return void - */ - public function testPropertyValueOnceSetProtectedProperty() { - $this->instance->setProperties(); - - $this->assertSame( 100, $this->getPropertyValue( $this->instance, 'protected_prop' ) ); - } - - /** - * Test retrieving information on the private property once it has been set. - * - * @return void - */ - public function testPropertyValueOnceSetPrivateProperty() { - $this->instance->setProperties(); - - $this->assertTrue( $this->getPropertyValue( $this->instance, 'private_prop' ) ); - } - - /** - * Test retrieving information on the dynamic property once it has been set. - * - * @return void - */ - public function testPropertyValueOnceSetDynamicProperty() { - $this->instance->setProperties(); - - $this->assertInstanceOf( ClassWithProperties::class, $this->getPropertyValue( $this->instance, 'dynamic' ) ); - $this->assertFalse( $this->getProperty( $this->instance, 'dynamic' )->isDefault() ); - } -} diff --git a/tests/Helpers/Fixtures/ClassWithProperties.php b/tests/Helpers/Fixtures/ClassWithProperties.php deleted file mode 100644 index 0f2701e..0000000 --- a/tests/Helpers/Fixtures/ClassWithProperties.php +++ /dev/null @@ -1,47 +0,0 @@ -public_prop = 'public'; - $this->protected_prop = 100; - $this->private_prop = true; - - // Set a non-predefined property. - $this->dynamic = new self(); - } -}