Skip to content

Commit 5e542bc

Browse files
committed
Merge remote-tracking branch 'upstream/4.0' into 4.1
2 parents 849cb1f + 25bd203 commit 5e542bc

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

.github/workflows/build-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
php:
2323
- '8.1'
2424
- '8.2'
25+
- '8.3'
2526

2627
steps:
2728
- uses: actions/checkout@v4

src/Query/Builder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,11 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not =
613613
/** @inheritdoc */
614614
public function insert(array $values)
615615
{
616+
// Allow empty insert batch for consistency with Eloquent SQL
617+
if ($values === []) {
618+
return true;
619+
}
620+
616621
// Since every insert gets treated like a batch insert, we will have to detect
617622
// if the user is inserting a single document or an array of documents.
618623
$batch = true;

tests/ModelTest.php

Lines changed: 14 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
@@ -217,6 +225,12 @@ public function testFind(): void
217225
$this->assertEquals(35, $check->age);
218226
}
219227

228+
public function testInsertEmpty(): void
229+
{
230+
$success = User::insert([]);
231+
$this->assertTrue($success);
232+
}
233+
220234
public function testGet(): void
221235
{
222236
User::insert([

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)