Skip to content

Commit

Permalink
Fix error on resource assertion if message has a second placeholder
Browse files Browse the repository at this point in the history
Prior to this change code such as
`Assert::resource(null, 'stream', 'Got: %s, type: %s')` would fail as
the second placeholder was only passed into `sprintf()` if the value
supplied is a resource.

Test I've added without the code change fails with:

```
1) Webmozart\Assert\Tests\AssertTest::testResourceOfTypeCustomMessage
Failed asserting that exception of type "ArgumentCountError" matches expected exception "\InvalidArgumentException". Message was: "3 arguments are required, 2 given" at
<snip>/src/Assert.php:255
<snip>/tests/AssertTest.php:827
```
  • Loading branch information
cs278 committed Jun 13, 2024
1 parent 11cb219 commit 214460c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public static function resource($value, $type = null, $message = '')
if (!\is_resource($value)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a resource. Got: %s',
static::typeToString($value)
static::typeToString($value),
$type // User supplied message might include the second placeholder.
));
}

Expand Down
8 changes: 8 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,14 @@ public function testIsAOfExceptionMessages(array $args, string $exceptionMessage

call_user_func_array(array('Webmozart\Assert\Assert', 'isAOf'), $args);
}

public function testResourceOfTypeCustomMessage(): void
{
$this->expectException('\InvalidArgumentException');
$this->expectExceptionMessage('I want a resource of type curl. Got: NULL');

Assert::resource(null, 'curl', 'I want a resource of type %2$s. Got: %s');
}
}

/**
Expand Down

0 comments on commit 214460c

Please sign in to comment.