Skip to content

Commit 21dca2b

Browse files
committed
Add tests to doc examples
1 parent 945882b commit 21dca2b

File tree

5 files changed

+173
-53
lines changed

5 files changed

+173
-53
lines changed

docs/includes/eloquent-models/relationships/RelationshipController.php

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
namespace App\Http\Controllers;
66

7+
use App\Models\Cargo;
8+
use App\Models\Moon;
79
use App\Models\Orbit;
10+
use App\Models\Passenger;
811
use App\Models\Planet;
12+
use App\Models\SpaceExplorer;
13+
use App\Models\SpaceShip;
914
use Illuminate\Http\Request;
1015

11-
class RelationshipController extends Controller
16+
class RelationshipController
1217
{
13-
private function oneToOne()
18+
public function oneToOne()
1419
{
1520
// begin one-to-one save
1621
$planet = new Planet();
@@ -26,7 +31,7 @@ private function oneToOne()
2631
// end one-to-one save
2732
}
2833

29-
private function oneToMany()
34+
public function oneToMany()
3035
{
3136
// begin one-to-many save
3237
$planet = new Planet();
@@ -47,7 +52,7 @@ private function oneToMany()
4752
// end one-to-many save
4853
}
4954

50-
private function planetOrbitDynamic()
55+
public function planetOrbitDynamic()
5156
{
5257
// begin planet orbit dynamic property example
5358
$planet = Planet::first();
@@ -58,7 +63,7 @@ private function planetOrbitDynamic()
5863
// end planet orbit dynamic property example
5964
}
6065

61-
private function planetMoonsDynamic()
66+
public function planetMoonsDynamic()
6267
{
6368
// begin planet moons dynamic property example
6469
$planet = Planet::first();
@@ -69,7 +74,7 @@ private function planetMoonsDynamic()
6974
// end planet moons dynamic property example
7075
}
7176

72-
private function manyToMany()
77+
public function manyToMany()
7378
{
7479
// begin many-to-many save
7580
$planetEarth = new Planet();
@@ -106,18 +111,18 @@ private function manyToMany()
106111
// end many-to-many save
107112
}
108113

109-
private function manyToManyDynamic()
114+
public function manyToManyDynamic()
110115
{
111116
// begin many-to-many dynamic property example
112117
$planet = Planet::first();
113118
$explorers = $planet->visitors;
114119

115-
$spaceExplorer = SpaceExplorer:first();
120+
$spaceExplorer = SpaceExplorer::first();
116121
$explored = $spaceExplorer->planetsVisited;
117122
// end many-to-many dynamic property example
118123
}
119124

120-
private function embedsMany()
125+
public function embedsMany()
121126
{
122127
// begin embedsMany save
123128
$spaceship = new SpaceShip();
@@ -137,7 +142,7 @@ private function embedsMany()
137142
// end embedsMany save
138143
}
139144

140-
private function crossDatabase()
145+
public function crossDatabase()
141146
{
142147
// begin cross-database save
143148
$spaceship = new SpaceShip();
@@ -155,40 +160,4 @@ private function crossDatabase()
155160
$spaceship->passengers()->save($passengerDwayne);
156161
// end cross-database save
157162
}
158-
159-
/**
160-
* Store a newly created resource in storage.
161-
*/
162-
public function store(Request $request)
163-
{
164-
}
165-
166-
/**
167-
* Display the specified resource.
168-
*/
169-
public function show()
170-
{
171-
return 'ok';
172-
}
173-
174-
/**
175-
* Show the form for editing the specified resource.
176-
*/
177-
public function edit(Planet $planet)
178-
{
179-
}
180-
181-
/**
182-
* Update the specified resource in storage.
183-
*/
184-
public function update(Request $request, Planet $planet)
185-
{
186-
}
187-
188-
/**
189-
* Remove the specified resource from storage.
190-
*/
191-
public function destroy(Planet $planet)
192-
{
193-
}
194163
}

docs/includes/eloquent-models/relationships/cross-db/Passenger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace App\Models;
66

7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78
use MongoDB\Laravel\Eloquent\Model;
8-
use MongoDB\Laravel\Relations\BelongsTo;
99

1010
class Passenger extends Model
1111
{

src/Query/Builder.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,11 +1158,6 @@ protected function compileWheres(): array
11581158
return $compiled;
11591159
}
11601160

1161-
/**
1162-
* @param array $where
1163-
*
1164-
* @return array
1165-
*/
11661161
protected function compileWhereBasic(array $where): array
11671162
{
11681163
$column = $where['column'];

tests/DocumentationExamplesTest.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests;
6+
7+
use App\Http\Controllers\RelationshipController;
8+
use App\Models\Cargo;
9+
use App\Models\Moon;
10+
use App\Models\Orbit;
11+
use App\Models\Passenger;
12+
use App\Models\Planet;
13+
use App\Models\SpaceExplorer;
14+
use App\Models\SpaceShip;
15+
use Illuminate\Database\Schema\Blueprint;
16+
use Illuminate\Database\Schema\SQLiteBuilder;
17+
use Illuminate\Support\Facades\Schema;
18+
19+
use function assert;
20+
21+
class DocumentationExamplesTest extends TestCase
22+
{
23+
/**
24+
* @runInSeparateProcess
25+
* @preserveGlobalState disabled
26+
*/
27+
public function testOneToOne(): void
28+
{
29+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/one-to-one/Orbit.php';
30+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/one-to-one/Planet.php';
31+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/RelationshipController.php';
32+
33+
// Clear the database
34+
Planet::truncate();
35+
36+
$controller = new RelationshipController();
37+
$controller->oneToOne();
38+
39+
$planet = Planet::first();
40+
$this->assertInstanceOf(Planet::class, $planet);
41+
$this->assertInstanceOf(Orbit::class, $planet->orbit);
42+
}
43+
44+
/**
45+
* @runInSeparateProcess
46+
* @preserveGlobalState disabled
47+
*/
48+
public function testOneToMany(): void
49+
{
50+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/one-to-many/Planet.php';
51+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/one-to-many/Moon.php';
52+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/RelationshipController.php';
53+
54+
// Clear the database
55+
Planet::truncate();
56+
57+
$controller = new RelationshipController();
58+
$controller->oneToMany();
59+
60+
$planet = Planet::first();
61+
$this->assertInstanceOf(Planet::class, $planet);
62+
$this->assertCount(2, $planet->moons);
63+
$this->assertInstanceOf(Moon::class, $planet->moons->first());
64+
65+
$controller->planetMoonsDynamic();
66+
}
67+
68+
/**
69+
* @runInSeparateProcess
70+
* @preserveGlobalState disabled
71+
*/
72+
public function testManyToMany(): void
73+
{
74+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/many-to-many/Planet.php';
75+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/many-to-many/SpaceExplorer.php';
76+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/RelationshipController.php';
77+
78+
// Clear the database
79+
Planet::truncate();
80+
SpaceExplorer::truncate();
81+
82+
$controller = new RelationshipController();
83+
$controller->manyToMany();
84+
85+
$planet = Planet::where('name', 'Earth')->first();
86+
$this->assertInstanceOf(Planet::class, $planet);
87+
$this->assertCount(3, $planet->visitors);
88+
$this->assertInstanceOf(SpaceExplorer::class, $planet->visitors->first());
89+
90+
$explorer = SpaceExplorer::where('name', 'Jean-Luc Picard')->first();
91+
$this->assertInstanceOf(SpaceExplorer::class, $explorer);
92+
$this->assertCount(3, $explorer->planetsVisited);
93+
$this->assertInstanceOf(Planet::class, $explorer->planetsVisited->first());
94+
95+
$controller->manyToManyDynamic();
96+
}
97+
98+
/**
99+
* @runInSeparateProcess
100+
* @preserveGlobalState disabled
101+
*/
102+
public function testEmbedsMany(): void
103+
{
104+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/embeds/Cargo.php';
105+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/embeds/SpaceShip.php';
106+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/RelationshipController.php';
107+
108+
// Clear the database
109+
SpaceShip::truncate();
110+
111+
$controller = new RelationshipController();
112+
$controller->embedsMany();
113+
114+
$spaceship = SpaceShip::first();
115+
$this->assertInstanceOf(SpaceShip::class, $spaceship);
116+
$this->assertCount(2, $spaceship->cargo);
117+
$this->assertInstanceOf(Cargo::class, $spaceship->cargo->first());
118+
}
119+
120+
/**
121+
* @r unInSeparateProcess
122+
* @ preserveGlobalState disabled
123+
*/
124+
public function testCrossDatabase(): void
125+
{
126+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/cross-db/Passenger.php';
127+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/cross-db/SpaceShip.php';
128+
require_once __DIR__ . '/../docs/includes/eloquent-models/relationships/RelationshipController.php';
129+
130+
$schema = Schema::connection('sqlite');
131+
assert($schema instanceof SQLiteBuilder);
132+
133+
$schema->dropIfExists('space_ships');
134+
$schema->create('space_ships', function (Blueprint $table) {
135+
$table->id();
136+
$table->string('name');
137+
$table->timestamps();
138+
});
139+
140+
// Clear the database
141+
Passenger::truncate();
142+
SpaceShip::truncate();
143+
144+
$controller = new RelationshipController();
145+
$controller->crossDatabase();
146+
147+
$spaceship = SpaceShip::first();
148+
$this->assertInstanceOf(SpaceShip::class, $spaceship);
149+
$this->assertCount(2, $spaceship->passengers);
150+
$this->assertInstanceOf(Passenger::class, $spaceship->passengers->first());
151+
152+
$passenger = Passenger::first();
153+
$this->assertInstanceOf(Passenger::class, $passenger);
154+
$this->assertInstanceOf(SpaceShip::class, $passenger->spaceship);
155+
}
156+
}

tests/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function username(): Attribute
130130
{
131131
return Attribute::make(
132132
get: fn ($value) => $value,
133-
set: fn ($value) => Str::slug($value)
133+
set: fn ($value) => Str::slug($value),
134134
);
135135
}
136136

0 commit comments

Comments
 (0)