Skip to content

Commit

Permalink
Add typed factory method on model if it uses HasFactory trait
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Jul 15, 2020
1 parent 44f15b1 commit 7f28242
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Domain/Models/Output/ModelDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function docblock(): string
$this->all(),
$this->query(),
$this->queryMixin(),
$this->factory(),
' */',
])
->map(fn (?string $line): string => $this->line($line))
Expand Down Expand Up @@ -102,4 +103,21 @@ private function queryMixin(): ?string

return " * @mixin {$this->model->queryBuilder->fqcn}";
}

private function factory(): ?string
{
if (!trait_exists(\Illuminate\Database\Eloquent\Factories\HasFactory::class)) {
return null;
}

if (!in_array(\Illuminate\Database\Eloquent\Factories\HasFactory::class, class_uses_recursive($this->model->fqcn))) {
return null;
}

$factory = get_class(
($this->model->fqcn)::factory()
);

return " * @method static \\{$factory} factory(\$count = 1, \$state = [])";
}
}
9 changes: 9 additions & 0 deletions tests/Feature/ModelsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Soyhuce\NextIdeHelper\Tests\Feature;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\File;
use Soyhuce\NextIdeHelper\Tests\ResetsFixtures;
use Soyhuce\NextIdeHelper\Tests\TestCase;
Expand All @@ -13,6 +14,14 @@ class ModelsCommandTest extends TestCase
{
use ResetsFixtures;

protected function setUp(): void
{
parent::setUp();
Factory::guessFactoryNamesUsing(function (string $modelFqcn) {
return 'Soyhuce\NextIdeHelper\Tests\Fixtures\Factories\\' . class_basename($modelFqcn) . 'Factory';
});
}

/**
* @test
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Soyhuce\NextIdeHelper\Tests\Fixtures;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post;
Expand All @@ -19,6 +20,8 @@
*/
class User extends Model
{
use HasFactory;

protected $casts = [
'address' => AddressCaster::class,
];
Expand Down
4 changes: 4 additions & 0 deletions tests/expected/User.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Soyhuce\NextIdeHelper\Tests\Fixtures;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post;
Expand All @@ -16,9 +17,12 @@ use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post;
* @property \Illuminate\Support\Carbon $updated_at
* @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $laravelPosts
* @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $posts
* @method static \Soyhuce\NextIdeHelper\Tests\Fixtures\Factories\UserFactory factory($count = 1, $state = [])
*/
class User extends Model
{
use HasFactory;

protected $casts = [
'address' => AddressCaster::class,
];
Expand Down

0 comments on commit 7f28242

Please sign in to comment.