From df304c9cf9279fd398d0b72ec33b971b2e218ca9 Mon Sep 17 00:00:00 2001 From: Timo Bakx Date: Mon, 2 Aug 2021 11:30:55 +0200 Subject: [PATCH 1/3] Added a test, expecting the hint option to propagate from Query->execute to Collection->find Changed test to move hint from options to query array --- .../Doctrine/ODM/MongoDB/Tests/QueryTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php index 9db60a176b..0883fbd611 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php @@ -490,6 +490,30 @@ public function testCountOptionInheritance() $this->assertSame(100, $query->execute()); } + public function testFindWithHint(): void + { + $cursor = $this->createMock(Traversable::class); + + $collection = $this->getMockCollection(); + $collection->expects($this->once()) + ->method('find') + ->with(['foo' => 'bar'], ['hint' => 'foo']) + ->will($this->returnValue($cursor)); + + // Using QueryBuilder->find adds hint to the query array + $queryArray = [ + 'type' => Query::TYPE_FIND, + 'query' => ['foo' => 'bar'], + 'hint' => 'foo', + ]; + + $query = new Query($this->dm, new ClassMetadata(User::class), $collection, $queryArray, []); + + /* Do not expect the same object returned by Collection::find(), since + * Query::makeIterator() wraps the return value with CachingIterator. */ + $this->assertInstanceOf(Traversable::class, $query->execute()); + } + public function testFindOptionInheritance() { $nearest = new ReadPreference('nearest'); From 3f6dd275b8a882fe93b89d73876b02c87348d540 Mon Sep 17 00:00:00 2001 From: Timo Bakx Date: Mon, 2 Aug 2021 11:31:36 +0200 Subject: [PATCH 2/3] Included hint query option for find queries. --- lib/Doctrine/ODM/MongoDB/Query/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ODM/MongoDB/Query/Query.php b/lib/Doctrine/ODM/MongoDB/Query/Query.php index 3f4fcfc3c7..7ecdcec025 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Query.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Query.php @@ -424,7 +424,7 @@ private function runQuery() switch ($this->query['type']) { case self::TYPE_FIND: - $queryOptions = $this->getQueryOptions('select', 'sort', 'skip', 'limit', 'readPreference'); + $queryOptions = $this->getQueryOptions('select', 'sort', 'skip', 'limit', 'readPreference', 'hint'); $queryOptions = $this->renameQueryOptions($queryOptions, ['select' => 'projection']); $cursor = $this->collection->find( From 8eb1c7f697880c7a8d3960eca0b607f054c16e9a Mon Sep 17 00:00:00 2001 From: Timo Bakx Date: Mon, 2 Aug 2021 12:34:53 +0200 Subject: [PATCH 3/3] Added a test to Builder to make sure hints are present in both the builder and the resulting query --- .../Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php index 491f4aaad7..b54a7577c1 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php @@ -321,6 +321,18 @@ public function testFindQuery() $this->assertEquals($expected, $qb->getQueryArray()); } + public function testFindWithHint(): void + { + $qb = $this->getTestQueryBuilder() + ->find(User::class) + ->hint('foo'); + + $expected = 'foo'; + + $this->assertEquals($expected, $qb->debug('hint')); + $this->assertEquals($expected, $qb->getQuery()->debug('hint')); + } + public function testUpsertUpdateQuery() { $qb = $this->getTestQueryBuilder()