Skip to content

Commit 981b266

Browse files
authored
Merge pull request #97 from Yoast/feature/improve-error-messages
Polyfills: improve handling of custom error message
2 parents 07ad708 + e873683 commit 981b266

File tree

6 files changed

+234
-36
lines changed

6 files changed

+234
-36
lines changed

src/Polyfills/AssertClosedResource.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ trait AssertClosedResource {
2525
* @return void
2626
*/
2727
public static function assertIsClosedResource( $actual, $message = '' ) {
28-
if ( $message === '' ) {
29-
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
30-
$message = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', $exporter->export( $actual ) );
28+
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
29+
$msg = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', $exporter->export( $actual ) );
30+
31+
if ( $message !== '' ) {
32+
$msg = $message . \PHP_EOL . $msg;
3133
}
3234

33-
static::assertTrue( ResourceHelper::isClosedResource( $actual ), $message );
35+
static::assertTrue( ResourceHelper::isClosedResource( $actual ), $msg );
3436
}
3537

3638
/**
@@ -42,16 +44,18 @@ public static function assertIsClosedResource( $actual, $message = '' ) {
4244
* @return void
4345
*/
4446
public static function assertIsNotClosedResource( $actual, $message = '' ) {
45-
if ( $message === '' ) {
46-
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
47-
$type = $exporter->export( $actual );
48-
if ( $type === 'NULL' ) {
49-
$type = 'resource (closed)';
50-
}
51-
$message = \sprintf( 'Failed asserting that %s is not of type "resource (closed)"', $type );
47+
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
48+
$type = $exporter->export( $actual );
49+
if ( $type === 'NULL' ) {
50+
$type = 'resource (closed)';
51+
}
52+
$msg = \sprintf( 'Failed asserting that %s is not of type "resource (closed)"', $type );
53+
54+
if ( $message !== '' ) {
55+
$msg = $message . \PHP_EOL . $msg;
5256
}
5357

54-
static::assertFalse( ResourceHelper::isClosedResource( $actual ), $message );
58+
static::assertFalse( ResourceHelper::isClosedResource( $actual ), $msg );
5559
}
5660

5761
/**

src/Polyfills/AssertFileDirectory.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public static function assertIsReadable( $filename, $message = '' ) {
3232
throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'string' );
3333
}
3434

35-
if ( $message === '' ) {
36-
$message = \sprintf( 'Failed asserting that "%s" is readable', $filename );
35+
$msg = \sprintf( 'Failed asserting that "%s" is readable', $filename );
36+
if ( $message !== '' ) {
37+
$msg = $message . \PHP_EOL . $msg;
3738
}
3839

39-
static::assertTrue( \is_readable( $filename ), $message );
40+
static::assertTrue( \is_readable( $filename ), $msg );
4041
}
4142

4243
/**
@@ -54,11 +55,12 @@ public static function assertNotIsReadable( $filename, $message = '' ) {
5455
throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'string' );
5556
}
5657

57-
if ( $message === '' ) {
58-
$message = \sprintf( 'Failed asserting that "%s" is not readable', $filename );
58+
$msg = \sprintf( 'Failed asserting that "%s" is not readable', $filename );
59+
if ( $message !== '' ) {
60+
$msg = $message . \PHP_EOL . $msg;
5961
}
6062

61-
static::assertFalse( \is_readable( $filename ), $message );
63+
static::assertFalse( \is_readable( $filename ), $msg );
6264
}
6365

6466
/**
@@ -76,11 +78,12 @@ public static function assertIsWritable( $filename, $message = '' ) {
7678
throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'string' );
7779
}
7880

79-
if ( $message === '' ) {
80-
$message = \sprintf( 'Failed asserting that "%s" is writable', $filename );
81+
$msg = \sprintf( 'Failed asserting that "%s" is writable', $filename );
82+
if ( $message !== '' ) {
83+
$msg = $message . \PHP_EOL . $msg;
8184
}
8285

83-
static::assertTrue( \is_writable( $filename ), $message );
86+
static::assertTrue( \is_writable( $filename ), $msg );
8487
}
8588

8689
/**
@@ -98,11 +101,12 @@ public static function assertNotIsWritable( $filename, $message = '' ) {
98101
throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'string' );
99102
}
100103

101-
if ( $message === '' ) {
102-
$message = \sprintf( 'Failed asserting that "%s" is not writable', $filename );
104+
$msg = \sprintf( 'Failed asserting that "%s" is not writable', $filename );
105+
if ( $message !== '' ) {
106+
$msg = $message . \PHP_EOL . $msg;
103107
}
104108

105-
static::assertFalse( \is_writable( $filename ), $message );
109+
static::assertFalse( \is_writable( $filename ), $msg );
106110
}
107111

108112
/**
@@ -120,11 +124,12 @@ public static function assertDirectoryExists( $directory, $message = '' ) {
120124
throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'string' );
121125
}
122126

123-
if ( $message === '' ) {
124-
$message = \sprintf( 'Failed asserting that directory "%s" exists', $directory );
127+
$msg = \sprintf( 'Failed asserting that directory "%s" exists', $directory );
128+
if ( $message !== '' ) {
129+
$msg = $message . \PHP_EOL . $msg;
125130
}
126131

127-
static::assertTrue( \is_dir( $directory ), $message );
132+
static::assertTrue( \is_dir( $directory ), $msg );
128133
}
129134

130135
/**
@@ -142,11 +147,12 @@ public static function assertDirectoryNotExists( $directory, $message = '' ) {
142147
throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'string' );
143148
}
144149

145-
if ( $message === '' ) {
146-
$message = \sprintf( 'Failed asserting that directory "%s" does not exist', $directory );
150+
$msg = \sprintf( 'Failed asserting that directory "%s" does not exist', $directory );
151+
if ( $message !== '' ) {
152+
$msg = $message . \PHP_EOL . $msg;
147153
}
148154

149-
static::assertFalse( \is_dir( $directory ), $message );
155+
static::assertFalse( \is_dir( $directory ), $msg );
150156
}
151157

152158
/**

src/Polyfills/AssertStringContains.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ public static function assertStringContainsStringIgnoringCase( $needle, $haystac
6969
*/
7070
public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
7171
if ( $needle === '' ) {
72-
if ( $message === '' ) {
73-
$message = "Failed asserting that '{$haystack}' does not contain \"{$needle}\".";
72+
$msg = "Failed asserting that '{$haystack}' does not contain \"\".";
73+
if ( $message !== '' ) {
74+
$msg = $message . \PHP_EOL . $msg;
7475
}
7576

76-
static::fail( $message );
77+
static::fail( $msg );
7778
}
7879

7980
static::assertNotContains( $needle, $haystack, $message );
@@ -90,11 +91,12 @@ public static function assertStringNotContainsString( $needle, $haystack, $messa
9091
*/
9192
public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
9293
if ( $needle === '' ) {
93-
if ( $message === '' ) {
94-
$message = "Failed asserting that '{$haystack}' does not contain \"{$needle}\".";
94+
$msg = "Failed asserting that '{$haystack}' does not contain \"\".";
95+
if ( $message !== '' ) {
96+
$msg = $message . \PHP_EOL . $msg;
9597
}
9698

97-
static::fail( $message );
99+
static::fail( $msg );
98100
}
99101

100102
static::assertNotContains( $needle, $haystack, $message, true );

tests/Polyfills/AssertClosedResourceNotResourceTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public function testAssertIsClosedResource( $value ) {
3939
$this->assertIsClosedResource( $value );
4040
}
4141

42+
/**
43+
* Verify that the assertIsClosedResource() method fails a test with the correct custom failure message,
44+
* when the custom $message parameter has been passed.
45+
*
46+
* @return void
47+
*/
48+
public function testAssertIsClosedResourceFailsWithCustomMessage() {
49+
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that .+? is of type ["]?resource \(closed\)["]?`s';
50+
51+
$this->expectException( $this->getAssertionFailedExceptionName() );
52+
$this->expectExceptionMessageMatches( $pattern );
53+
54+
$this->assertIsClosedResource( 'text string', 'This assertion failed for reason XYZ' );
55+
}
56+
4257
/**
4358
* Verify that the assertIsNotClosedResource() method passes the test when the variable
4459
* passed is not a resource.
@@ -53,6 +68,24 @@ public function testAssertIsNotClosedResource( $value ) {
5368
self::assertIsNotClosedResource( $value );
5469
}
5570

71+
/**
72+
* Verify that the assertIsNotClosedResource() method fails a test with the correct custom failure message,
73+
* when the custom $message parameter has been passed.
74+
*
75+
* @return void
76+
*/
77+
public function testAssertIsNotClosedResourceFailsWithCustomMessage() {
78+
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that .+? not of type ["]?resource \(closed\)["]?`s';
79+
80+
$this->expectException( $this->getAssertionFailedExceptionName() );
81+
$this->expectExceptionMessageMatches( $pattern );
82+
83+
$resource = \opendir( __DIR__ . '/Fixtures/' );
84+
\closedir( $resource );
85+
86+
$this->assertIsNotClosedResource( $resource, 'This assertion failed for reason XYZ' );
87+
}
88+
5689
/**
5790
* Verify that the shouldClosedResourceAssertionBeSkipped() method returns true for non-resources.
5891
*

0 commit comments

Comments
 (0)