Skip to content

Commit

Permalink
fix isA*Of exception messages (#231)
Browse files Browse the repository at this point in the history
Co-authored-by: smoench <[email protected]>
  • Loading branch information
smoench and smoench authored May 28, 2021
1 parent 8ee533c commit 8403a94
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}
Expand All @@ -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
));
}
Expand All @@ -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)
));
}

Expand Down
41 changes: 40 additions & 1 deletion tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

/**
Expand Down

0 comments on commit 8403a94

Please sign in to comment.