|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MongoDB\Laravel\Tests\Query; |
| 6 | + |
| 7 | +use BadMethodCallException; |
| 8 | +use DateTimeImmutable; |
| 9 | +use Illuminate\Support\Collection; |
| 10 | +use Illuminate\Support\LazyCollection; |
| 11 | +use InvalidArgumentException; |
| 12 | +use MongoDB\BSON\Document; |
| 13 | +use MongoDB\BSON\ObjectId; |
| 14 | +use MongoDB\BSON\UTCDateTime; |
| 15 | +use MongoDB\Builder\BuilderEncoder; |
| 16 | +use MongoDB\Builder\Expression; |
| 17 | +use MongoDB\Builder\Pipeline; |
| 18 | +use MongoDB\Builder\Type\Sort; |
| 19 | +use MongoDB\Collection as MongoDBCollection; |
| 20 | +use MongoDB\Laravel\Query\AggregationBuilder; |
| 21 | +use MongoDB\Laravel\Tests\Models\User; |
| 22 | +use MongoDB\Laravel\Tests\TestCase; |
| 23 | + |
| 24 | +class AggregationBuilderTest extends TestCase |
| 25 | +{ |
| 26 | + public function tearDown(): void |
| 27 | + { |
| 28 | + User::truncate(); |
| 29 | + |
| 30 | + parent::tearDown(); |
| 31 | + } |
| 32 | + |
| 33 | + public function testCreateAggregationBuilder(): void |
| 34 | + { |
| 35 | + User::insert([ |
| 36 | + ['name' => 'John Doe', 'birthday' => new UTCDateTime(new DateTimeImmutable('1989-01-01'))], |
| 37 | + ['name' => 'Jane Doe', 'birthday' => new UTCDateTime(new DateTimeImmutable('1990-01-01'))], |
| 38 | + ]); |
| 39 | + |
| 40 | + // Create the aggregation pipeline from the query builder |
| 41 | + $pipeline = User::aggregate(); |
| 42 | + |
| 43 | + $this->assertInstanceOf(AggregationBuilder::class, $pipeline); |
| 44 | + |
| 45 | + $pipeline |
| 46 | + ->match(name: 'John Doe') |
| 47 | + ->limit(10) |
| 48 | + ->addFields( |
| 49 | + // Requires MongoDB 5.0+ |
| 50 | + year: Expression::year( |
| 51 | + Expression::dateFieldPath('birthday'), |
| 52 | + ), |
| 53 | + ) |
| 54 | + ->sort(year: Sort::Desc, name: Sort::Asc) |
| 55 | + ->unset('birthday'); |
| 56 | + |
| 57 | + // Compare with the expected pipeline |
| 58 | + $expected = [ |
| 59 | + ['$match' => ['name' => 'John Doe']], |
| 60 | + ['$limit' => 10], |
| 61 | + [ |
| 62 | + '$addFields' => [ |
| 63 | + 'year' => ['$year' => ['date' => '$birthday']], |
| 64 | + ], |
| 65 | + ], |
| 66 | + ['$sort' => ['year' => -1, 'name' => 1]], |
| 67 | + ['$unset' => ['birthday']], |
| 68 | + ]; |
| 69 | + |
| 70 | + $this->assertSamePipeline($expected, $pipeline->getPipeline()); |
| 71 | + |
| 72 | + // Execute the pipeline and validate the results |
| 73 | + $results = $pipeline->get(); |
| 74 | + $this->assertInstanceOf(Collection::class, $results); |
| 75 | + $this->assertCount(1, $results); |
| 76 | + $this->assertInstanceOf(ObjectId::class, $results->first()['_id']); |
| 77 | + $this->assertSame('John Doe', $results->first()['name']); |
| 78 | + $this->assertIsInt($results->first()['year']); |
| 79 | + $this->assertArrayNotHasKey('birthday', $results->first()); |
| 80 | + |
| 81 | + // Execute the pipeline and validate the results in a lazy collection |
| 82 | + $results = $pipeline->cursor(); |
| 83 | + $this->assertInstanceOf(LazyCollection::class, $results); |
| 84 | + |
| 85 | + // Execute the pipeline and return the first result |
| 86 | + $result = $pipeline->first(); |
| 87 | + $this->assertIsArray($result); |
| 88 | + $this->assertInstanceOf(ObjectId::class, $result['_id']); |
| 89 | + $this->assertSame('John Doe', $result['name']); |
| 90 | + } |
| 91 | + |
| 92 | + public function testAddRawStage(): void |
| 93 | + { |
| 94 | + $collection = $this->createMock(MongoDBCollection::class); |
| 95 | + |
| 96 | + $pipeline = new AggregationBuilder($collection); |
| 97 | + $pipeline |
| 98 | + ->addRawStage('$match', ['name' => 'John Doe']) |
| 99 | + ->addRawStage('$limit', 10) |
| 100 | + ->addRawStage('$replaceRoot', (object) ['newRoot' => '$$ROOT']); |
| 101 | + |
| 102 | + $expected = [ |
| 103 | + ['$match' => ['name' => 'John Doe']], |
| 104 | + ['$limit' => 10], |
| 105 | + ['$replaceRoot' => ['newRoot' => '$$ROOT']], |
| 106 | + ]; |
| 107 | + |
| 108 | + $this->assertSamePipeline($expected, $pipeline->getPipeline()); |
| 109 | + } |
| 110 | + |
| 111 | + public function testAddRawStageInvalid(): void |
| 112 | + { |
| 113 | + $collection = $this->createMock(MongoDBCollection::class); |
| 114 | + |
| 115 | + $pipeline = new AggregationBuilder($collection); |
| 116 | + |
| 117 | + $this->expectException(InvalidArgumentException::class); |
| 118 | + $this->expectExceptionMessage('The stage name "match" is invalid. It must start with a "$" sign.'); |
| 119 | + $pipeline->addRawStage('match', ['name' => 'John Doe']); |
| 120 | + } |
| 121 | + |
| 122 | + public function testColumnsCannotBeSpecifiedToCreateAnAggregationBuilder(): void |
| 123 | + { |
| 124 | + $this->expectException(InvalidArgumentException::class); |
| 125 | + $this->expectExceptionMessage('Columns cannot be specified to create an aggregation builder.'); |
| 126 | + User::aggregate(null, ['name']); |
| 127 | + } |
| 128 | + |
| 129 | + public function testAggrecationBuilderDoesNotSupportPreviousQueryBuilderInstructions(): void |
| 130 | + { |
| 131 | + $this->expectException(BadMethodCallException::class); |
| 132 | + $this->expectExceptionMessage('Aggregation builder does not support previous query-builder instructions.'); |
| 133 | + User::where('name', 'John Doe')->aggregate(); |
| 134 | + } |
| 135 | + |
| 136 | + private static function assertSamePipeline(array $expected, Pipeline $pipeline): void |
| 137 | + { |
| 138 | + $expected = Document::fromPHP(['pipeline' => $expected])->toCanonicalExtendedJSON(); |
| 139 | + |
| 140 | + $codec = new BuilderEncoder(); |
| 141 | + $actual = $codec->encode($pipeline); |
| 142 | + // Normalize with BSON round-trip |
| 143 | + $actual = Document::fromPHP(['pipeline' => $actual])->toCanonicalExtendedJSON(); |
| 144 | + |
| 145 | + self::assertJsonStringEqualsJsonString($expected, $actual); |
| 146 | + } |
| 147 | +} |
0 commit comments