Skip to content

Commit

Permalink
AssertStringContains: improve handling of custom error message
Browse files Browse the repository at this point in the history
Some of the assertions in the `AssertStringContains` trait generate a specific error message.
However, if a custom `$message` parameter was passed, that message would not be displayed and only the custom `$message` would be shown when the assertion failed.

As the PHPUnit native error message from the _underlying_ assertion used - _"true is not false"_ - would be obscuring the real issue, this commit adjusts the code to always display the assertion specific error message as well.

Includes tests safeguarding the fix.
  • Loading branch information
jrfnl committed Mar 12, 2023
1 parent dd6b6c1 commit e873683
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Polyfills/AssertStringContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ public static function assertStringContainsStringIgnoringCase( $needle, $haystac
*/
public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
if ( $needle === '' ) {
if ( $message === '' ) {
$message = "Failed asserting that '{$haystack}' does not contain \"{$needle}\".";
$msg = "Failed asserting that '{$haystack}' does not contain \"\".";
if ( $message !== '' ) {
$msg = $message . \PHP_EOL . $msg;
}

static::fail( $message );
static::fail( $msg );
}

static::assertNotContains( $needle, $haystack, $message );
Expand All @@ -90,11 +91,12 @@ public static function assertStringNotContainsString( $needle, $haystack, $messa
*/
public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
if ( $needle === '' ) {
if ( $message === '' ) {
$message = "Failed asserting that '{$haystack}' does not contain \"{$needle}\".";
$msg = "Failed asserting that '{$haystack}' does not contain \"\".";
if ( $message !== '' ) {
$msg = $message . \PHP_EOL . $msg;
}

static::fail( $message );
static::fail( $msg );
}

static::assertNotContains( $needle, $haystack, $message, true );
Expand Down
32 changes: 32 additions & 0 deletions tests/Polyfills/AssertStringContainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;

/**
* Availability test for the functions polyfilled by the AssertStringContains trait.
Expand All @@ -15,6 +16,7 @@ final class AssertStringContainsTest extends TestCase {

use AssertStringContains;
use ExpectException; // Needed for PHPUnit < 5.2.0 support.
use ExpectExceptionMessageMatches;

/**
* Verify availability of the assertStringContainsString() method.
Expand Down Expand Up @@ -145,6 +147,36 @@ public static function dataHaystacks() {
];
}

/**
* Verify that the assertStringNotContainsString() method fails a test with a custom failure message,
* when the custom $message parameter has been passed.
*
* @return void
*/
public function testAssertStringNotContainsStringFailsWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that \'.+?\' does not contain ""\.`s';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectExceptionMessageMatches( $pattern );

$this->assertStringNotContainsString( '', 'something', 'This assertion failed for reason XYZ' );
}

/**
* Verify that the assertStringNotContainsStringIgnoringCase() method fails a test with a custom failure message,
* when the custom $message parameter has been passed.
*
* @return void
*/
public function testssertStringNotContainsStringIgnoringCaseFailsWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that \'.+?\' does not contain ""\.`s';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectExceptionMessageMatches( $pattern );

$this->assertStringNotContainsStringIgnoringCase( '', 'something', 'This assertion failed for reason XYZ' );
}

/**
* Helper function: retrieve the name of the "assertion failed" exception to expect (PHPUnit cross-version).
*
Expand Down

0 comments on commit e873683

Please sign in to comment.