Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
f088c10d
Unverified
Commit
f088c10d
authored
Oct 08, 2019
by
Andreas Braun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-485: Inherit collection's typeMap in distinct
parent
b6b972c2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
0 deletions
+89
-0
Collection.php
src/Collection.php
+4
-0
CollectionFunctionalTest.php
tests/Collection/CollectionFunctionalTest.php
+85
-0
No files found.
src/Collection.php
View file @
f088c10d
...
@@ -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
))
{
...
...
tests/Collection/CollectionFunctionalTest.php
View file @
f088c10d
...
@@ -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
]);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment