Skip to content

Commit 5eadd2c

Browse files
authored
Fix model factory method arguments for Laravel >= 9 (#1361)
1 parent 6e26f22 commit 5eadd2c

File tree

5 files changed

+125
-4
lines changed

5 files changed

+125
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77

88
### Fixes
99
- Handle PHP 8.1 deprecation warnings when passing `null` to `new \ReflectionClass` [#1351 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1351)
10+
- Fix model factory method arguments for Laravel >= 9 [#1361 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1361)
1011

1112
2022-03-06, 2.12.3
1213
------------------

src/Console/ModelsCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,11 @@ protected function getFactoryMethods($model)
12501250
return;
12511251
}
12521252

1253-
$this->setMethod('factory', $factory, ['...$parameters']);
1253+
if (version_compare($this->laravel->version(), '9', '>=')) {
1254+
$this->setMethod('factory', $factory, ['$count = null, $state = []']);
1255+
} else {
1256+
$this->setMethod('factory', $factory, ['...$parameters']);
1257+
}
12541258
}
12551259

12561260
/**

tests/Console/ModelsCommand/Factories/Test.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,33 @@
1313

1414
class Test extends AbstractModelsCommand
1515
{
16-
public function test(): void
16+
public function test_8(): void
1717
{
18-
if (!version_compare(Application::VERSION, '8.2', '>=')) {
18+
if (!version_compare(Application::VERSION, '8.2', '>=') || !version_compare(Application::VERSION, '9', '<')) {
1919
$this->markTestSkipped(
20-
'This test only works in Laravel >= 8.2'
20+
'This test only works in Laravel >= 8.2 and < 9'
21+
);
22+
}
23+
24+
Factory::guessFactoryNamesUsing(static::getFactoryNameResolver());
25+
26+
$command = $this->app->make(ModelsCommand::class);
27+
28+
$tester = $this->runCommand($command, [
29+
'--write' => true,
30+
]);
31+
32+
$this->assertSame(0, $tester->getStatusCode());
33+
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
34+
$this->assertStringNotContainsString('not found', $tester->getDisplay());
35+
$this->assertMatchesMockedSnapshot();
36+
}
37+
38+
public function test_9(): void
39+
{
40+
if (!version_compare(Application::VERSION, '9', '>=')) {
41+
$this->markTestSkipped(
42+
'This test only works in Laravel >= 9'
2143
);
2244
}
2345

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
6+
7+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace\ModelWithCustomNamespaceFactory;
8+
use Illuminate\Database\Eloquent\Factories\HasFactory;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
/**
12+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithCustomNamespace
13+
*
14+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace\ModelWithCustomNamespaceFactory factory($count = null, $state = [])
15+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace newModelQuery()
16+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace newQuery()
17+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace query()
18+
* @mixin \Eloquent
19+
*/
20+
class ModelWithCustomNamespace extends Model
21+
{
22+
use HasFactory;
23+
24+
/**
25+
* Create a new factory instance for the model.
26+
*
27+
* @return \Illuminate\Database\Eloquent\Factories\Factory
28+
*/
29+
protected static function newFactory()
30+
{
31+
return ModelWithCustomNamespaceFactory::new();
32+
}
33+
}
34+
<?php
35+
36+
declare(strict_types=1);
37+
38+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
39+
40+
use Illuminate\Database\Eloquent\Factories\HasFactory;
41+
use Illuminate\Database\Eloquent\Model;
42+
43+
/**
44+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithFactory
45+
*
46+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\ModelWithFactoryFactory factory($count = null, $state = [])
47+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithFactory newModelQuery()
48+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithFactory newQuery()
49+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithFactory query()
50+
* @mixin \Eloquent
51+
*/
52+
class ModelWithFactory extends Model
53+
{
54+
use HasFactory;
55+
}
56+
<?php
57+
58+
declare(strict_types=1);
59+
60+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
61+
62+
/**
63+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithNestedFactory
64+
*
65+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\ModelWithNestedFactoryFactory factory($count = null, $state = [])
66+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory newModelQuery()
67+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory newQuery()
68+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithNestedFactory query()
69+
* @mixin \Eloquent
70+
*/
71+
class ModelWithNestedFactory extends ModelWithFactory
72+
{
73+
}
74+
<?php
75+
76+
declare(strict_types=1);
77+
78+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
79+
80+
use Illuminate\Database\Eloquent\Factories\HasFactory;
81+
use Illuminate\Database\Eloquent\Model;
82+
83+
/**
84+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithoutFactory
85+
*
86+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newModelQuery()
87+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newQuery()
88+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory query()
89+
* @mixin \Eloquent
90+
*/
91+
class ModelWithoutFactory extends Model
92+
{
93+
use HasFactory;
94+
}

0 commit comments

Comments
 (0)