From 0e82e243da2bc605f3f5a6e242064cc4930d6f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 11 Mar 2024 15:33:31 +0100 Subject: [PATCH] Rename AggregationBuilder class --- ...lineBuilder.php => AggregationBuilder.php} | 2 +- src/Query/Builder.php | 22 ++++++++--------- tests/Models/User.php | 2 +- ...derTest.php => AggregationBuilderTest.php} | 24 +++++++++---------- tests/Query/BuilderTest.php | 4 ++-- 5 files changed, 26 insertions(+), 28 deletions(-) rename src/Query/{PipelineBuilder.php => AggregationBuilder.php} (96%) rename tests/Query/{PipelineBuilderTest.php => AggregationBuilderTest.php} (84%) diff --git a/src/Query/PipelineBuilder.php b/src/Query/AggregationBuilder.php similarity index 96% rename from src/Query/PipelineBuilder.php rename to src/Query/AggregationBuilder.php index e5339cb72..27c71f4f9 100644 --- a/src/Query/PipelineBuilder.php +++ b/src/Query/AggregationBuilder.php @@ -12,7 +12,7 @@ use function array_replace; use function collect; -final class PipelineBuilder extends FluentFactory +final class AggregationBuilder extends FluentFactory { public function __construct( array $pipeline, diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 7e416a4c4..19cdb5f6b 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -282,14 +282,14 @@ public function dump(mixed ...$args) return $this; } - private function getPipelineBuilder(): PipelineBuilder + private function getAggregationBuilder(): AggregationBuilder { - $pipelineBuilder = new PipelineBuilder([], $this->collection, $this->options); + $agg = new AggregationBuilder([], $this->collection, $this->options); $wheres = $this->compileWheres(); if (count($wheres)) { - $pipelineBuilder->match(...$wheres); + $agg->match(...$wheres); } // Distinct query @@ -297,25 +297,25 @@ private function getPipelineBuilder(): PipelineBuilder // Return distinct results directly $column = $columns[0] ?? '_id'; - $pipelineBuilder->group( + $agg->group( _id: \MongoDB\Builder\Expression::fieldPath($column), _document: Accumulator::first(Variable::root()), ); - $pipelineBuilder->replaceRoot( + $agg->replaceRoot( newRoot: new FieldPath('_document'), ); } if ($this->orders) { - $pipelineBuilder->sort(...$this->orders); + $agg->sort(...$this->orders); } if ($this->offset) { - $pipelineBuilder->skip($this->offset); + $agg->skip($this->offset); } if ($this->limit) { - $pipelineBuilder->limit($this->limit); + $agg->limit($this->limit); } // Normal query @@ -324,11 +324,11 @@ private function getPipelineBuilder(): PipelineBuilder $columns = in_array('*', $this->columns) ? [] : $this->columns; $projection = array_fill_keys($columns, true) + $this->projections; if ($projection) { - $pipelineBuilder->project(...$projection); + $agg->project(...$projection); } } - return $pipelineBuilder; + return $agg; } /** @@ -594,7 +594,7 @@ public function generateCacheKey() public function aggregate($function = null, $columns = []) { if ($function === null) { - return $this->getPipelineBuilder(); + return $this->getAggregationBuilder(); } $this->aggregate = [ diff --git a/tests/Models/User.php b/tests/Models/User.php index f2d2cf7cc..98f76d931 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -130,7 +130,7 @@ protected function username(): Attribute { return Attribute::make( get: fn ($value) => $value, - set: fn ($value) => Str::slug($value) + set: fn ($value) => Str::slug($value), ); } diff --git a/tests/Query/PipelineBuilderTest.php b/tests/Query/AggregationBuilderTest.php similarity index 84% rename from tests/Query/PipelineBuilderTest.php rename to tests/Query/AggregationBuilderTest.php index 26d59d1d1..76a2f4443 100644 --- a/tests/Query/PipelineBuilderTest.php +++ b/tests/Query/AggregationBuilderTest.php @@ -14,13 +14,13 @@ use MongoDB\Builder\Type\Sort; use MongoDB\Builder\Type\TimeUnit; use MongoDB\Builder\Variable; -use MongoDB\Laravel\Query\PipelineBuilder; +use MongoDB\Laravel\Query\AggregationBuilder; use MongoDB\Laravel\Tests\Models\User; use MongoDB\Laravel\Tests\TestCase; use function assert; -class PipelineBuilderTest extends TestCase +class AggregationBuilderTest extends TestCase { public function tearDown(): void { @@ -35,8 +35,11 @@ public function testCreateFromQueryBuilder(): void ]); // Create the aggregation pipeline from the query builder - $pipeline = User::where('name', 'John Doe')->aggregate(); - assert($pipeline instanceof PipelineBuilder); + $pipeline = User::where('name', 'John Doe') + ->limit(10) + ->offset(0) + ->aggregate(); + assert($pipeline instanceof AggregationBuilder); $pipeline ->addFields( age: Expression::dateDiff( @@ -57,9 +60,8 @@ public function testCreateFromQueryBuilder(): void // Compare with the expected pipeline $expected = Document::fromPHP([ 'pipeline' => [ - [ - '$match' => ['name' => 'John Doe'], - ], + ['$match' => ['name' => 'John Doe']], + ['$limit' => 10], [ '$addFields' => [ 'age' => [ @@ -71,12 +73,8 @@ public function testCreateFromQueryBuilder(): void ], ], ], - [ - '$sort' => ['age' => -1, 'name' => 1], - ], - [ - '$unset' => ['birthday'], - ], + ['$sort' => ['age' => -1, 'name' => 1]], + ['$unset' => ['birthday']], ], ])->toCanonicalExtendedJSON(); diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index 10dc71ddb..37def5d93 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -17,9 +17,9 @@ use MongoDB\BSON\UTCDateTime; use MongoDB\Builder\BuilderEncoder; use MongoDB\Laravel\Connection; +use MongoDB\Laravel\Query\AggregationBuilder; use MongoDB\Laravel\Query\Builder; use MongoDB\Laravel\Query\Grammar; -use MongoDB\Laravel\Query\PipelineBuilder; use MongoDB\Laravel\Query\Processor; use PHPUnit\Framework\TestCase; use stdClass; @@ -67,7 +67,7 @@ public function testAggregate(?array $expectedFind, ?array $expectedPipeline, Cl $this->assertInstanceOf(Builder::class, $builder); $encoder = new BuilderEncoder(); $pipelineBuilder = $builder->aggregate(); - $this->assertInstanceOf(PipelineBuilder::class, $pipelineBuilder); + $this->assertInstanceOf(AggregationBuilder::class, $pipelineBuilder); $pipeline = $encoder->encode($pipelineBuilder->getPipeline()); $expected = Document::fromPHP($expectedPipeline)->toCanonicalExtendedJSON();