-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from mr-feek/factory
Factory support
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |