|
| 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 | +} |
0 commit comments