Skip to content

Commit 37398d2

Browse files
committedMar 30, 2016
Fixed logQuery to display ObjectID in parameters
1 parent 289ec4c commit 37398d2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 

‎src/Jenssegers/Mongodb/Collection.php

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public function __call($method, $parameters)
4949
$query = [];
5050

5151
// Convert the query paramters to a json string.
52+
array_walk_recursive($parameters, function (&$item, $key){
53+
if ($item instanceof \MongoDB\BSON\ObjectID) {
54+
$item = (string) $item;
55+
}
56+
});
57+
5258
foreach ($parameters as $parameter) {
5359
try {
5460
$query[] = json_encode($parameter);

‎tests/CollectionTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Jenssegers\Mongodb\Connection;
4+
use Jenssegers\Mongodb\Collection;
5+
use MongoDB\Collection as MongoCollection;
6+
use MongoDB\BSON\ObjectID;
7+
8+
class CollectionTest extends TestCase
9+
{
10+
11+
public function testExecuteMethodCall()
12+
{
13+
$return = ['foo' => 'bar'];
14+
$where = ['id' => new ObjectID('56f94800911dcc276b5723dd')];
15+
$time = 1.1;
16+
$queryString = 'name-collection.findOne({"id":"56f94800911dcc276b5723dd"})';
17+
18+
$mongoCollection = $this->getMockBuilder(MongoCollection::class)
19+
->disableOriginalConstructor()
20+
->getMock();
21+
22+
$mongoCollection->expects($this->once())->method('findOne')->with($where)->willReturn($return);
23+
$mongoCollection->expects($this->once())->method('getCollectionName')->willReturn('name-collection');
24+
25+
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
26+
$connection->expects($this->once())->method('logging')->willReturn(true);
27+
$connection->expects($this->once())->method('getElapsedTime')->willReturn($time);
28+
$connection->expects($this->once())->method('logQuery')->with($queryString, [], $time);
29+
30+
$collection = new Collection($connection, $mongoCollection);
31+
32+
$this->assertEquals($return, $collection->findOne($where));
33+
}
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.