Skip to content

Commit

Permalink
AssertClosedResource: improve error messages
Browse files Browse the repository at this point in the history
The `sebastian/exporter` package has been a dependency of PHPUnit from before PHPUnit 4.8.x (minimum supported version by this library), so it is safe to use the same variable export methodology as PHPUnit itself uses.

The class is prefixed via PHP_Scoper when it is includes as part of a PHPUnit Phar file and the toggle added will make sure the class can be loaded both when run via a Composer install, as well as when run via a Phar file.

Also note that the Exporter reports closed resources as `NULL`. This has been reported upstream via sebastianbergmann/exporter#36 and for the polyfill a fix has been put in place to work around this.

**Important**:
While the `sebastian/exporter` package has , in effect, now become a direct dependency of the Polyfills, I'm not going to declare it as such in the `composer.json` file as for all supported PHPUnit versions, the package will already be available via PHPUnit itself, whether installed via Composer or as a PHAR.
This prevent potential version conflicts when tests are run via the PHAR, while the Polyfills have been installed via Composer and saves hassle of having to take care of autoloading the Exporter file, while it will be loaded for PHPUnit itself anyway.
  • Loading branch information
jrfnl committed Nov 11, 2021
1 parent 0670f2e commit d7c0982
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Polyfills/AssertClosedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Yoast\PHPUnitPolyfills\Polyfills;

use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use SebastianBergmann\Exporter\Exporter;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;

/**
Expand All @@ -24,7 +26,8 @@ trait AssertClosedResource {
*/
public static function assertIsClosedResource( $actual, $message = '' ) {
if ( $message === '' ) {
$message = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', \var_export( $actual, true ) );
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
$message = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', $exporter->export( $actual ) );
}

static::assertTrue( ResourceHelper::isClosedResource( $actual ), $message );
Expand All @@ -40,7 +43,12 @@ public static function assertIsClosedResource( $actual, $message = '' ) {
*/
public static function assertIsNotClosedResource( $actual, $message = '' ) {
if ( $message === '' ) {
$message = \sprintf( 'Failed asserting that %s is not of type "resource (closed)"', \var_export( $actual, true ) );
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
$type = $exporter->export( $actual );
if ( $type === 'NULL' ) {
$type = 'resource (closed)';
}
$message = \sprintf( 'Failed asserting that %s is not of type "resource (closed)"', $type );
}

static::assertFalse( ResourceHelper::isClosedResource( $actual ), $message );
Expand Down
8 changes: 6 additions & 2 deletions tests/Polyfills/AssertClosedResourceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ public function isClosedResourceExpectExceptionOnOpenResource( $actual ) {
* @return void
*/
public function isNotClosedResourceExpectExceptionOnClosedResource( $actual ) {
$msg = 'Failed asserting that NULL is not of type "resource (closed)"';
/*
* PHPUnit itself will report closed resources as `NULL` prior to Exporter 3.0.4/4.1.4.
* See: https://github.com/sebastianbergmann/exporter/pull/37
*/
$pattern = '`^Failed asserting that (resource \(closed\)|NULL) is not of type "resource \(closed\)"`';
$exception = 'PHPUnit\Framework\AssertionFailedError';
if ( \class_exists( 'PHPUnit_Framework_AssertionFailedError' ) ) {
// PHPUnit < 6.
$exception = 'PHPUnit_Framework_AssertionFailedError';
}

$this->expectException( $exception );
$this->expectExceptionMessage( $msg );
$this->expectExceptionMessageMatches( $pattern );

self::assertIsNotClosedResource( $actual );
}
Expand Down

0 comments on commit d7c0982

Please sign in to comment.