Skip to content

Commit 56a7233

Browse files
authored
Add tests on Adding New Fields and fetch relationships withThrashed (#2644)
1 parent 7fee8be commit 56a7233

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

tests/ModelTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ public function testUpdate(): void
114114

115115
$check = User::find($user->_id);
116116
$this->assertEquals(20, $check->age);
117+
118+
$check->age = 24;
119+
$check->fullname = 'Hans Thomas'; // new field
120+
$check->save();
121+
122+
$check = User::find($user->_id);
123+
$this->assertEquals(24, $check->age);
124+
$this->assertEquals('Hans Thomas', $check->fullname);
117125
}
118126

119127
public function testManualStringId(): void

tests/Models/Soft.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public function prunable(): Builder
2525
{
2626
return $this->newQuery();
2727
}
28+
29+
public function user()
30+
{
31+
return $this->belongsTo(User::class);
32+
}
2833
}

tests/Models/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ public function books()
5151
return $this->hasMany(Book::class, 'author_id');
5252
}
5353

54+
public function softs()
55+
{
56+
return $this->hasMany(Soft::class);
57+
}
58+
59+
public function softsWithTrashed()
60+
{
61+
return $this->hasMany(Soft::class)->withTrashed();
62+
}
63+
5464
public function sqlBooks()
5565
{
5666
return $this->hasMany(SqlBook::class, 'author_id');

tests/RelationsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use MongoDB\Laravel\Tests\Models\Item;
1414
use MongoDB\Laravel\Tests\Models\Photo;
1515
use MongoDB\Laravel\Tests\Models\Role;
16+
use MongoDB\Laravel\Tests\Models\Soft;
1617
use MongoDB\Laravel\Tests\Models\User;
1718

1819
class RelationsTest extends TestCase
@@ -50,6 +51,24 @@ public function testHasMany(): void
5051
$this->assertCount(3, $items);
5152
}
5253

54+
public function testHasManyWithTrashed(): void
55+
{
56+
$user = User::create(['name' => 'George R. R. Martin']);
57+
$first = Soft::create(['title' => 'A Game of Thrones', 'user_id' => $user->_id]);
58+
$second = Soft::create(['title' => 'The Witcher', 'user_id' => $user->_id]);
59+
60+
self::assertNull($first->deleted_at);
61+
self::assertEquals($user->_id, $first->user->_id);
62+
self::assertEquals([$first->_id, $second->_id], $user->softs->pluck('_id')->toArray());
63+
64+
$first->delete();
65+
$user->refresh();
66+
67+
self::assertNotNull($first->deleted_at);
68+
self::assertEquals([$second->_id], $user->softs->pluck('_id')->toArray());
69+
self::assertEquals([$first->_id, $second->_id], $user->softsWithTrashed->pluck('_id')->toArray());
70+
}
71+
5372
public function testBelongsTo(): void
5473
{
5574
$user = User::create(['name' => 'George R. R. Martin']);

0 commit comments

Comments
 (0)