Skip to content

Commit

Permalink
Merge branch '2.2.x' into 2.2.x-merge-up-into-2.3.x_iA5R4YuE
Browse files Browse the repository at this point in the history
* 2.2.x:
  allow to reset hydration setting for Aggregation (#2353)
  Fix missing hint query option during find (#2351)
  Ignore unreachable statement (#2352)
  • Loading branch information
alcaeus committed Aug 5, 2021
2 parents 81b0647 + 9ca6f5f commit 4a6f291
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public function group(): Stage\Group
/**
* Set which class to use when hydrating results as document class instances.
*/
public function hydrate(string $className): self
public function hydrate(?string $className): self
{
$this->hydrationClass = $className;

Expand Down
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
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Types/DateImmutableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function getDateTime($value): DateTimeInterface
return DateTimeImmutable::createFromMutable($datetime);
}

// @phpstan-ignore-next-line
throw new RuntimeException(sprintf(
'%s::getDateTime has returned an unsupported implementation of DateTimeInterface: %s',
parent::class,
Expand Down
29 changes: 29 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Documents\CmsComment;
use Documents\GuestServer;
use Documents\Tag;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;

use function array_keys;
Expand Down Expand Up @@ -199,6 +200,34 @@ public function testAggregationBuilder(): void
$this->assertSame(3, $results[0]->numPosts);
}

public function testAggregationBuilderResetHydration(): void
{
$this->insertTestData();

$builder = $this->dm->createAggregationBuilder(BlogPost::class)->hydrate(BlogTagAggregation::class);

$resultCursor = $builder
->hydrate(null)
->unwind('$tags')
->group()
->field('id')
->expression('$tags')
->field('numPosts')
->sum(1)
->sort('numPosts', 'desc')
->getAggregation()
->getIterator();

$this->assertInstanceOf(Iterator::class, $resultCursor);

$results = $resultCursor->toArray();
$this->assertCount(2, $results);
$this->assertIsArray($results[0]);
$this->assertInstanceOf(ObjectId::class, $results[0]['_id']['$id']);
$this->assertSame('Tag', $results[0]['_id']['$ref']);
$this->assertSame(3, $results[0]['numPosts']);
}

public function testGetAggregation(): void
{
$this->insertTestData();
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 @@ -323,6 +323,18 @@ public function testFindQuery(): void
$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(): void
{
$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 @@ -491,6 +491,30 @@ public function testCountOptionInheritance(): void
$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(): void
{
$nearest = new ReadPreference('nearest');
Expand Down

0 comments on commit 4a6f291

Please sign in to comment.