Skip to content

Commit 15a120e

Browse files
Add tests
1 parent 1aa438c commit 15a120e

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

tests/Models/PlanetOid.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Models;
6+
7+
use MongoDB\Laravel\Eloquent\Model as Eloquent;
8+
9+
class PlanetOid extends Eloquent
10+
{
11+
protected $connection = 'mongodb';
12+
protected static $unguarded = true;
13+
14+
public function getIdAttribute($value = null)
15+
{
16+
return $value;
17+
}
18+
19+
public function visitors()
20+
{
21+
return $this->belongsToMany(SpaceExplorerOid::class);
22+
}
23+
}

tests/Models/SpaceExplorerOid.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Models;
6+
7+
use MongoDB\Laravel\Eloquent\Model as Eloquent;
8+
9+
class SpaceExplorerOid extends Eloquent
10+
{
11+
protected $connection = 'mongodb';
12+
protected static $unguarded = true;
13+
14+
public function getIdAttribute($value = null)
15+
{
16+
return $value;
17+
}
18+
19+
public function planetsVisited()
20+
{
21+
return $this->belongsToMany(PlanetOid::class);
22+
}
23+
}

tests/RelationsTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use MongoDB\Laravel\Tests\Models\Item;
1515
use MongoDB\Laravel\Tests\Models\Label;
1616
use MongoDB\Laravel\Tests\Models\Photo;
17+
use MongoDB\Laravel\Tests\Models\PlanetOid;
1718
use MongoDB\Laravel\Tests\Models\Role;
1819
use MongoDB\Laravel\Tests\Models\Skill;
1920
use MongoDB\Laravel\Tests\Models\Soft;
21+
use MongoDB\Laravel\Tests\Models\SpaceExplorerOid;
2022
use MongoDB\Laravel\Tests\Models\User;
2123

2224
class RelationsTest extends TestCase
@@ -35,6 +37,8 @@ public function tearDown(): void
3537
Photo::truncate();
3638
Label::truncate();
3739
Skill::truncate();
40+
SpaceExplorerOid::truncate();
41+
PlanetOid::truncate();
3842
}
3943

4044
public function testHasMany(): void
@@ -1273,4 +1277,105 @@ public function testWhereBelongsTo()
12731277

12741278
$this->assertCount(3, $items);
12751279
}
1280+
1281+
public function testBelongsToManyOid(): void
1282+
{
1283+
$explorer = SpaceExplorerOid::create(['name' => 'John Doe']);
1284+
1285+
// Add 2 explorer
1286+
$explorer->planetsVisited()->save(new PlanetOid(['name' => 'Mars']));
1287+
$explorer->planetsVisited()->create(['name' => 'Jupiter']);
1288+
1289+
// Refetch
1290+
$explorer = SpaceExplorerOid::with('planetsVisited')->find($explorer->_id);
1291+
$planet = PlanetOid::with('visitors')->first();
1292+
1293+
// Check for relation attributes
1294+
$this->assertArrayHasKey('space_explorer_oid_ids', $planet->getAttributes());
1295+
$this->assertArrayHasKey('planet_oid_ids', $explorer->getAttributes());
1296+
1297+
$planets = $explorer->getRelation('planetsVisited');
1298+
$explorers = $planet->getRelation('visitors');
1299+
1300+
$this->assertInstanceOf(Collection::class, $planets);
1301+
$this->assertInstanceOf(Collection::class, $explorers);
1302+
$this->assertInstanceOf(SpaceExplorerOid::class, $explorers[0]);
1303+
$this->assertInstanceOf(PlanetOid::class, $planets[0]);
1304+
$this->assertCount(2, $explorer->planetsVisited);
1305+
$this->assertCount(1, $planet->visitors);
1306+
1307+
// Now create a new explorer to an existing planet
1308+
$explorer = $planet->visitors()->create(['name' => 'skaywalker']);
1309+
1310+
$this->assertInstanceOf(Collection::class, $explorer->planetsVisited);
1311+
$this->assertInstanceOf(PlanetOid::class, $explorer->planetsVisited->first());
1312+
$this->assertCount(1, $explorer->planetsVisited);
1313+
1314+
// Get explorer and unattached planet
1315+
$explorer = SpaceExplorerOid::where('name', '=', 'skaywalker')->first();
1316+
$planet = PlanetOid::where('name', '=', 'Jupiter')->first();
1317+
1318+
// Check the models are what they should be
1319+
$this->assertInstanceOf(PlanetOid::class, $planet);
1320+
$this->assertInstanceOf(SpaceExplorerOid::class, $explorer);
1321+
1322+
// Assert they are not attached
1323+
$this->assertNotContains($planet->_id, $explorer->planet_oid_ids);
1324+
$this->assertNotContains($explorer->_id, $planet->space_explorer_oid_ids);
1325+
$this->assertCount(1, $explorer->planetsVisited);
1326+
$this->assertCount(1, $planet->visitors);
1327+
1328+
// Attach the planet to the explorer
1329+
$explorer->planetsVisited()->attach($planet);
1330+
1331+
// Get the new explorer model
1332+
$explorer = SpaceExplorerOid::where('name', '=', 'skaywalker')->first();
1333+
$planet = PlanetOid::where('name', '=', 'Mars')->first();
1334+
1335+
// Assert they are attached
1336+
$this->assertNotContains($planet->_id, $explorer->planet_oid_ids);
1337+
$this->assertNotContains($explorer->_id, $planet->space_explorer_oid_ids);
1338+
$this->assertCount(2, $explorer->planetsVisited);
1339+
$this->assertCount(2, $planet->visitors);
1340+
1341+
1342+
// Detach planets from explorer
1343+
$explorer->planetsVisited()->sync([]);
1344+
1345+
// Get the new user model
1346+
$explorer = SpaceExplorerOid::where('name', '=', 'skaywalker')->first();
1347+
$planet = PlanetOid::where('name', '=', 'Mars')->first();
1348+
1349+
// Assert they are attached
1350+
$this->assertNotContains($planet->_id, $explorer->planet_oid_ids);
1351+
$this->assertNotContains($explorer->_id, $planet->space_explorer_oid_ids);
1352+
$this->assertCount(0, $explorer->planetsVisited);
1353+
$this->assertCount(1, $planet->visitors);
1354+
}
1355+
1356+
public function testBelongsToManySyncOid(): void
1357+
{
1358+
// create test instances
1359+
$explorer = SpaceExplorerOid::create(['name' => 'John Doe']);
1360+
$planet1 = PlanetOid::create(['name' => 'Mars']);
1361+
$planet2 = PlanetOid::create(['name' => 'Jupiter']);
1362+
1363+
// Sync multiple
1364+
$explorer->planetsVisited()->sync([$planet1->_id, $planet2->_id]);
1365+
$this->assertCount(2, $explorer->planetsVisited);
1366+
1367+
// Sync single wrapped by an array
1368+
$explorer->planetsVisited()->sync([$planet1->_id]);
1369+
$explorer->load('planetsVisited');
1370+
1371+
$this->assertCount(1, $explorer->planetsVisited);
1372+
self::assertTrue($explorer->planetsVisited->first()->is($planet1));
1373+
1374+
// Sync single model
1375+
$explorer->planetsVisited()->sync($planet2);
1376+
$explorer->load('planetsVisited');
1377+
1378+
$this->assertCount(1, $explorer->planetsVisited);
1379+
self::assertTrue($explorer->planetsVisited->first()->is($planet2));
1380+
}
12761381
}

0 commit comments

Comments
 (0)