Skip to content

Commit d7ebd10

Browse files
committed
test: ensure firstOrCreate/New work in 6.x/8.x
1 parent 98a7bd5 commit d7ebd10

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

tests/acceptance/EloquentBuilderTypes.feature

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,62 @@ Feature: Eloquent Builder types
165165
"""
166166
When I run Psalm
167167
Then I see no errors
168+
169+
Scenario: cannot call firstOrNew and firstOrCreate without parameters in Laravel 6.x
170+
Given I have the "laravel/framework" package satisfying the "6.*"
171+
And I have the following code
172+
"""
173+
<?php declare(strict_types=1);
174+
175+
use Tests\Psalm\LaravelPlugin\Models\User;
176+
use \Illuminate\Database\Eloquent\Builder;
177+
178+
/**
179+
* @psalm-param Builder<User> $builder
180+
* @psalm-return User
181+
*/
182+
function test_firstOrCreate(Builder $builder): User {
183+
return $builder->firstOrCreate();
184+
}
185+
186+
/**
187+
* @psalm-param Builder<User> $builder
188+
* @psalm-return User
189+
*/
190+
function test_firstOrNew(Builder $builder): User {
191+
return $builder->firstOrNew();
192+
}
193+
"""
194+
When I run Psalm
195+
Then I see these errors
196+
| Type | Message |
197+
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstorcreate saw 0 |
198+
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstornew saw 0 |
199+
200+
Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 8.x
201+
Given I have the "laravel/framework" package satisfying the ">= 8.0"
202+
And I have the following code
203+
"""
204+
<?php declare(strict_types=1);
205+
206+
use Tests\Psalm\LaravelPlugin\Models\User;
207+
use Illuminate\Database\Eloquent\Builder;
208+
209+
/**
210+
* @psalm-param Builder<User> $builder
211+
* @psalm-return User
212+
*/
213+
function test_firstOrCreate(Builder $builder): User {
214+
return $builder->firstOrCreate();
215+
}
216+
217+
/**
218+
* @psalm-param Builder<User> $builder
219+
* @psalm-return User
220+
*/
221+
function test_firstOrNew(Builder $builder): User {
222+
return $builder->firstOrNew();
223+
}
224+
"""
225+
When I run Psalm
226+
Then I see no errors

tests/acceptance/EloquentRelationTypes.feature

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,56 @@ Feature: Eloquent Relation types
356356
"""
357357
When I run Psalm
358358
Then I see no errors
359+
360+
Scenario: cannot call firstOrNew and firstOrCreate without parameters in Laravel 6.x
361+
Given I have the "laravel/framework" package satisfying the "6.*"
362+
And I have the following code
363+
"""
364+
function test_hasOne_firstOrCreate(User $user): Phone {
365+
return $user->phone()->firstOrCreate();
366+
}
367+
368+
function test_hasOne_firstOrNew(User $user): Phone {
369+
return $user->phone()->firstOrNew();
370+
}
371+
372+
function test_hasMany_firstOrCreate(Post $post): Comment {
373+
return $post->comments()->firstOrCreate();
374+
}
375+
376+
function test_hasMany_firstOrNew(Post $post): Comment {
377+
return $post->comments()->firstOrNew();
378+
}
379+
"""
380+
When I run Psalm
381+
Then I see these errors
382+
| Type | Message |
383+
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstorcreate saw 0 |
384+
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstornew saw 0 |
385+
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstorcreate saw 0 |
386+
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstornew saw 0 |
387+
388+
389+
Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 8.x
390+
Given I have the "laravel/framework" package satisfying the ">= 8.0"
391+
And I have the following code
392+
"""
393+
function test_hasOne_firstOrCreate(User $user): Phone {
394+
return $user->phone()->firstOrCreate();
395+
}
396+
397+
function test_hasOne_firstOrNew(User $user): Phone {
398+
return $user->phone()->firstOrNew();
399+
}
400+
401+
function test_hasMany_firstOrCreate(Post $post): Comment {
402+
return $post->comments()->firstOrCreate();
403+
}
404+
405+
function test_hasMany_firstOrNew(Post $post): Comment {
406+
return $post->comments()->firstOrNew();
407+
}
408+
"""
409+
When I run Psalm
410+
Then I see no errors
411+

0 commit comments

Comments
 (0)