From e95c89954804c1e014eb0f6242c1e1a675e0631d Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 31 Mar 2016 17:45:20 +0200 Subject: [PATCH] Convert legacy types in query for distinct calls --- CHANGELOG-1.0.md | 3 +++ lib/Mongo/MongoCollection.php | 2 +- .../Mongo/MongoCollectionTest.php | 26 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-1.0.md b/CHANGELOG-1.0.md index 01044a34..4f022472 100644 --- a/CHANGELOG-1.0.md +++ b/CHANGELOG-1.0.md @@ -13,6 +13,9 @@ milestone. * [#85](https://github.com/alcaeus/mongo-php-adapter/pull/85) fixes calls to `MongoCollection::count` using the legacy syntax of providing `skip` and `limit` arguments instead of an `options` array. + * [#88](https://github.com/alcaeus/mongo-php-adapter/pull/88) fixes an error + where a call to `MongoCollection::distinct` with a query did not convert legacy + BSON types to the new driver types. 1.0.0 (2016-03-18) diff --git a/lib/Mongo/MongoCollection.php b/lib/Mongo/MongoCollection.php index 7a3b0895..75b50052 100644 --- a/lib/Mongo/MongoCollection.php +++ b/lib/Mongo/MongoCollection.php @@ -476,7 +476,7 @@ public function find(array $query = [], array $fields = []) public function distinct($key, array $query = []) { try { - return array_map([TypeConverter::class, 'toLegacy'], $this->collection->distinct($key, $query)); + return array_map([TypeConverter::class, 'toLegacy'], $this->collection->distinct($key, TypeConverter::fromLegacy($query))); } catch (\MongoDB\Driver\Exception\Exception $e) { return false; } diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php index 25182fbc..2245227f 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php @@ -546,6 +546,32 @@ public function testDistinctWithQuery() $this->assertEquals(['bar'], $values); } + public function testDistinctWithIdQuery() + { + $document1 = ['foo' => 'bar']; + $document2 = ['foo' => 'bar']; + $document3 = ['foo' => 'foo']; + + $collection = $this->getCollection(); + $collection->insert($document1); + $collection->insert($document2); + $collection->insert($document3); + + $this->assertSame( + ['bar'], + $collection->distinct('foo', ['_id' => [ + '$in' => [$document1['_id'], $document2['_id']] + ]]) + ); + + $this->assertEquals( + ['bar', 'foo'], + $collection->distinct('foo', ['_id' => [ + '$in' => [$document1['_id'], $document3['_id']] + ]]) + ); + } + public function testAggregate() { $collection = $this->getCollection();