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