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

parent e0dd83cb
...@@ -33,6 +33,7 @@ use stdClass; ...@@ -33,6 +33,7 @@ use stdClass;
use Symfony\Bridge\PhpUnit\ConstraintTrait; use Symfony\Bridge\PhpUnit\ConstraintTrait;
use function array_values; use function array_values;
use function get_class; use function get_class;
use function gettype;
use function in_array; use function in_array;
use function is_array; use function is_array;
use function is_object; use function is_object;
...@@ -313,8 +314,11 @@ class DocumentsMatchConstraint extends Constraint ...@@ -313,8 +314,11 @@ class DocumentsMatchConstraint extends Constraint
continue; 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 // Workaround for ObjectComparator printing the whole actual object
if (get_class($expectedValue) !== get_class($actualValue)) { if ($expectedType !== $actualType) {
throw new ComparisonFailure( throw new ComparisonFailure(
$expectedValue, $expectedValue,
$actualValue, $actualValue,
...@@ -322,10 +326,10 @@ class DocumentsMatchConstraint extends Constraint ...@@ -322,10 +326,10 @@ class DocumentsMatchConstraint extends Constraint
'', '',
false, false,
sprintf( 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, $keyPrefix . $key,
$this->exporter()->shortenedExport($actualValue), $this->exporter()->shortenedExport($actualValue),
get_class($expectedValue) $expectedType
) )
); );
} }
...@@ -441,6 +445,12 @@ class DocumentsMatchConstraint extends Constraint ...@@ -441,6 +445,12 @@ class DocumentsMatchConstraint extends Constraint
$bson[$key] = $this->prepareBSON($value, false, $sortKeys); $bson[$key] = $this->prepareBSON($value, false, $sortKeys);
continue; 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; return $bson;
......
...@@ -154,7 +154,7 @@ class DocumentsMatchConstraintTest extends TestCase ...@@ -154,7 +154,7 @@ class DocumentsMatchConstraintTest extends TestCase
['foo' => '42'], ['foo' => '42'],
], ],
'Type mismatch' => [ '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']]), new DocumentsMatchConstraint(['foo' => ['bar']]),
['foo' => (object) ['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