diff --git a/CHANGELOG.md b/CHANGELOG.md index 538c846..2449ee4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog ========= +## 2.1.5 + +### Fixed + +- Fixed regression of `instanceOf` messages + ## 2.1.4 ### Fixed diff --git a/src/Assert.php b/src/Assert.php index 65a45a9..24f9c1a 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -484,7 +484,6 @@ public static function isIterable(mixed $value, string $message = ''): iterable */ public static function isInstanceOf(mixed $value, mixed $class, string $message = ''): object { - static::object($value, $message); static::string($class, 'Expected class as a string. Got: %s'); if (!($value instanceof $class)) { @@ -510,10 +509,9 @@ public static function isInstanceOf(mixed $value, mixed $class, string $message */ public static function notInstanceOf(mixed $value, mixed $class, string $message = ''): object { - static::object($value, $message); static::string($class, 'Expected class as a string. Got: %s'); - if ($value instanceof $class) { + if (!\is_object($value) || $value instanceof $class) { static::reportInvalidArgument(\sprintf( $message ?: 'Expected an instance other than %2$s. Got: %s', static::typeToString($value), @@ -537,7 +535,6 @@ public static function notInstanceOf(mixed $value, mixed $class, string $message */ public static function isInstanceOfAny(mixed $value, mixed $classes, string $message = ''): object { - static::object($value, $message); static::isIterable($classes); foreach ($classes as $class) { diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 5b3d0a3..8fefdbc 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -188,7 +188,9 @@ public static function getTests(): array ['isInstanceOf', [null, 'stdClass'], false], ['notInstanceOf', [new stdClass(), 'stdClass'], false], ['notInstanceOf', [new Exception(), 'stdClass'], true], + ['notInstanceOf', [123, 'stdClass'], false], ['notInstanceOf', [[], 'stdClass'], false], + ['notInstanceOf', [null, 'stdClass'], false], ['isInstanceOfAny', [new ArrayIterator(), ['Iterator', 'ArrayAccess']], true], ['isInstanceOfAny', [new Exception(), ['Exception', 'Countable']], true], ['isInstanceOfAny', [new Exception(), ['ArrayAccess', 'Countable']], false], @@ -803,6 +805,38 @@ public function testConvertValuesToStrings(string $method, array $args, string $ call_user_func_array(['Webmozart\Assert\Assert', $method], $args); } + public static function getInvalidInstanceOfCases(): iterable + { + yield [ + [null, 'stdClass'], + 'Expected an instance of stdClass. Got: NULL', + ]; + + yield [ + [123, 'stdClass'], + 'Expected an instance of stdClass. Got: integer', + ]; + + yield [ + [[], 'Iterator'], + 'Expected an instance of Iterator. Got: array', + ]; + + yield [ + [new stdClass(), 'Iterator'], + 'Expected an instance of Iterator. Got: stdClass', + ]; + } + + #[DataProvider('getInvalidInstanceOfCases')] + public function testInstanceOfExceptionMessages(array $args, string $exceptionMessage): void + { + $this->expectException('\InvalidArgumentException'); + $this->expectExceptionMessage($exceptionMessage); + + call_user_func_array(['Webmozart\Assert\Assert', 'isInstanceOf'], $args); + } + public static function getInvalidIsAOfCases(): iterable { yield [ @@ -955,7 +989,7 @@ public static function getMethodsThatUseOtherMethods(): array ], [ 'method' => 'isInstanceOfAny', - 'args' => [111, 'stdClass', 'Value must be an instance of stdClass. Got: %s'], + 'args' => [111, ['stdClass'], 'Value must be an instance of stdClass. Got: %s'], 'exceptionMessage' => 'Value must be an instance of stdClass. Got: integer', ], [ @@ -965,7 +999,7 @@ public static function getMethodsThatUseOtherMethods(): array ], [ 'method' => 'isAnyOf', - 'args' => [111, 'stdClass', 'Value must be an instance of stdClass. Got: %s'], + 'args' => [111, ['stdClass'], 'Value must be an instance of stdClass. Got: %s'], 'exceptionMessage' => 'Value must be an instance of stdClass. Got: integer', ], ];