Skip to content

Commit

Permalink
Merge pull request #68 from mr-feek/factory
Browse files Browse the repository at this point in the history
Factory support
  • Loading branch information
mr-feek authored May 23, 2020
2 parents f646b57 + fd7e5c9 commit 6b91ff8
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Stubs/FactoryBuilder.stubphp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Illuminate\Database\Eloquent;

use Faker\Generator as Faker;
use InvalidArgumentException;
use Illuminate\Support\Traits\Macroable;

/**
* @template TModel of \Illuminate\Database\Eloquent\Model
* @template TCount of int
*/
class FactoryBuilder
{
use Macroable;

/**
* Create an new builder instance.
*
* @param class-string<TModel> $class
* @param string $name
* @param array $definitions
* @param array $states
* @param array $afterMaking
* @param array $afterCreating
* @param \Faker\Generator $faker
* @return void
*/
public function __construct($class, $name, array $definitions, array $states,
array $afterMaking, array $afterCreating, Faker $faker)
{

}

/**
* Create a collection of models and persist them to the database.
*
* @param array $attributes
* @return (TCount is 1 ? TModel : \Illuminate\Database\Eloquent\Collection<TModel>)
*/
public function create(array $attributes = [])
{

}

/**
* Create a collection of models.
*
* @param array $attributes
* @return (TCount is 1 ? TModel : \Illuminate\Database\Eloquent\Collection<TModel>)
*/
public function make(array $attributes = [])
{

}
}
26 changes: 26 additions & 0 deletions src/Stubs/helpers.stubphp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* @template TModel of \Illuminate\Database\Eloquent\Model
* @template TNameOrCountOrNull of string|int|null
* @template TCountOrNull of int|null
*
* @param class-string<TModel> $modelClassName
* @param TNameOrCountOrNull $nameOrCount
* @param TCountOrNull $count
*
* @return (
TCountOrNull is int
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TCountOrNull>
: (
TNameOrCountOrNull is int
? Illuminate\Database\Eloquent\FactoryBuilder<TModel, TNameOrCountOrNull>
: Illuminate\Database\Eloquent\FactoryBuilder<TModel, 1>
)

)
*/
function factory(string $modelClassName, $nameOrCount = null, $count = null)
{

}
73 changes: 73 additions & 0 deletions tests/acceptance/FactoryTypes.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
Feature: Factory Types
Model factories have type support

Background:
Given I have the following config
"""
<?xml version="1.0"?>
<psalm totallyTyped="true">
<projectFiles>
<directory name="."/>
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\LaravelPlugin\Plugin"/>
</plugins>
</psalm>
"""

Scenario:
Given I have the following code
"""
<?php declare(strict_types=1);
class User extends \Illuminate\Database\Eloquent\Model {
protected $table = 'users';
};
class FactoryTest {
/**
* @return \Illuminate\Database\Eloquent\FactoryBuilder<User, 1>
*/
public function getFactory(): \Illuminate\Database\Eloquent\FactoryBuilder
{
return factory(User::class);
}
/**
* @return \Illuminate\Database\Eloquent\FactoryBuilder<User, 2>
*/
public function getFactoryForTwo(): \Illuminate\Database\Eloquent\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 \Illuminate\Database\Eloquent\Collection<User>
*/
public function createUsers(): \Illuminate\Database\Eloquent\Collection
{
return factory(User::class, 2)->create();
}
/**
* @return \Illuminate\Database\Eloquent\Collection<User>
*/
public function createUsersWithNameAttribute(): \Illuminate\Database\Eloquent\Collection
{
return factory(User::class, 'new name', 2)->create();
}
}
"""
When I run Psalm
Then I see no errors

0 comments on commit 6b91ff8

Please sign in to comment.