Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

## 2.1.5

### Fixed

- Fixed regression of `instanceOf` messages

## 2.1.4

### Fixed
Expand Down
5 changes: 1 addition & 4 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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),
Expand All @@ -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) {
Expand Down
38 changes: 36 additions & 2 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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',
],
[
Expand All @@ -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',
],
];
Expand Down