From 306905c60267167fbdf54c0bbfdd089127ea55ca Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 16 Oct 2024 17:27:34 +0100 Subject: [PATCH] Fix error on `resource` assertion if message has a second placeholder (#300) --- 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'); + } } /**