Skip to content

Commit 206745b

Browse files
committed
Improve Model::createOrFirst() tests and error checking
1 parent 19fc801 commit 206745b

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

src/Eloquent/Builder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Database\ConnectionInterface;
88
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
9+
use InvalidArgumentException;
910
use MongoDB\Driver\Cursor;
1011
use MongoDB\Laravel\Collection;
1112
use MongoDB\Laravel\Helpers\QueriesRelationships;
@@ -195,6 +196,10 @@ public function raw($value = null)
195196
*/
196197
public function createOrFirst(array $attributes = [], array $values = []): Model
197198
{
199+
if ($attributes === []) {
200+
throw new InvalidArgumentException('You must provide attributes to check for duplicates');
201+
}
202+
198203
// Apply casting and default values to the attributes
199204
$instance = $this->newModelInstance($values + $attributes);
200205
$values = $instance->getAttributes();

src/Query/Builder.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,11 +1154,6 @@ protected function compileWheres(): array
11541154
return $compiled;
11551155
}
11561156

1157-
/**
1158-
* @param array $where
1159-
*
1160-
* @return array
1161-
*/
11621157
protected function compileWhereBasic(array $where): array
11631158
{
11641159
$column = $where['column'];

tests/ModelTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\Eloquent\ModelNotFoundException;
1212
use Illuminate\Support\Facades\Date;
1313
use Illuminate\Support\Str;
14+
use InvalidArgumentException;
1415
use MongoDB\BSON\Binary;
1516
use MongoDB\BSON\ObjectID;
1617
use MongoDB\BSON\UTCDateTime;
@@ -1047,40 +1048,47 @@ public function testNumericFieldName(): void
10471048

10481049
public function testCreateOrFirst()
10491050
{
1050-
$user1 = User::createOrFirst(['email' => 'taylorotwell@gmail.com']);
1051+
$user1 = User::createOrFirst(['email' => 'john.doe@example.com']);
10511052

1052-
$this->assertSame('taylorotwell@gmail.com', $user1->email);
1053+
$this->assertSame('john.doe@example.com', $user1->email);
10531054
$this->assertNull($user1->name);
10541055
$this->assertTrue($user1->wasRecentlyCreated);
10551056

10561057
$user2 = User::createOrFirst(
1057-
['email' => 'taylorotwell@gmail.com'],
1058-
['name' => 'Taylor Otwell', 'birthday' => new DateTime('1987-05-28')],
1058+
['email' => 'john.doe@example.com'],
1059+
['name' => 'John Doe', 'birthday' => new DateTime('1987-05-28')],
10591060
);
10601061

10611062
$this->assertEquals($user1->id, $user2->id);
1062-
$this->assertSame('taylorotwell@gmail.com', $user2->email);
1063+
$this->assertSame('john.doe@example.com', $user2->email);
10631064
$this->assertNull($user2->name);
10641065
$this->assertNull($user2->birthday);
10651066
$this->assertFalse($user2->wasRecentlyCreated);
10661067

10671068
$user3 = User::createOrFirst(
1068-
['email' => 'abigailotwell@gmail.com'],
1069-
['name' => 'Abigail Otwell', 'birthday' => new DateTime('1987-05-28')],
1069+
['email' => 'jane.doe@example.com'],
1070+
['name' => 'Jane Doe', 'birthday' => new DateTime('1987-05-28')],
10701071
);
10711072

10721073
$this->assertNotEquals($user3->id, $user1->id);
1073-
$this->assertSame('abigailotwell@gmail.com', $user3->email);
1074-
$this->assertSame('Abigail Otwell', $user3->name);
1074+
$this->assertSame('jane.doe@example.com', $user3->email);
1075+
$this->assertSame('Jane Doe', $user3->name);
10751076
$this->assertEquals(new DateTime('1987-05-28'), $user3->birthday);
10761077
$this->assertTrue($user3->wasRecentlyCreated);
10771078

10781079
$user4 = User::createOrFirst(
1079-
['name' => 'Dries Vints'],
1080-
['name' => 'Nuno Maduro', 'email' => 'nuno@laravel.com'],
1080+
['name' => 'Robert Doe'],
1081+
['name' => 'Maria Doe', 'email' => 'maria.doe@example.com'],
10811082
);
10821083

1083-
$this->assertSame('Nuno Maduro', $user4->name);
1084+
$this->assertSame('Maria Doe', $user4->name);
10841085
$this->assertTrue($user4->wasRecentlyCreated);
10851086
}
1087+
1088+
public function testCreateOrFirstRequiresFilter()
1089+
{
1090+
$this->expectException(InvalidArgumentException::class);
1091+
$this->expectExceptionMessage('You must provide attributes to check for duplicates');
1092+
User::createOrFirst([]);
1093+
}
10861094
}

tests/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function username(): Attribute
130130
{
131131
return Attribute::make(
132132
get: fn ($value) => $value,
133-
set: fn ($value) => Str::slug($value)
133+
set: fn ($value) => Str::slug($value),
134134
);
135135
}
136136

0 commit comments

Comments
 (0)