Skip to content

Commit 727b260

Browse files
committed
Throw exceptions when non-array values are expected or actual
1 parent 66ce515 commit 727b260

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/Framework/Constraint/Dictionary/IsIdenticalKeysValues.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace PHPUnit\Framework\Constraint\Dictionary;
1313

1414
use function array_key_exists;
15-
use function assert;
1615
use function in_array;
1716
use function is_array;
1817
use function is_object;
@@ -48,13 +47,36 @@ public function __construct(mixed $value)
4847
*/
4948
public function evaluate(mixed $other, string $description = '', bool $returnResult = false): bool
5049
{
51-
assert(is_array($this->value));
52-
assert(is_array($other));
53-
5450
// cribbed from `src/Framework/Constraint/Equality/IsEqualCanonicalizing.php`
5551
try {
56-
$this->compareDictionary($this->value, $other);
52+
if (!is_array($this->value)) {
53+
throw new ComparisonFailure(
54+
gettype([]),
55+
gettype($this->value),
56+
(new Exporter)->export(gettype([])),
57+
(new Exporter)->export(gettype($this->value)),
58+
sprintf(
59+
'%s is not an instance of %s',
60+
(new Exporter)->export(gettype($this->value)),
61+
(new Exporter)->export(gettype([])),
62+
)
63+
);
64+
}
65+
if (!is_array($other)) {
66+
throw new ComparisonFailure(
67+
gettype([]),
68+
gettype($other),
69+
(new Exporter)->export(gettype([])),
70+
(new Exporter)->export(gettype($other)),
71+
sprintf(
72+
'%s is not an instance of %s',
73+
(new Exporter)->export(gettype($other)),
74+
(new Exporter)->export(gettype([])),
75+
)
76+
);
77+
}
5778

79+
$this->compareDictionary($this->value, $other);
5880
} catch (ComparisonFailure $f) {
5981
if ($returnResult) {
6082
return false;

tests/unit/Framework/Constraint/Dictionary/IsIdenticalKeysValuesTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,32 @@ public static function provider(): array
3030
'expected is not an array' => [
3131
false,
3232
'is identical to \'not-array\'',
33-
'assert(is_array($this->value))',
34-
'',
33+
'\'string\' is not an instance of \'array\'',
34+
<<<'EOT'
35+
'string' is not an instance of 'array'
36+
--- Expected
37+
+++ Actual
38+
@@ @@
39+
-'array'
40+
+'string'
41+
EOT
42+
,
3543
'not-array',
3644
[],
3745
],
3846
'actual is not an array' => [
3947
false,
40-
'is identical to Array &0 []',
41-
'assert(is_array($other))',
42-
'',
48+
'is identical to Array &%d []',
49+
'\'string\' is not an instance of \'array\'',
50+
<<<'EOT'
51+
'string' is not an instance of 'array'
52+
--- Expected
53+
+++ Actual
54+
@@ @@
55+
-'array'
56+
+'string'
57+
EOT
58+
,
4359
[],
4460
'not-array',
4561
],

0 commit comments

Comments
 (0)