|
4 | 4 |
|
5 | 5 | namespace MongoDB\Laravel\Tests\Query;
|
6 | 6 |
|
| 7 | +use DateTimeImmutable; |
| 8 | +use Illuminate\Support\Collection; |
7 | 9 | use MongoDB\BSON\Document;
|
| 10 | +use MongoDB\BSON\ObjectId; |
| 11 | +use MongoDB\BSON\UTCDateTime; |
8 | 12 | use MongoDB\Builder\BuilderEncoder;
|
| 13 | +use MongoDB\Builder\Expression; |
| 14 | +use MongoDB\Builder\Type\TimeUnit; |
| 15 | +use MongoDB\Builder\Variable; |
9 | 16 | use MongoDB\Laravel\Query\PipelineBuilder;
|
10 | 17 | use MongoDB\Laravel\Tests\Models\User;
|
11 | 18 | use MongoDB\Laravel\Tests\TestCase;
|
|
14 | 21 |
|
15 | 22 | class PipelineBuilderTest extends TestCase
|
16 | 23 | {
|
| 24 | + public function tearDown(): void |
| 25 | + { |
| 26 | + User::truncate(); |
| 27 | + } |
| 28 | + |
17 | 29 | public function testCreateFromQueryBuilder(): void
|
18 | 30 | {
|
19 |
| - $builder = User::where('foo', 'bar')->aggregate(); |
20 |
| - assert($builder instanceof PipelineBuilder); |
21 |
| - $builder->addFields(baz: 'qux'); |
| 31 | + User::insert([ |
| 32 | + // @todo apply cast to mass insert? |
| 33 | + ['name' => 'John Doe', 'birthday' => new UTCDateTime(new DateTimeImmutable('1989-01-01'))], |
| 34 | + ['name' => 'Jane Doe', 'birthday' => new UTCDateTime(new DateTimeImmutable('1990-01-01'))], |
| 35 | + ]); |
22 | 36 |
|
| 37 | + // Create the aggregation pipeline from the query builder |
| 38 | + $pipeline = User::where('name', 'John Doe')->aggregate(); |
| 39 | + assert($pipeline instanceof PipelineBuilder); |
| 40 | + $pipeline |
| 41 | + ->addFields( |
| 42 | + age: Expression::dateDiff( |
| 43 | + startDate: Expression::dateFieldPath('birthday'), |
| 44 | + endDate: Variable::now(), |
| 45 | + unit: TimeUnit::Year, |
| 46 | + ), |
| 47 | + ) |
| 48 | + ->unset('birthday'); |
| 49 | + |
| 50 | + // The encoder is used to convert the pipeline to a BSON document |
23 | 51 | $codec = new BuilderEncoder();
|
24 |
| - $pipeline = $codec->encode($builder->getPipeline()); |
25 |
| - $json = Document::fromPHP(['pipeline' => $pipeline])->toCanonicalExtendedJSON(); |
| 52 | + $json = Document::fromPHP([ |
| 53 | + 'pipeline' => $codec->encode($pipeline->getPipeline()), |
| 54 | + ])->toCanonicalExtendedJSON(); |
26 | 55 |
|
| 56 | + // Compare with the expected pipeline |
27 | 57 | $expected = Document::fromPHP([
|
28 | 58 | 'pipeline' => [
|
29 | 59 | [
|
30 |
| - '$match' => ['foo' => 'bar'], |
| 60 | + '$match' => ['name' => 'John Doe'], |
31 | 61 | ],
|
32 | 62 | [
|
33 |
| - '$addFields' => ['baz' => 'qux'], |
| 63 | + '$addFields' => [ |
| 64 | + 'age' => [ |
| 65 | + '$dateDiff' => [ |
| 66 | + 'startDate' => '$birthday', |
| 67 | + 'endDate' => '$$NOW', |
| 68 | + 'unit' => 'year', |
| 69 | + ], |
| 70 | + ], |
| 71 | + ], |
| 72 | + ], |
| 73 | + [ |
| 74 | + '$unset' => ['birthday'], |
34 | 75 | ],
|
35 | 76 | ],
|
36 | 77 | ])->toCanonicalExtendedJSON();
|
37 | 78 |
|
38 | 79 | $this->assertJsonStringEqualsJsonString($expected, $json);
|
| 80 | + |
| 81 | + // Execute the pipeline and verify the results |
| 82 | + $results = $pipeline->get(); |
| 83 | + |
| 84 | + $this->assertInstanceOf(Collection::class, $results); |
| 85 | + $this->assertCount(1, $results); |
| 86 | + $this->assertInstanceOf(ObjectId::class, $results->first()['_id']); |
| 87 | + $this->assertSame('John Doe', $results->first()['name']); |
| 88 | + $this->assertIsInt($results->first()['age']); |
| 89 | + $this->assertArrayNotHasKey('birthday', $results->first()); |
39 | 90 | }
|
40 | 91 | }
|
0 commit comments