From 73bfe48a015b309efe2c83f28f4477d688e15c8e Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 13 Jun 2024 17:31:32 +0100 Subject: [PATCH] Fix error on `resource` assertion if message has a second placeholder 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 /src/Assert.php:255 /tests/AssertTest.php:827 ``` --- src/Assert.php | 3 ++- tests/AssertTest.php | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Assert.php b/src/Assert.php index cbf8ed8..7058112 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -266,7 +266,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. )); } diff --git a/tests/AssertTest.php b/tests/AssertTest.php index f094d79..b95eba1 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -820,6 +820,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'); + } } /**