Skip to content

Commit 8403a94

Browse files
smoenchsmoench
andauthored
fix isA*Of exception messages (#231)
Co-authored-by: smoench <simon.moench@sensiolabs.de>
1 parent 8ee533c commit 8403a94

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/Assert.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ public static function isAOf($value, $class, $message = '')
485485

486486
if (!\is_a($value, $class, \is_string($value))) {
487487
static::reportInvalidArgument(sprintf(
488-
$message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s',
489-
static::typeToString($value),
488+
$message ?: 'Expected an instance of this class or to this class among its parents "%2$s". Got: %s',
489+
static::valueToString($value),
490490
$class
491491
));
492492
}
@@ -511,8 +511,8 @@ public static function isNotA($value, $class, $message = '')
511511

512512
if (\is_a($value, $class, \is_string($value))) {
513513
static::reportInvalidArgument(sprintf(
514-
$message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s',
515-
static::typeToString($value),
514+
$message ?: 'Expected an instance of this class or to this class among its parents other than "%2$s". Got: %s',
515+
static::valueToString($value),
516516
$class
517517
));
518518
}
@@ -539,9 +539,9 @@ public static function isAnyOf($value, array $classes, $message = '')
539539
}
540540

541541
static::reportInvalidArgument(sprintf(
542-
$message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s',
543-
static::typeToString($value),
544-
\implode(', ', \array_map(array('static', 'valueToString'), $classes))
542+
$message ?: 'Expected an instance of any of this classes or any of those classes among their parents "%2$s". Got: %s',
543+
static::valueToString($value),
544+
\implode(', ', $classes)
545545
));
546546
}
547547

tests/AssertTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ public function getStringConversions()
727727
*/
728728
public function testConvertValuesToStrings($method, $args, $exceptionMessage)
729729
{
730-
$this->expectException('\InvalidArgumentException', $exceptionMessage);
730+
$this->expectException('\InvalidArgumentException');
731731
$this->expectExceptionMessage($exceptionMessage);
732732

733733
call_user_func_array(array('Webmozart\Assert\Assert', $method), $args);
@@ -739,6 +739,45 @@ public function testAnUnknownMethodThrowsABadMethodCall()
739739

740740
Assert::nonExistentMethod();
741741
}
742+
743+
public function getInvalidIsAOfCases(): iterable
744+
{
745+
yield array(
746+
array('stdClass', 123),
747+
'Expected class as a string. Got: integer',
748+
);
749+
750+
yield array(
751+
array('Iterator', 'ArrayIterator'),
752+
'Expected an instance of this class or to this class among its parents "ArrayIterator". Got: "Iterator"',
753+
);
754+
755+
yield array(
756+
array(123, 'Iterator'),
757+
'Expected an instance of this class or to this class among its parents "Iterator". Got: 123',
758+
);
759+
760+
yield array(
761+
array(array(), 'Iterator'),
762+
'Expected an instance of this class or to this class among its parents "Iterator". Got: array',
763+
);
764+
765+
yield array(
766+
array(new \stdClass(), 'Iterator'),
767+
'Expected an instance of this class or to this class among its parents "Iterator". Got: stdClass',
768+
);
769+
}
770+
771+
/**
772+
* @dataProvider getInvalidIsAOfCases
773+
*/
774+
public function testIsAOfExceptionMessages(array $args, string $exceptionMessage): void
775+
{
776+
$this->expectException('\InvalidArgumentException');
777+
$this->expectExceptionMessage($exceptionMessage);
778+
779+
call_user_func_array(array('Webmozart\Assert\Assert', 'isAOf'), $args);
780+
}
742781
}
743782

744783
/**

0 commit comments

Comments
 (0)