PHPLIB-510: Improve handling of NULL and Int64 values in DocumentsMatchConstraint

parent e0dd83cb
......@@ -33,6 +33,7 @@ use stdClass;
use Symfony\Bridge\PhpUnit\ConstraintTrait;
use function array_values;
use function get_class;
use function gettype;
use function in_array;
use function is_array;
use function is_object;
......@@ -313,8 +314,11 @@ class DocumentsMatchConstraint extends Constraint
continue;
}
$expectedType = is_object($expectedValue) ? get_class($expectedValue) : gettype($expectedValue);
$actualType = is_object($expectedValue) ? get_class($actualValue) : gettype($actualValue);
// Workaround for ObjectComparator printing the whole actual object
if (get_class($expectedValue) !== get_class($actualValue)) {
if ($expectedType !== $actualType) {
throw new ComparisonFailure(
$expectedValue,
$actualValue,
......@@ -322,10 +326,10 @@ class DocumentsMatchConstraint extends Constraint
'',
false,
sprintf(
'Field path "%s": %s is not instance of expected class "%s".',
'Field path "%s": %s is not instance of expected type "%s".',
$keyPrefix . $key,
$this->exporter()->shortenedExport($actualValue),
get_class($expectedValue)
$expectedType
)
);
}
......@@ -441,6 +445,12 @@ class DocumentsMatchConstraint extends Constraint
$bson[$key] = $this->prepareBSON($value, false, $sortKeys);
continue;
}
/* Convert Int64 objects to integers on 64-bit platforms for
* compatibility reasons. */
if ($value instanceof Int64 && PHP_INT_SIZE != 4) {
$bson[$key] = (int) ((string) $value);
}
}
return $bson;
......
......@@ -154,7 +154,7 @@ class DocumentsMatchConstraintTest extends TestCase
['foo' => '42'],
],
'Type mismatch' => [
'Field path "foo": MongoDB\Model\BSONDocument Object (...) is not instance of expected class "MongoDB\Model\BSONArray".',
'Field path "foo": MongoDB\Model\BSONDocument Object (...) is not instance of expected type "MongoDB\Model\BSONArray".',
new DocumentsMatchConstraint(['foo' => ['bar']]),
['foo' => (object) ['bar']],
],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment