diff --git a/src/Assert.php b/src/Assert.php index 74659b8..5451fe1 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -485,8 +485,8 @@ public static function isAOf($value, $class, $message = '') if (!\is_a($value, $class, \is_string($value))) { static::reportInvalidArgument(sprintf( - $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', - static::typeToString($value), + $message ?: 'Expected an instance of this class or to this class among its parents "%2$s". Got: %s', + static::valueToString($value), $class )); } @@ -511,8 +511,8 @@ public static function isNotA($value, $class, $message = '') if (\is_a($value, $class, \is_string($value))) { static::reportInvalidArgument(sprintf( - $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s', - static::typeToString($value), + $message ?: 'Expected an instance of this class or to this class among its parents other than "%2$s". Got: %s', + static::valueToString($value), $class )); } @@ -539,9 +539,9 @@ public static function isAnyOf($value, array $classes, $message = '') } static::reportInvalidArgument(sprintf( - $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', - static::typeToString($value), - \implode(', ', \array_map(array('static', 'valueToString'), $classes)) + $message ?: 'Expected an instance of any of this classes or any of those classes among their parents "%2$s". Got: %s', + static::valueToString($value), + \implode(', ', $classes) )); } diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 4e2237e..c92b580 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -727,7 +727,7 @@ public function getStringConversions() */ public function testConvertValuesToStrings($method, $args, $exceptionMessage) { - $this->expectException('\InvalidArgumentException', $exceptionMessage); + $this->expectException('\InvalidArgumentException'); $this->expectExceptionMessage($exceptionMessage); call_user_func_array(array('Webmozart\Assert\Assert', $method), $args); @@ -739,6 +739,45 @@ public function testAnUnknownMethodThrowsABadMethodCall() Assert::nonExistentMethod(); } + + public function getInvalidIsAOfCases(): iterable + { + yield array( + array('stdClass', 123), + 'Expected class as a string. Got: integer', + ); + + yield array( + array('Iterator', 'ArrayIterator'), + 'Expected an instance of this class or to this class among its parents "ArrayIterator". Got: "Iterator"', + ); + + yield array( + array(123, 'Iterator'), + 'Expected an instance of this class or to this class among its parents "Iterator". Got: 123', + ); + + yield array( + array(array(), 'Iterator'), + 'Expected an instance of this class or to this class among its parents "Iterator". Got: array', + ); + + yield array( + array(new \stdClass(), 'Iterator'), + 'Expected an instance of this class or to this class among its parents "Iterator". Got: stdClass', + ); + } + + /** + * @dataProvider getInvalidIsAOfCases + */ + public function testIsAOfExceptionMessages(array $args, string $exceptionMessage): void + { + $this->expectException('\InvalidArgumentException'); + $this->expectExceptionMessage($exceptionMessage); + + call_user_func_array(array('Webmozart\Assert\Assert', 'isAOf'), $args); + } } /**