Skip to content

Commit 0b923b0

Browse files
committed
PHPLIB-510: Improve handling of NULL and Int64 values in DocumentsMatchConstraint
1 parent e0dd83c commit 0b923b0

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

tests/SpecTests/DocumentsMatchConstraint.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Bridge\PhpUnit\ConstraintTrait;
3434
use function array_values;
3535
use function get_class;
36+
use function gettype;
3637
use function in_array;
3738
use function is_array;
3839
use function is_object;
@@ -313,19 +314,22 @@ private function assertEquals(ArrayObject $expected, ArrayObject $actual, $ignor
313314
continue;
314315
}
315316

317+
$expectedType = is_object($expectedValue) ? get_class($expectedValue) : gettype($expectedValue);
318+
$actualType = is_object($expectedValue) ? get_class($actualValue) : gettype($actualValue);
319+
316320
// Workaround for ObjectComparator printing the whole actual object
317-
if (get_class($expectedValue) !== get_class($actualValue)) {
321+
if ($expectedType !== $actualType) {
318322
throw new ComparisonFailure(
319323
$expectedValue,
320324
$actualValue,
321325
'',
322326
'',
323327
false,
324328
sprintf(
325-
'Field path "%s": %s is not instance of expected class "%s".',
329+
'Field path "%s": %s is not instance of expected type "%s".',
326330
$keyPrefix . $key,
327331
$this->exporter()->shortenedExport($actualValue),
328-
get_class($expectedValue)
332+
$expectedType
329333
)
330334
);
331335
}
@@ -441,6 +445,12 @@ private function prepareBSON($bson, $isRoot, $sortKeys = false)
441445
$bson[$key] = $this->prepareBSON($value, false, $sortKeys);
442446
continue;
443447
}
448+
449+
/* Convert Int64 objects to integers on 64-bit platforms for
450+
* compatibility reasons. */
451+
if ($value instanceof Int64 && PHP_INT_SIZE != 4) {
452+
$bson[$key] = (int) ((string) $value);
453+
}
444454
}
445455

446456
return $bson;

tests/SpecTests/DocumentsMatchConstraintTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function errorMessageProvider()
154154
['foo' => '42'],
155155
],
156156
'Type mismatch' => [
157-
'Field path "foo": MongoDB\Model\BSONDocument Object (...) is not instance of expected class "MongoDB\Model\BSONArray".',
157+
'Field path "foo": MongoDB\Model\BSONDocument Object (...) is not instance of expected type "MongoDB\Model\BSONArray".',
158158
new DocumentsMatchConstraint(['foo' => ['bar']]),
159159
['foo' => (object) ['bar']],
160160
],

0 commit comments

Comments
 (0)