Skip to content

Commit 453139a

Browse files
authored
DOCSP-38327: add Query Builder examples to usage examples (mongodb#3259)
* DOCSP-38327: add qb examples to usage exs * add imports * wip * formatting * wip * fix tests? * fix tests? * wip * wip * wip: * formatting * formatting * formatting * fix tests * fix tests * small text changes * fix error * JS PR fixes 1 * add extra tests for each type of query * formatting * remove sort from deleteOne
1 parent 4af26e7 commit 453139a

23 files changed

+979
-402
lines changed

docs/includes/usage-examples/CountTest.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class CountTest extends TestCase
@@ -29,14 +30,24 @@ public function testCount(): void
2930
],
3031
]);
3132

32-
// begin-count
33+
// begin-eloquent-count
3334
$count = Movie::where('genres', 'Biography')
3435
->count();
3536

3637
echo 'Number of documents: ' . $count;
37-
// end-count
38+
// end-eloquent-count
3839

3940
$this->assertEquals(2, $count);
40-
$this->expectOutputString('Number of documents: 2');
41+
42+
// begin-qb-count
43+
$count = DB::table('movies')
44+
->where('genres', 'Biography')
45+
->count();
46+
47+
echo 'Number of documents: ' . $count;
48+
// end-qb-count
49+
50+
$this->assertEquals(2, $count);
51+
$this->expectOutputString('Number of documents: 2Number of documents: 2');
4152
}
4253
}

docs/includes/usage-examples/DeleteManyTest.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class DeleteManyTest extends TestCase
@@ -29,14 +30,35 @@ public function testDeleteMany(): void
2930
],
3031
]);
3132

32-
// begin-delete-many
33+
// begin-eloquent-delete-many
3334
$deleted = Movie::where('year', '<=', 1910)
3435
->delete();
3536

3637
echo 'Deleted documents: ' . $deleted;
37-
// end-delete-many
38+
// end-eloquent-delete-many
3839

3940
$this->assertEquals(2, $deleted);
40-
$this->expectOutputString('Deleted documents: 2');
41+
42+
Movie::insert([
43+
[
44+
'title' => 'Train Pulling into a Station',
45+
'year' => 1896,
46+
],
47+
[
48+
'title' => 'The Ball Game',
49+
'year' => 1898,
50+
],
51+
]);
52+
53+
// begin-qb-delete-many
54+
$deleted = DB::table('movies')
55+
->where('year', '<=', 1910)
56+
->delete();
57+
58+
echo 'Deleted documents: ' . $deleted;
59+
// end-qb-delete-many
60+
61+
$this->assertEquals(2, $deleted);
62+
$this->expectOutputString('Deleted documents: 2Deleted documents: 2');
4163
}
4264
}

docs/includes/usage-examples/DeleteOneTest.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class DeleteOneTest extends TestCase
@@ -25,16 +26,33 @@ public function testDeleteOne(): void
2526
],
2627
]);
2728

28-
// begin-delete-one
29+
// begin-eloquent-delete-one
2930
$deleted = Movie::where('title', 'Quiz Show')
30-
->orderBy('_id')
3131
->limit(1)
3232
->delete();
3333

3434
echo 'Deleted documents: ' . $deleted;
35-
// end-delete-one
35+
// end-eloquent-delete-one
3636

3737
$this->assertEquals(1, $deleted);
38-
$this->expectOutputString('Deleted documents: 1');
38+
39+
Movie::insert([
40+
[
41+
'title' => 'Quiz Show',
42+
'runtime' => 133,
43+
],
44+
]);
45+
46+
// begin-qb-delete-one
47+
$deleted = DB::table('movies')
48+
->where('title', 'Quiz Show')
49+
->limit(1)
50+
->delete();
51+
52+
echo 'Deleted documents: ' . $deleted;
53+
// end-qb-delete-one
54+
55+
$this->assertEquals(1, $deleted);
56+
$this->expectOutputString('Deleted documents: 1Deleted documents: 1');
3957
}
4058
}

docs/includes/usage-examples/DistinctTest.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class DistinctTest extends TestCase
@@ -45,15 +46,25 @@ public function testDistinct(): void
4546
],
4647
]);
4748

48-
// begin-distinct
49+
// begin-eloquent-distinct
4950
$ratings = Movie::where('directors', 'Sofia Coppola')
5051
->select('imdb.rating')
5152
->distinct()
5253
->get();
5354

5455
echo $ratings;
55-
// end-distinct
56+
// end-eloquent-distinct
5657

57-
$this->expectOutputString('[[6.4],[7.8]]');
58+
// begin-qb-distinct
59+
$ratings = DB::table('movies')
60+
->where('directors', 'Sofia Coppola')
61+
->select('imdb.rating')
62+
->distinct()
63+
->get();
64+
65+
echo $ratings;
66+
// end-qb-distinct
67+
68+
$this->expectOutputString('[[6.4],[7.8]][6.4,7.8]');
5869
}
5970
}

docs/includes/usage-examples/FindManyTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class FindManyTest extends TestCase
@@ -33,11 +34,20 @@ public function testFindMany(): void
3334
],
3435
]);
3536

36-
// begin-find
37+
// begin-eloquent-find
3738
$movies = Movie::where('runtime', '>', 900)
3839
->orderBy('_id')
3940
->get();
40-
// end-find
41+
// end-eloquent-find
42+
43+
$this->assertEquals(2, $movies->count());
44+
45+
// begin-qb-find
46+
$movies = DB::table('movies')
47+
->where('runtime', '>', 900)
48+
->orderBy('_id')
49+
->get();
50+
// end-qb-find
4151

4252
$this->assertEquals(2, $movies->count());
4353
}

docs/includes/usage-examples/FindOneTest.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class FindOneTest extends TestCase
@@ -13,7 +14,7 @@ class FindOneTest extends TestCase
1314
* @runInSeparateProcess
1415
* @preserveGlobalState disabled
1516
*/
16-
public function testFindOne(): void
17+
public function testEloquentFindOne(): void
1718
{
1819
require_once __DIR__ . '/Movie.php';
1920

@@ -22,15 +23,41 @@ public function testFindOne(): void
2223
['title' => 'The Shawshank Redemption', 'directors' => ['Frank Darabont', 'Rob Reiner']],
2324
]);
2425

25-
// begin-find-one
26+
// begin-eloquent-find-one
2627
$movie = Movie::where('directors', 'Rob Reiner')
27-
->orderBy('_id')
28+
->orderBy('id')
2829
->first();
2930

3031
echo $movie->toJson();
31-
// end-find-one
32+
// end-eloquent-find-one
3233

3334
$this->assertInstanceOf(Movie::class, $movie);
3435
$this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\]}$/');
3536
}
37+
38+
/**
39+
* @runInSeparateProcess
40+
* @preserveGlobalState disabled
41+
*/
42+
public function testQBFindOne(): void
43+
{
44+
require_once __DIR__ . '/Movie.php';
45+
46+
Movie::truncate();
47+
Movie::insert([
48+
['title' => 'The Shawshank Redemption', 'directors' => ['Frank Darabont', 'Rob Reiner']],
49+
]);
50+
51+
// begin-qb-find-one
52+
$movie = DB::table('movies')
53+
->where('directors', 'Rob Reiner')
54+
->orderBy('_id')
55+
->first();
56+
57+
echo $movie['title'];
58+
// end-qb-find-one
59+
60+
$this->assertSame($movie['title'], 'The Shawshank Redemption');
61+
$this->expectOutputString('The Shawshank Redemption');
62+
}
3663
}

docs/includes/usage-examples/InsertManyTest.php

+26-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Models\Movie;
88
use DateTimeImmutable;
9+
use Illuminate\Support\Facades\DB;
910
use MongoDB\BSON\UTCDateTime;
1011
use MongoDB\Laravel\Tests\TestCase;
1112

@@ -21,7 +22,7 @@ public function testInsertMany(): void
2122

2223
Movie::truncate();
2324

24-
// begin-insert-many
25+
// begin-eloquent-insert-many
2526
$success = Movie::insert([
2627
[
2728
'title' => 'Anatomy of a Fall',
@@ -38,9 +39,31 @@ public function testInsertMany(): void
3839
]);
3940

4041
echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
41-
// end-insert-many
42+
// end-eloquent-insert-many
4243

4344
$this->assertTrue($success);
44-
$this->expectOutputString('Insert operation success: yes');
45+
46+
// begin-qb-insert-many
47+
$success = DB::table('movies')
48+
->insert([
49+
[
50+
'title' => 'Anatomy of a Fall',
51+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-08-23')),
52+
],
53+
[
54+
'title' => 'The Boy and the Heron',
55+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-12-08')),
56+
],
57+
[
58+
'title' => 'Passages',
59+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-06-28')),
60+
],
61+
]);
62+
63+
echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
64+
// end-qb-insert-many
65+
66+
$this->assertTrue($success);
67+
$this->expectOutputString('Insert operation success: yesInsert operation success: yes');
4568
}
4669
}

docs/includes/usage-examples/InsertOneTest.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Http\Controllers;
66

77
use App\Models\Movie;
8+
use Illuminate\Support\Facades\DB;
89
use MongoDB\Laravel\Tests\TestCase;
910

1011
class InsertOneTest extends TestCase
@@ -13,23 +14,48 @@ class InsertOneTest extends TestCase
1314
* @runInSeparateProcess
1415
* @preserveGlobalState disabled
1516
*/
16-
public function testInsertOne(): void
17+
public function testEloquentInsertOne(): void
1718
{
1819
require_once __DIR__ . '/Movie.php';
1920

2021
Movie::truncate();
2122

22-
// begin-insert-one
23+
// begin-eloquent-insert-one
2324
$movie = Movie::create([
2425
'title' => 'Marriage Story',
2526
'year' => 2019,
2627
'runtime' => 136,
2728
]);
2829

2930
echo $movie->toJson();
30-
// end-insert-one
31+
// end-eloquent-insert-one
3132

3233
$this->assertInstanceOf(Movie::class, $movie);
34+
$this->assertSame($movie->title, 'Marriage Story');
3335
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/');
3436
}
37+
38+
/**
39+
* @runInSeparateProcess
40+
* @preserveGlobalState disabled
41+
*/
42+
public function testQBInsertOne(): void
43+
{
44+
require_once __DIR__ . '/Movie.php';
45+
46+
Movie::truncate();
47+
48+
// begin-qb-insert-one
49+
$success = DB::table('movies')
50+
->insert([
51+
'title' => 'Marriage Story',
52+
'year' => 2019,
53+
'runtime' => 136,
54+
]);
55+
56+
echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
57+
// end-qb-insert-one
58+
59+
$this->expectOutputString('Insert operation success: yes');
60+
}
3561
}

0 commit comments

Comments
 (0)