Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-38327: add Query Builder examples to usage examples #3259

Merged
merged 21 commits into from
Feb 6, 2025
Merged
17 changes: 14 additions & 3 deletions docs/includes/usage-examples/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class CountTest extends TestCase
Expand All @@ -29,14 +30,24 @@ public function testCount(): void
],
]);

// begin-count
// begin-eloquent-count
$count = Movie::where('genres', 'Biography')
->count();

echo 'Number of documents: ' . $count;
// end-count
// end-eloquent-count

$this->assertEquals(2, $count);
$this->expectOutputString('Number of documents: 2');

// begin-qb-count
$count = DB::table('movies')
->where('genres', 'Biography')
->count();

echo 'Number of documents: ' . $count;
// end-qb-count

$this->assertEquals(2, $count);
$this->expectOutputString('Number of documents: 2Number of documents: 2');
}
}
28 changes: 25 additions & 3 deletions docs/includes/usage-examples/DeleteManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class DeleteManyTest extends TestCase
Expand All @@ -29,14 +30,35 @@ public function testDeleteMany(): void
],
]);

// begin-delete-many
// begin-eloquent-delete-many
$deleted = Movie::where('year', '<=', 1910)
->delete();

echo 'Deleted documents: ' . $deleted;
// end-delete-many
// end-eloquent-delete-many

$this->assertEquals(2, $deleted);
$this->expectOutputString('Deleted documents: 2');

Movie::insert([
[
'title' => 'Train Pulling into a Station',
'year' => 1896,
],
[
'title' => 'The Ball Game',
'year' => 1898,
],
]);

// begin-qb-delete-many
$deleted = DB::table('movies')
->where('year', '<=', 1910)
->delete();

echo 'Deleted documents: ' . $deleted;
// end-qb-delete-many

$this->assertEquals(2, $deleted);
$this->expectOutputString('Deleted documents: 2Deleted documents: 2');
}
}
26 changes: 22 additions & 4 deletions docs/includes/usage-examples/DeleteOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class DeleteOneTest extends TestCase
Expand All @@ -25,16 +26,33 @@ public function testDeleteOne(): void
],
]);

// begin-delete-one
// begin-eloquent-delete-one
$deleted = Movie::where('title', 'Quiz Show')
->orderBy('_id')
->limit(1)
->delete();

echo 'Deleted documents: ' . $deleted;
// end-delete-one
// end-eloquent-delete-one

$this->assertEquals(1, $deleted);
$this->expectOutputString('Deleted documents: 1');

Movie::insert([
[
'title' => 'Quiz Show',
'runtime' => 133,
],
]);

// begin-qb-delete-one
$deleted = DB::table('movies')
->where('title', 'Quiz Show')
->limit(1)
->delete();

echo 'Deleted documents: ' . $deleted;
// end-qb-delete-one

$this->assertEquals(1, $deleted);
$this->expectOutputString('Deleted documents: 1Deleted documents: 1');
}
}
17 changes: 14 additions & 3 deletions docs/includes/usage-examples/DistinctTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class DistinctTest extends TestCase
Expand Down Expand Up @@ -45,15 +46,25 @@ public function testDistinct(): void
],
]);

// begin-distinct
// begin-eloquent-distinct
$ratings = Movie::where('directors', 'Sofia Coppola')
->select('imdb.rating')
->distinct()
->get();

echo $ratings;
// end-distinct
// end-eloquent-distinct

$this->expectOutputString('[[6.4],[7.8]]');
// begin-qb-distinct
$ratings = DB::table('movies')
->where('directors', 'Sofia Coppola')
->select('imdb.rating')
->distinct()
->get();

echo $ratings;
// end-qb-distinct

$this->expectOutputString('[[6.4],[7.8]][6.4,7.8]');
}
}
14 changes: 12 additions & 2 deletions docs/includes/usage-examples/FindManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class FindManyTest extends TestCase
Expand Down Expand Up @@ -33,11 +34,20 @@ public function testFindMany(): void
],
]);

// begin-find
// begin-eloquent-find
$movies = Movie::where('runtime', '>', 900)
->orderBy('_id')
->get();
// end-find
// end-eloquent-find
GromNaN marked this conversation as resolved.
Show resolved Hide resolved

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

// begin-qb-find
$movies = DB::table('movies')
->where('runtime', '>', 900)
->orderBy('_id')
->get();
// end-qb-find

$this->assertEquals(2, $movies->count());
}
Expand Down
35 changes: 31 additions & 4 deletions docs/includes/usage-examples/FindOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class FindOneTest extends TestCase
Expand All @@ -13,7 +14,7 @@ class FindOneTest extends TestCase
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testFindOne(): void
public function testEloquentFindOne(): void
{
require_once __DIR__ . '/Movie.php';

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

// begin-find-one
// begin-eloquent-find-one
$movie = Movie::where('directors', 'Rob Reiner')
->orderBy('_id')
->orderBy('id')
->first();

echo $movie->toJson();
// end-find-one
// end-eloquent-find-one

$this->assertInstanceOf(Movie::class, $movie);
$this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\]}$/');
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testQBFindOne(): void
{
require_once __DIR__ . '/Movie.php';

Movie::truncate();
Movie::insert([
['title' => 'The Shawshank Redemption', 'directors' => ['Frank Darabont', 'Rob Reiner']],
]);

// begin-qb-find-one
$movie = DB::table('movies')
->where('directors', 'Rob Reiner')
->orderBy('_id')
->first();

echo $movie['title'];
// end-qb-find-one

$this->assertSame($movie['title'], 'The Shawshank Redemption');
$this->expectOutputString('The Shawshank Redemption');
}
}
29 changes: 26 additions & 3 deletions docs/includes/usage-examples/InsertManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Movie;
use DateTimeImmutable;
use Illuminate\Support\Facades\DB;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Tests\TestCase;

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

Movie::truncate();

// begin-insert-many
// begin-eloquent-insert-many
$success = Movie::insert([
[
'title' => 'Anatomy of a Fall',
Expand All @@ -38,9 +39,31 @@ public function testInsertMany(): void
]);

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

$this->assertTrue($success);
$this->expectOutputString('Insert operation success: yes');

// begin-qb-insert-many
$success = DB::table('movies')
->insert([
[
'title' => 'Anatomy of a Fall',
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-08-23')),
],
[
'title' => 'The Boy and the Heron',
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-12-08')),
],
[
'title' => 'Passages',
'release_date' => new UTCDateTime(new DateTimeImmutable('2023-06-28')),
],
]);

echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
// end-qb-insert-many

$this->assertTrue($success);
$this->expectOutputString('Insert operation success: yesInsert operation success: yes');
}
}
32 changes: 29 additions & 3 deletions docs/includes/usage-examples/InsertOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers;

use App\Models\Movie;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Tests\TestCase;

class InsertOneTest extends TestCase
Expand All @@ -13,23 +14,48 @@ class InsertOneTest extends TestCase
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testInsertOne(): void
public function testEloquentInsertOne(): void
{
require_once __DIR__ . '/Movie.php';

Movie::truncate();

// begin-insert-one
// begin-eloquent-insert-one
$movie = Movie::create([
'title' => 'Marriage Story',
'year' => 2019,
'runtime' => 136,
]);

echo $movie->toJson();
// end-insert-one
// end-eloquent-insert-one

$this->assertInstanceOf(Movie::class, $movie);
$this->assertSame($movie->title, 'Marriage Story');
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/');
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testQBInsertOne(): void
{
require_once __DIR__ . '/Movie.php';

Movie::truncate();

// begin-qb-insert-one
$success = DB::table('movies')
->insert([
'title' => 'Marriage Story',
'year' => 2019,
'runtime' => 136,
]);

echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
// end-qb-insert-one

$this->expectOutputString('Insert operation success: yes');
}
}
Loading