test: extract common Builder preamble

This commit is contained in:
Claas Augner 2021-07-09 21:53:36 +02:00
parent d7ebd103d5
commit 02f4b50c8a

View File

@ -15,73 +15,78 @@ Feature: Eloquent Builder types
</plugins> </plugins>
</psalm> </psalm>
""" """
And I have the following code preamble
"""
<?php declare(strict_types=1);
namespace Tests\Psalm\LaravelPlugin\Sandbox;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Tests\Psalm\LaravelPlugin\Models\User;
"""
Scenario: Models can call eloquent query builder instance methods Scenario: Models can call eloquent query builder instance methods
Given I have the following code Given I have the following code
""" """
<?php declare(strict_types=1);
use Tests\Psalm\LaravelPlugin\Models\User;
final class UserRepository final class UserRepository
{ {
/** /**
* @return \Illuminate\Database\Eloquent\Builder<User> * @return Builder<User>
*/ */
public function getNewQuery(): \Illuminate\Database\Eloquent\Builder public function getNewQuery(): Builder
{ {
return User::query(); return User::query();
} }
/** /**
* @return \Illuminate\Database\Eloquent\Builder<User> * @return Builder<User>
*/ */
public function getNewModelQuery(): \Illuminate\Database\Eloquent\Builder public function getNewModelQuery(): Builder
{ {
return (new User())->newModelQuery(); return (new User())->newModelQuery();
} }
/** /**
* @param \Illuminate\Database\Eloquent\Builder<User> $builder * @param Builder<User> $builder
*/ */
public function firstOrFailFromBuilderInstance(\Illuminate\Database\Eloquent\Builder $builder): User { public function firstOrFailFromBuilderInstance(Builder $builder): User {
return $builder->firstOrFail(); return $builder->firstOrFail();
} }
/** /**
* @param \Illuminate\Database\Eloquent\Builder<User> $builder * @param Builder<User> $builder
*/ */
public function findOrFailFromBuilderInstance(\Illuminate\Database\Eloquent\Builder $builder): User { public function findOrFailFromBuilderInstance(Builder $builder): User {
return $builder->findOrFail(1); return $builder->findOrFail(1);
} }
/** /**
* @param \Illuminate\Database\Eloquent\Builder<User> $builder * @param Builder<User> $builder
* @return \Illuminate\Database\Eloquent\Collection<User> * @return Collection<User>
*/ */
public function findMultipleOrFailFromBuilderInstance(\Illuminate\Database\Eloquent\Builder $builder): \Illuminate\Database\Eloquent\Collection { public function findMultipleOrFailFromBuilderInstance(Builder $builder): Collection {
return $builder->findOrFail([1, 2]); return $builder->findOrFail([1, 2]);
} }
/** /**
* @param \Illuminate\Database\Eloquent\Builder<User> $builder * @param Builder<User> $builder
*/ */
public function findOne(\Illuminate\Database\Eloquent\Builder $builder): ?User { public function findOne(Builder $builder): ?User {
return $builder->find(1); return $builder->find(1);
} }
/** /**
* @param \Illuminate\Database\Eloquent\Builder<User> $builder * @param Builder<User> $builder
*/ */
public function findViaArray(\Illuminate\Database\Eloquent\Builder $builder): \Illuminate\Database\Eloquent\Collection { public function findViaArray(Builder $builder): Collection {
return $builder->find([1]); return $builder->find([1]);
} }
/** /**
* @return \Illuminate\Database\Eloquent\Builder<User> * @return Builder<User>
*/ */
public function getWhereBuilderViaInstance(array $attributes): \Illuminate\Database\Eloquent\Builder { public function getWhereBuilderViaInstance(array $attributes): Builder {
return (new User())->where($attributes); return (new User())->where($attributes);
} }
} }
@ -90,10 +95,16 @@ Feature: Eloquent Builder types
Then I see no errors Then I see no errors
Scenario: can call static methods on model Scenario: can call static methods on model
Given I have the following code Given I have the following code preamble
""" """
<?php declare(strict_types=1); <?php declare(strict_types=1);
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
"""
And I have the following code
"""
final class User extends \Illuminate\Database\Eloquent\Model { final class User extends \Illuminate\Database\Eloquent\Model {
protected $table = 'users'; protected $table = 'users';
}; };
@ -102,17 +113,17 @@ Feature: Eloquent Builder types
{ {
/** /**
* @return \Illuminate\Database\Eloquent\Builder<User> * @return Builder<User>
*/ */
public function getWhereBuilderViaStatic(array $attributes): \Illuminate\Database\Eloquent\Builder public function getWhereBuilderViaStatic(array $attributes): Builder
{ {
return User::where($attributes); return User::where($attributes);
} }
/** /**
* @psalm-return \Illuminate\Database\Eloquent\Collection<User> * @psalm-return Collection<User>
*/ */
public function getWhereViaStatic(array $attributes): \Illuminate\Database\Eloquent\Collection public function getWhereViaStatic(array $attributes): Collection
{ {
return User::where($attributes)->get(); return User::where($attributes)->get();
} }
@ -122,20 +133,25 @@ Feature: Eloquent Builder types
Then I see no errors Then I see no errors
Scenario: Scenario:
Given I have the following code Given I have the following code preamble
""" """
<?php declare(strict_types=1); <?php declare(strict_types=1);
final class User extends \Illuminate\Database\Eloquent\Model { use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
"""
And I have the following code
"""
final class User extends Model {
protected $table = 'users'; protected $table = 'users';
}; };
final class UserRepository final class UserRepository
{ {
/** /**
* @return \Illuminate\Database\Eloquent\Builder<User> * @return Builder<User>
*/ */
public function test_failure(): \Illuminate\Database\Eloquent\Builder public function test_failure(): Builder
{ {
return User::fakeQueryMethodThatDoesntExist(); return User::fakeQueryMethodThatDoesntExist();
} }
@ -150,11 +166,6 @@ Feature: Eloquent Builder types
Scenario: can call methods on underlying query builder Scenario: can call methods on underlying query builder
Given I have the following code Given I have the following code
""" """
<?php declare(strict_types=1);
use Tests\Psalm\LaravelPlugin\Models\User;
use \Illuminate\Database\Eloquent\Builder;
/** /**
* @psalm-param Builder<User> $builder * @psalm-param Builder<User> $builder
* @psalm-return Builder<User> * @psalm-return Builder<User>
@ -170,11 +181,6 @@ Feature: Eloquent Builder types
Given I have the "laravel/framework" package satisfying the "6.*" Given I have the "laravel/framework" package satisfying the "6.*"
And I have the following code And I have the following code
""" """
<?php declare(strict_types=1);
use Tests\Psalm\LaravelPlugin\Models\User;
use \Illuminate\Database\Eloquent\Builder;
/** /**
* @psalm-param Builder<User> $builder * @psalm-param Builder<User> $builder
* @psalm-return User * @psalm-return User
@ -201,11 +207,6 @@ Feature: Eloquent Builder types
Given I have the "laravel/framework" package satisfying the ">= 8.0" Given I have the "laravel/framework" package satisfying the ">= 8.0"
And I have the following code And I have the following code
""" """
<?php declare(strict_types=1);
use Tests\Psalm\LaravelPlugin\Models\User;
use Illuminate\Database\Eloquent\Builder;
/** /**
* @psalm-param Builder<User> $builder * @psalm-param Builder<User> $builder
* @psalm-return User * @psalm-return User