-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathReadOperationsTest.php
230 lines (198 loc) · 6.51 KB
/
ReadOperationsTest.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Driver\ReadPreference;
use MongoDB\Laravel\Tests\TestCase;
use function json_encode;
use const JSON_PRETTY_PRINT;
class ReadOperationsTest extends TestCase
{
protected function setUp(): void
{
require_once __DIR__ . '/Movie.php';
parent::setUp();
$moviesCollection = DB::connection('mongodb')->getCollection('movies');
$moviesCollection->drop();
$moviesCollection->createIndex(['plot' => 'text']);
Movie::insert([
['year' => 2010, 'imdb' => ['rating' => 9]],
['year' => 2010, 'imdb' => ['rating' => 9.5]],
['year' => 2010, 'imdb' => ['rating' => 7]],
['year' => 1999, 'countries' => ['Indonesia', 'Canada'], 'title' => 'Title 1'],
['year' => 1999, 'countries' => ['Indonesia'], 'title' => 'Title 2'],
['year' => 1999, 'countries' => ['Indonesia'], 'title' => 'Title 3'],
['year' => 1999, 'countries' => ['Indonesia'], 'title' => 'Title 4'],
['year' => 1999, 'countries' => ['Canada'], 'title' => 'Title 5'],
['year' => 1999, 'runtime' => 30],
['title' => 'movie_a', 'plot' => 'this is a love story'],
['title' => 'movie_b', 'plot' => 'love is a long story'],
['title' => 'movie_c', 'plot' => 'went on a trip'],
['title' => 'Carrie', 'year' => 1976],
['title' => 'Carrie', 'year' => 2002],
]);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testFindFilter(): void
{
// start-query
$movies = Movie::where('year', 2010)
->where('imdb.rating', '>', 8.5)
->get();
// end-query
$this->assertNotNull($movies);
$this->assertCount(2, $movies);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSkipLimit(): void
{
// start-skip-limit
$movies = Movie::where('year', 1999)
->skip(2)
->take(3)
->get();
// end-skip-limit
$this->assertNotNull($movies);
$this->assertCount(3, $movies);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSort(): void
{
// start-sort
$movies = Movie::where('countries', 'Indonesia')
->orderBy('year')
->orderBy('title', 'desc')
->get();
// end-sort
$this->assertNotNull($movies);
$this->assertCount(4, $movies);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testFirst(): void
{
// start-first
$movie = Movie::where('runtime', 30)
->orderBy('_id')
->first();
// end-first
$this->assertNotNull($movie);
$this->assertInstanceOf(Movie::class, $movie);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testText(): void
{
// start-text
$movies = Movie::where('$text', ['$search' => '"love story"'])
->get();
// end-text
$this->assertNotNull($movies);
$this->assertCount(1, $movies);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testTextRelevance(): void
{
// start-text-relevance
$movies = Movie::where('$text', ['$search' => '"love story"'])
->orderBy('score', ['$meta' => 'textScore'])
->get();
// end-text-relevance
$this->assertNotNull($movies);
$this->assertCount(1, $movies);
$this->assertEquals('this is a love story', $movies[0]->plot);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function exactArrayMatch(): void
{
// start-exact-array
$movies = Movie::where('countries', ['Indonesia', 'Canada'])
->get();
// end-exact-array
$this->assertNotNull($movies);
$this->assertCount(1, $movies);
$this->assertEquals('Title 1', $movies[0]->title);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function arrayElemMatch(): void
{
// start-elem-match
$movies = Movie::where('countries', 'in', ['Canada', 'Egypt'])
->get();
// end-elem-match
$this->assertNotNull($movies);
$this->assertCount(2, $movies);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testReadPreference(): void
{
// start-read-pref
$movies = Movie::where('title', 'Carrie')
->readPreference(ReadPreference::SECONDARY_PREFERRED)
->get();
// end-read-pref
$this->assertNotNull($movies);
$this->assertCount(2, $movies);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testQueryLog(): void
{
// start-query-log
DB::connection('mongodb')->enableQueryLog();
Movie::where('title', 'Carrie')->get();
Movie::where('year', '<', 2005)->get();
Movie::where('imdb.rating', '>', 8.5)->get();
$logs = DB::connection('mongodb')->getQueryLog();
foreach ($logs as $log) {
echo json_encode($log, JSON_PRETTY_PRINT);
}
// end-query-log
$this->assertNotNull($logs);
$this->expectOutputRegex('/^'
. '\{'
. '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"title\\\"\s*:\s*\\\"Carrie\\\"\}\}",'
. '\s*"bindings"\s*:\s*\[\],'
. '\s*"time"\s*:\s*\d+'
. '\}'
. '\{'
. '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"year\\\"\s*:\s*\{\\\"\\$lt\\\"\s*:\s*\{\\\"\\$numberInt\\\"\s*:\s*\\\"2005\\\"\}\}\}\}",'
. '\s*"bindings"\s*:\s*\[\],'
. '\s*"time"\s*:\s*\d+'
. '\}'
. '\{'
. '\s*"query"\s*:\s*"\{\\\"find\\\"\s*:\s*\\\"movies\\\",\s*\\\"filter\\\"\s*:\s*\{\\\"imdb.rating\\\"\s*:\s*\{\\\"\\$gt\\\"\s*:\s*\{\\\"\\$numberDouble\\\"\s*:\s*\\\"8\.5\\\"\}\}\}\}",'
. '\s*"bindings"\s*:\s*\[\],'
. '\s*"time"\s*:\s*\d+'
. '\}'
. '$/x');
}
}