Merge pull request #2 from tm1000/laravel-9-test-fixes

Laravel 9 Test fixes
This commit is contained in:
Niels Vanpachtenbeke 2022-04-07 22:56:42 +02:00 committed by GitHub
commit e50f7f0574
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 126 additions and 128 deletions

View File

@ -9,7 +9,7 @@ jobs:
fail-fast: false
matrix:
php: [8.0, 8.1]
illuminate_version: [8.67.*, 9.*]
illuminate_version: [8.67.*, 9.7.*]
stability: [prefer-lowest, prefer-stable]
name: P${{ matrix.php }} | I ${{ matrix.illuminate_version }} | ${{ matrix.stability }}

View File

@ -21,9 +21,15 @@
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<file name="src/Fakes/FakeMetaCommand.php" />
<file name="src/Fakes/FakeModelsCommand.php" />
<file name="src/Fakes/FakeModelsCommand291.php" />
<file name="src/Fakes/FakeModelsCommand210.php" />
</errorLevel>
</PropertyNotSetInConstructor>
<OverriddenMethodAccess>
<errorLevel type="suppress">
<file name="src/Fakes/FakeModelsCommand291.php" />
</errorLevel>
</OverriddenMethodAccess>
</issueHandlers>
<stubs>

View File

@ -6,6 +6,9 @@ use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
class FakeMetaCommand extends MetaCommand
{
/**
* @return callable
*/
protected function registerClassAutoloadExceptions(): callable
{
// by default, the ide-helper throws exceptions when it cannot find a class. However it does not unregister that

View File

@ -0,0 +1,32 @@
<?php
namespace Psalm\LaravelPlugin\Fakes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
class FakeModelsCommand210 extends ModelsCommand
{
use FakeModelsCommandLogic;
/** @var SchemaAggregator */
private $schema;
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
/**
* Load the properties from the database table.
*
* @param Model $model
*/
public function getPropertiesFromTable($model): void
{
$this->getProperties($model);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Psalm\LaravelPlugin\Fakes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
class FakeModelsCommand291 extends ModelsCommand
{
use FakeModelsCommandLogic;
/** @var SchemaAggregator */
private $schema;
/** @var array<class-string> */
private $model_classes = [];
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
/**
* Load the properties from the database table.
*
* @param Model $model
*/
protected function getPropertiesFromTable($model): void
{
$this->getProperties($model);
}
}

View File

@ -2,33 +2,21 @@
namespace Psalm\LaravelPlugin\Fakes;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use function config;
use function get_class;
use function implode;
use function in_array;
use function implode;
class FakeModelsCommand extends ModelsCommand
trait FakeModelsCommandLogic
{
/** @var SchemaAggregator */
private $schema;
/** @var array<class-string> */
private $model_classes = [];
public function __construct(Filesystem $files, SchemaAggregator $schema)
{
parent::__construct($files);
$this->schema = $schema;
}
/** @return array<class-string> */
public function getModels()
public function getModels(): array
{
return $this->model_classes + $this->loadModels();
}

View File

@ -0,0 +1,37 @@
<?php
namespace Psalm\LaravelPlugin\Providers;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Composer\InstalledVersions;
use Illuminate\Filesystem\Filesystem;
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand210;
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand291;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use function version_compare;
use function assert;
use function is_string;
class FakeModelsCommandProvider
{
public static function getCommand(Filesystem $filesystem, SchemaAggregator $schemaAggregator): ModelsCommand
{
$ideHelperVersion = InstalledVersions::getVersion('barryvdh/laravel-ide-helper');
assert(is_string($ideHelperVersion));
// ide-helper released a breaking change in a non-major version. As a result, we need to monkey patch our code
if (version_compare($ideHelperVersion, '2.9.2', '<')) {
return new FakeModelsCommand291(
$filesystem,
$schemaAggregator
);
}
return new FakeModelsCommand210(
$filesystem,
$schemaAggregator
);
}
}

View File

@ -4,12 +4,10 @@ namespace Psalm\LaravelPlugin\Providers;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\LaravelPlugin\Fakes\FakeFilesystem;
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use function dirname;
use function glob;
use function unlink;
@ -37,7 +35,7 @@ final class ModelStubProvider implements GeneratesStubs
$fake_filesystem = new FakeFilesystem();
$models_generator_command = new FakeModelsCommand(
$models_generator_command = FakeModelsCommandProvider::getCommand(
$fake_filesystem,
$schema_aggregator
);

View File

@ -147,9 +147,9 @@ class Module extends BaseModule
/**
* @Then I see exit code :code
*/
public function seeExitCode(int $exitCode): void
public function seeExitCode(string $exitCode): void
{
if ($this->exitCode === $exitCode) {
if ($this->exitCode === (int) $exitCode) {
return;
}

View File

@ -177,32 +177,6 @@ Feature: Eloquent Builder types
When I run Psalm
Then I see no errors
Scenario: cannot call firstOrNew and firstOrCreate without parameters in Laravel 6.x
Given I have the "laravel/framework" package satisfying the "6.*"
And I have the following code
"""
/**
* @psalm-param Builder<User> $builder
* @psalm-return User
*/
function test_firstOrCreate(Builder $builder): User {
return $builder->firstOrCreate();
}
/**
* @psalm-param Builder<User> $builder
* @psalm-return User
*/
function test_firstOrNew(Builder $builder): User {
return $builder->firstOrNew();
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstorcreate saw 0 |
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Builder::firstornew saw 0 |
Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 8.x
Given I have the "laravel/framework" package satisfying the ">= 8.0"
And I have the following code

View File

@ -357,35 +357,6 @@ Feature: Eloquent Relation types
When I run Psalm
Then I see no errors
Scenario: cannot call firstOrNew and firstOrCreate without parameters in Laravel 6.x
Given I have the "laravel/framework" package satisfying the "6.*"
And I have the following code
"""
function test_hasOne_firstOrCreate(User $user): Phone {
return $user->phone()->firstOrCreate();
}
function test_hasOne_firstOrNew(User $user): Phone {
return $user->phone()->firstOrNew();
}
function test_hasMany_firstOrCreate(Post $post): Comment {
return $post->comments()->firstOrCreate();
}
function test_hasMany_firstOrNew(Post $post): Comment {
return $post->comments()->firstOrNew();
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstorcreate saw 0 |
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstornew saw 0 |
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstorcreate saw 0 |
| TooFewArguments | Too few arguments for method Illuminate\Database\Eloquent\Relations\HasOneOrMany::firstornew saw 0 |
Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 8.x
Given I have the "laravel/framework" package satisfying the ">= 8.0"
And I have the following code

View File

@ -24,57 +24,6 @@ Feature: factory()
use Tests\Psalm\LaravelPlugin\Models\User;
"""
Scenario: can use factory helper in Laravel 6.x and 7.x
Given I have the "laravel/framework" package satisfying the "6.*"
And I have the following code
"""
class FactoryTest {
/**
* @return FactoryBuilder<User, 1>
*/
public function getFactory(): FactoryBuilder
{
return factory(User::class);
}
/**
* @return FactoryBuilder<User, 2>
*/
public function getFactoryForTwo(): FactoryBuilder
{
return factory(User::class, 2);
}
public function makeUser(): User
{
return factory(User::class)->make();
}
public function createUser(): User
{
return factory(User::class)->create();
}
/**
* @return Collection<User>
*/
public function createUsers(): Collection
{
return factory(User::class, 2)->create();
}
/**
* @return Collection<User>
*/
public function createUsersWithNameAttribute(): Collection
{
return factory(User::class, 'new name', 2)->create();
}
}
"""
When I run Psalm
Then I see no errors
Scenario: cannot use factory helper in Laravel 8.x and later
Given I have the "laravel/framework" package satisfying the ">= 8.0"
And I have the following code

View File

@ -89,6 +89,11 @@
<code>ExampleListener</code>
</UnusedClass>
</file>
<file src="app/Models/Example.php">
<UnusedClass occurrences="1">
<code>Example</code>
</UnusedClass>
</file>
<file src="app/Models/User.php">
<NonInvariantDocblockPropertyType occurrences="3">
<code>$casts</code>