PHPLIB-485: Inherit collection's typeMap in distinct

parent b6b972c2
...@@ -470,6 +470,10 @@ class Collection ...@@ -470,6 +470,10 @@ class Collection
$options['readPreference'] = $this->readPreference; $options['readPreference'] = $this->readPreference;
} }
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
$server = select_server($this->manager, $options); $server = select_server($this->manager, $options);
if (! isset($options['readConcern']) && server_supports_feature($server, self::$wireVersionForReadConcern) && ! is_in_transaction($options)) { if (! isset($options['readConcern']) && server_supports_feature($server, self::$wireVersionForReadConcern) && ! is_in_transaction($options)) {
......
...@@ -16,7 +16,10 @@ use MongoDB\Operation\Count; ...@@ -16,7 +16,10 @@ use MongoDB\Operation\Count;
use MongoDB\Tests\CommandObserver; use MongoDB\Tests\CommandObserver;
use function array_filter; use function array_filter;
use function call_user_func; use function call_user_func;
use function is_scalar;
use function json_encode;
use function strchr; use function strchr;
use function usort;
use function version_compare; use function version_compare;
/** /**
...@@ -170,6 +173,88 @@ class CollectionFunctionalTest extends FunctionalTestCase ...@@ -170,6 +173,88 @@ class CollectionFunctionalTest extends FunctionalTestCase
); );
} }
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocuments
*/
public function testDistinctWithTypeMap(array $typeMap, array $expectedDocuments)
{
$bulkWrite = new BulkWrite(['ordered' => true]);
$bulkWrite->insert([
'x' => (object) ['foo' => 'bar'],
]);
$bulkWrite->insert(['x' => 4]);
$bulkWrite->insert([
'x' => (object) ['foo' => ['foo' => 'bar']],
]);
$this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$values = $this->collection->withOptions(['typeMap' => $typeMap])->distinct('x');
/* This sort callable sorts all scalars to the front of the list. All
* non-scalar values are sorted by running json_encode on them and
* comparing their string representations.
*/
$sort = function ($a, $b) {
if (is_scalar($a) && ! is_scalar($b)) {
return -1;
}
if (! is_scalar($a)) {
if (is_scalar($b)) {
return 1;
}
$a = json_encode($a);
$b = json_encode($b);
}
return $a < $b ? -1 : 1;
};
usort($expectedDocuments, $sort);
usort($values, $sort);
$this->assertEquals($expectedDocuments, $values);
}
public function provideTypeMapOptionsAndExpectedDocuments()
{
return [
'No type map' => [
['root' => 'array', 'document' => 'array'],
[
['foo' => 'bar'],
4,
['foo' => ['foo' => 'bar']],
],
],
'array/array' => [
['root' => 'array', 'document' => 'array'],
[
['foo' => 'bar'],
4,
['foo' => ['foo' => 'bar']],
],
],
'object/array' => [
['root' => 'object', 'document' => 'array'],
[
(object) ['foo' => 'bar'],
4,
(object) ['foo' => ['foo' => 'bar']],
],
],
'array/stdClass' => [
['root' => 'array', 'document' => 'stdClass'],
[
['foo' => 'bar'],
4,
['foo' => (object) ['foo' => 'bar']],
],
],
];
}
public function testDrop() public function testDrop()
{ {
$writeResult = $this->collection->insertOne(['x' => 1]); $writeResult = $this->collection->insertOne(['x' => 1]);
......
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