Skip to content

Commit

Permalink
feat(mongodb): Replace usage of deprecated method `AggregationBuilder…
Browse files Browse the repository at this point in the history
…::execute()` (#6933)
  • Loading branch information
GromNaN authored Jan 29, 2025
1 parent 4bdf042 commit 2e2debb
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"doctrine/common": "^3.2.2",
"doctrine/dbal": "^4.0",
"doctrine/doctrine-bundle": "^2.11",
"doctrine/mongodb-odm": "^2.9.2",
"doctrine/mongodb-odm": "^2.10",
"doctrine/mongodb-odm-bundle": "^4.0 || ^5.0",
"doctrine/orm": "^2.17 || ^3.0",
"elasticsearch/elasticsearch": "^8.4",
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Odm/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function getResult(Builder $aggregationBuilder, string $resourceClass, ?O
$attribute = $operation?->getExtraProperties()['doctrine_mongodb'] ?? [];
$executeOptions = $attribute['execute_options'] ?? [];

return new Paginator($aggregationBuilder->execute($executeOptions), $manager->getUnitOfWork(), $resourceClass);
return new Paginator($aggregationBuilder->getAggregation($executeOptions)->getIterator(), $manager->getUnitOfWork(), $resourceClass);
}

private function addCountToContext(Builder $aggregationBuilder, array $context): array
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Odm/State/CollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$attribute = $operation->getExtraProperties()['doctrine_mongodb'] ?? [];
$executeOptions = $attribute['execute_options'] ?? [];

return $aggregationBuilder->hydrate($documentClass)->execute($executeOptions);
return $aggregationBuilder->hydrate($documentClass)->getAggregation($executeOptions)->getIterator();
}
}
2 changes: 1 addition & 1 deletion src/Doctrine/Odm/State/ItemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ public function provide(Operation $operation, array $uriVariables = [], array $c

$executeOptions = $operation->getExtraProperties()['doctrine_mongodb']['execute_options'] ?? [];

return $aggregationBuilder->hydrate($documentClass)->execute($executeOptions)->current() ?: null;
return $aggregationBuilder->hydrate($documentClass)->getAggregation($executeOptions)->getIterator()->current() ?: null;
}
}
2 changes: 1 addition & 1 deletion src/Doctrine/Odm/State/LinksHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function buildAggregation(string $toClass, array $links, array $identifi
return $aggregation;
}

$results = $aggregation->execute($executeOptions)->toArray();
$results = $aggregation->getAggregation($executeOptions)->getIterator()->toArray();
$in = [];
foreach ($results as $result) {
foreach ($result[$lookupPropertyAlias] ?? [] as $lookupResult) {
Expand Down
11 changes: 9 additions & 2 deletions src/Doctrine/Odm/Tests/Extension/PaginationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Doctrine\ODM\MongoDB\Aggregation\Stage\Facet;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Skip;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -333,8 +334,11 @@ public function testGetResult(): void
],
]);

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iteratorProphecy->reveal());

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->execute([])->willReturn($iteratorProphecy->reveal());
$aggregationBuilderProphecy->getAggregation([])->willReturn($aggregationProphecy->reveal());
$aggregationBuilderProphecy->getPipeline()->willReturn([
[
'$facet' => [
Expand Down Expand Up @@ -390,8 +394,11 @@ public function testGetResultWithExecuteOptions(): void
],
]);

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iteratorProphecy->reveal());

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->execute(['allowDiskUse' => true])->willReturn($iteratorProphecy->reveal());
$aggregationBuilderProphecy->getAggregation(['allowDiskUse' => true])->willReturn($aggregationProphecy->reveal());
$aggregationBuilderProphecy->getPipeline()->willReturn([
[
'$facet' => [
Expand Down
16 changes: 13 additions & 3 deletions src/Doctrine/Odm/Tests/State/CollectionProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -50,9 +51,12 @@ public function testGetCollection(): void
{
$iterator = $this->prophesize(Iterator::class)->reveal();

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iterator);

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
$aggregationBuilderProphecy->execute([])->willReturn($iterator)->shouldBeCalled();
$aggregationBuilderProphecy->getAggregation([])->willReturn($aggregationProphecy)->shouldBeCalled();
$aggregationBuilder = $aggregationBuilderProphecy->reveal();

$repositoryProphecy = $this->prophesize(DocumentRepository::class);
Expand All @@ -76,9 +80,12 @@ public function testGetCollectionWithExecuteOptions(): void
{
$iterator = $this->prophesize(Iterator::class)->reveal();

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iterator);

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
$aggregationBuilderProphecy->execute(['allowDiskUse' => true])->willReturn($iterator)->shouldBeCalled();
$aggregationBuilderProphecy->getAggregation(['allowDiskUse' => true])->willReturn($aggregationProphecy)->shouldBeCalled();
$aggregationBuilder = $aggregationBuilderProphecy->reveal();

$repositoryProphecy = $this->prophesize(DocumentRepository::class);
Expand Down Expand Up @@ -143,9 +150,12 @@ public function testOperationNotFound(): void
{
$iterator = $this->prophesize(Iterator::class)->reveal();

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iterator);

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
$aggregationBuilderProphecy->execute([])->willReturn($iterator)->shouldBeCalled();
$aggregationBuilderProphecy->getAggregation([])->willReturn($aggregationProphecy)->shouldBeCalled();
$aggregationBuilder = $aggregationBuilderProphecy->reveal();

$repositoryProphecy = $this->prophesize(DocumentRepository::class);
Expand Down
16 changes: 13 additions & 3 deletions src/Doctrine/Odm/Tests/State/ItemProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Aggregation\Stage\MatchStage as AggregationMatch;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
Expand All @@ -49,10 +50,13 @@ public function testGetItemSingleIdentifier(): void
$result = new \stdClass();
$iterator->current()->willReturn($result)->shouldBeCalled();

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iterator);

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->match()->willReturn($matchProphecy->reveal())->shouldBeCalled();
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
$aggregationBuilderProphecy->execute([])->willReturn($iterator->reveal())->shouldBeCalled();
$aggregationBuilderProphecy->getAggregation([])->willReturn($aggregationProphecy->reveal())->shouldBeCalled();
$aggregationBuilder = $aggregationBuilderProphecy->reveal();

$managerRegistry = $this->getManagerRegistry(ProviderDocument::class, $aggregationBuilder);
Expand Down Expand Up @@ -82,10 +86,13 @@ public function testGetItemWithExecuteOptions(): void
$result = new \stdClass();
$iterator->current()->willReturn($result)->shouldBeCalled();

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iterator);

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->match()->willReturn($matchProphecy->reveal())->shouldBeCalled();
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
$aggregationBuilderProphecy->execute(['allowDiskUse' => true])->willReturn($iterator->reveal())->shouldBeCalled();
$aggregationBuilderProphecy->getAggregation(['allowDiskUse' => true])->willReturn($aggregationProphecy->reveal())->shouldBeCalled();
$aggregationBuilder = $aggregationBuilderProphecy->reveal();

$managerRegistry = $this->getManagerRegistry(ProviderDocument::class, $aggregationBuilder);
Expand Down Expand Up @@ -116,10 +123,13 @@ public function testGetItemDoubleIdentifier(): void
$result = new \stdClass();
$iterator->current()->willReturn($result)->shouldBeCalled();

$aggregationProphecy = $this->prophesize(IterableResult::class);
$aggregationProphecy->getIterator()->willReturn($iterator);

$aggregationBuilderProphecy = $this->prophesize(Builder::class);
$aggregationBuilderProphecy->match()->willReturn($matchProphecy->reveal())->shouldBeCalled();
$aggregationBuilderProphecy->hydrate(ProviderDocument::class)->willReturn($aggregationBuilderProphecy)->shouldBeCalled();
$aggregationBuilderProphecy->execute([])->willReturn($iterator->reveal())->shouldBeCalled();
$aggregationBuilderProphecy->getAggregation([])->willReturn($aggregationProphecy->reveal())->shouldBeCalled();
$aggregationBuilder = $aggregationBuilderProphecy->reveal();

$managerRegistry = $this->getManagerRegistry(ProviderDocument::class, $aggregationBuilder);
Expand Down

0 comments on commit 2e2debb

Please sign in to comment.