Skip to content

Commit

Permalink
Fix missing hint query option during find (#2351)
Browse files Browse the repository at this point in the history
* 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

* Included hint query option for find queries.

* Added a test to Builder to make sure hints are present in both the builder and the resulting query
  • Loading branch information
TimoBakx authored Aug 2, 2021
1 parent f2f94ea commit 4979c0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 4979c0f

Please sign in to comment.