Skip to content

Add tests on Adding New Fields and fetch relationships withThrashed #2644

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

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public function testUpdate(): void

$check = User::find($user->_id);
$this->assertEquals(20, $check->age);

$check->age = 24;
$check->fullname = 'Hans Thomas'; // new field
$check->save();

$check = User::find($user->_id);
$this->assertEquals(24, $check->age);
$this->assertEquals('Hans Thomas', $check->fullname);
}

public function testManualStringId(): void
Expand Down
5 changes: 5 additions & 0 deletions tests/Models/Soft.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function prunable(): Builder
{
return $this->newQuery();
}

public function user()
{
return $this->belongsTo(User::class);
}
}
10 changes: 10 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public function books()
return $this->hasMany(Book::class, 'author_id');
}

public function softs()
{
return $this->hasMany(Soft::class);
}

public function softsWithTrashed()
{
return $this->hasMany(Soft::class)->withTrashed();
}

public function sqlBooks()
{
return $this->hasMany(SqlBook::class, 'author_id');
Expand Down
19 changes: 19 additions & 0 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use MongoDB\Laravel\Tests\Models\Item;
use MongoDB\Laravel\Tests\Models\Photo;
use MongoDB\Laravel\Tests\Models\Role;
use MongoDB\Laravel\Tests\Models\Soft;
use MongoDB\Laravel\Tests\Models\User;

class RelationsTest extends TestCase
Expand Down Expand Up @@ -50,6 +51,24 @@ public function testHasMany(): void
$this->assertCount(3, $items);
}

public function testHasManyWithTrashed(): void
{
$user = User::create(['name' => 'George R. R. Martin']);
$first = Soft::create(['title' => 'A Game of Thrones', 'user_id' => $user->_id]);
$second = Soft::create(['title' => 'The Witcher', 'user_id' => $user->_id]);

self::assertNull($first->deleted_at);
self::assertEquals($user->_id, $first->user->_id);
self::assertEquals([$first->_id, $second->_id], $user->softs->pluck('_id')->toArray());

$first->delete();
$user->refresh();

self::assertNotNull($first->deleted_at);
self::assertEquals([$second->_id], $user->softs->pluck('_id')->toArray());
self::assertEquals([$first->_id, $second->_id], $user->softsWithTrashed->pluck('_id')->toArray());
}

public function testBelongsTo(): void
{
$user = User::create(['name' => 'George R. R. Martin']);
Expand Down