New AssertClosedResource trait: polyfill the Assert::assertIs[Not]ClosedResource() methods#27
Merged
Conversation
…esource() methods
PHPUnit 9.3.0 introduced the new `Assert::assertIsClosedResource()` and `Assert::assertIsNotClosedResource()` methods.
This commit:
* Adds two traits with the same name.
One to polyfill the methods when not available in PHPUnit.
The other to allow for `use`-ing the trait in PHPUnit versions in which the methods are already natively available.
* Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used.
* A `ResourceHelper` helper class containing the actual logic to determine whether something is a resource and whether that resource is open or closed.
As the PHP native functionality used in PHPUnit itself was only added to PHP in PHP 7.2.0 and the polyfills are available to older PHP versions, this polyfill needs actual logic and doesn't just fall through to an underlying PHPUnit native method.
With that in mind, an availability test is not sufficient to test this.
Additionally, resources in various extensions may behave and identify differently, so this commit also adds extensive tests with a variety of functionality using resources in PHP.
Note: the methods use `static::` to call the PHPUnit native functionality. This allows for existing method overloads in a child class of the PHPUnit native `TestCase` to be respected.
Includes:
* Adding information on the new polyfill to the README.
* Adding the new polyfill to the existing `TestCases` classes.
* Adding a few select exceptions to the PHPCS ruleset.
…helper function
While creating tests for this polyfill, I discovered that for resources created via the `libxml` extension, the PHP native functions showed some curious behaviour.
This behaviour basically makes the "is closed resource" determination unreliable for a select group of PHP versions in combination with resources created via the `libxml` extension.
To prevent tests failing on this, calls to the `assertIs[Not]ClosedResource()` method should - selectively - be wrapped in a condition checking the output of the `AssertClosedResource::shouldClosedResourceAssertionBeSkipped()` method, like so:
```php
if ( $this->shouldClosedResourceAssertionBeSkipped( $actual ) === false ) {
$this->assertIsClosedResource( $actual );
}
```
As the PHPUnit native implementation of the assertions is also afflicted by the underlying bugs in PHP itself, this helper method is also available when the "empty" version of the `AssertClosedResource` trait is loaded and can therefore safely be used PHPUnit cross-version.
As things stand, the `libxml` inconsistency is the only one I have discovered so far.
However, there are quite a few extensions producing resources which are currently not covered in the tests as that would require databases or ssh/ftp/mail credentials to be added to the tests.
Users are encouraged to report other inconsistencies found and the `ResourceHelper::isResourceStateReliable()` method can be expanded with additional inconsistencies when found in the future.
Includes tests for this trait in combination with the `libxml` extension.
Also adds calls to a few of the other tests to ensure that the `shouldClosedResourceAssertionBeSkipped()` correctly returns `false` in all non-affected situations.
Collaborator
Author
|
For the record - comparison of resource determination functions as used in the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AssertClosedResource trait: polyfill the
Assert::assertIs[Not]ClosedResource()methodsPHPUnit 9.3.0 introduced the new
Assert::assertIsClosedResource()andAssert::assertIsNotClosedResource()methods.This commit:
One to polyfill the methods when not available in PHPUnit.
The other to allow for
use-ing the trait in PHPUnit versions in which the methods are already natively available.ResourceHelperhelper class containing the actual logic to determine whether something is a resource and whether that resource is open or closed.As the PHP native functionality used in PHPUnit itself was only added to PHP in PHP 7.2.0 and the polyfills are available to older PHP versions, this polyfill needs actual logic and doesn't just fall through to an underlying PHPUnit native method.
With that in mind, an availability test is not sufficient to test this.
Additionally, resources in various extensions may behave and identify differently, so this commit also adds extensive tests with a variety of functionality using resources in PHP.
Note: the methods use
static::to call the PHPUnit native functionality. This allows for existing method overloads in a child class of the PHPUnit nativeTestCaseto be respected.Includes:
TestCasesclasses.AssertClosedResource: add shouldClosedResourceAssertionBeSkipped() helper function
While creating tests for this polyfill, I discovered that for resources created via the
libxmlextension, the PHP native functions showed some curious behaviour.This behaviour basically makes the "is closed resource" determination unreliable for a select group of PHP versions in combination with resources created via the
libxmlextension.To prevent tests failing on this, calls to the
assertIs[Not]ClosedResource()method should - selectively - be wrapped in a condition checking the output of theAssertClosedResource::shouldClosedResourceAssertionBeSkipped()method, like so:As the PHPUnit native implementation of the assertions is also afflicted by the underlying bugs in PHP itself, this helper method is also available when the "empty" version of the
AssertClosedResourcetrait is loaded and can therefore safely be used PHPUnit cross-version.As things stand, the
libxmlinconsistency is the only one I have discovered so far.However, there are quite a few extensions producing resources which are currently not covered in the tests as that would require databases or ssh/ftp/mail credentials to be added to the tests.
Users are encouraged to report other inconsistencies found and the
ResourceHelper::isResourceStateReliable()method can be expanded with additional inconsistencies when found in the future.Includes tests for this trait in combination with the
libxmlextension.Also adds calls to a few of the other tests to ensure that the
shouldClosedResourceAssertionBeSkipped()correctly returnsfalsein all non-affected situations.Fixes #4