Skip to content

Commit

Permalink
remove factory "types" (#30867)
Browse files Browse the repository at this point in the history
- simplify the global helper `factory()`. since the signature is no longer dynamic, we can be a little more explicit in our parameters
- remove `defineAs`, `createAs`, `makeAs`, and `rawOf` which were methods specific to factory "types"
- update the `FactoryBuilder` to remove references to the "name". we do keep one reference to the name "default" because that is how the callbacks are registered.
  • Loading branch information
browner12 authored and taylorotwell committed Dec 18, 2019
1 parent a4d6ec6 commit 67b814b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 85 deletions.
67 changes: 6 additions & 61 deletions src/Illuminate/Database/Eloquent/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,16 @@ public static function construct(Faker $faker, $pathToFactories = null)
return (new static($faker))->load($pathToFactories);
}

/**
* Define a class with a given short-name.
*
* @param string $class
* @param string $name
* @param callable $attributes
* @return $this
*/
public function defineAs($class, $name, callable $attributes)
{
return $this->define($class, $attributes, $name);
}

/**
* Define a class with a given set of attributes.
*
* @param string $class
* @param callable $attributes
* @param string $name
* @return $this
*/
public function define($class, callable $attributes, $name = 'default')
public function define($class, callable $attributes)
{
$this->definitions[$class][$name] = $attributes;
$this->definitions[$class] = $attributes;

return $this;
}
Expand Down Expand Up @@ -179,19 +165,6 @@ public function create($class, array $attributes = [])
return $this->of($class)->create($attributes);
}

/**
* Create an instance of the given model and type and persist it to the database.
*
* @param string $class
* @param string $name
* @param array $attributes
* @return mixed
*/
public function createAs($class, $name, array $attributes = [])
{
return $this->of($class, $name)->create($attributes);
}

/**
* Create an instance of the given model.
*
Expand All @@ -204,58 +177,30 @@ public function make($class, array $attributes = [])
return $this->of($class)->make($attributes);
}

/**
* Create an instance of the given model and type.
*
* @param string $class
* @param string $name
* @param array $attributes
* @return mixed
*/
public function makeAs($class, $name, array $attributes = [])
{
return $this->of($class, $name)->make($attributes);
}

/**
* Get the raw attribute array for a given named model.
*
* @param string $class
* @param string $name
* @param array $attributes
* @return array
*/
public function rawOf($class, $name, array $attributes = [])
{
return $this->raw($class, $attributes, $name);
}

/**
* Get the raw attribute array for a given model.
*
* @param string $class
* @param array $attributes
* @param string $name
* @return array
*/
public function raw($class, array $attributes = [], $name = 'default')
public function raw($class, array $attributes = [])
{
return array_merge(
call_user_func($this->definitions[$class][$name], $this->faker), $attributes
call_user_func($this->definitions[$class], $this->faker), $attributes
);
}

/**
* Create a builder for the given model.
*
* @param string $class
* @param string $name
* @return \Illuminate\Database\Eloquent\FactoryBuilder
*/
public function of($class, $name = 'default')
public function of($class)
{
return new FactoryBuilder(
$class, $name, $this->definitions, $this->states,
$class, $this->definitions, $this->states,
$this->afterMaking, $this->afterCreating, $this->faker
);
}
Expand Down
19 changes: 5 additions & 14 deletions src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ class FactoryBuilder
*/
protected $class;

/**
* The name of the model being built.
*
* @var string
*/
protected $name = 'default';

/**
* The database connection on which the model instance should be persisted.
*
Expand Down Expand Up @@ -84,18 +77,16 @@ class FactoryBuilder
* Create an new builder instance.
*
* @param string $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,
public function __construct($class, array $definitions, array $states,
array $afterMaking, array $afterCreating, Faker $faker)
{
$this->name = $name;
$this->class = $class;
$this->faker = $faker;
$this->states = $states;
Expand Down Expand Up @@ -265,12 +256,12 @@ public function raw(array $attributes = [])
*/
protected function getRawAttributes(array $attributes = [])
{
if (! isset($this->definitions[$this->class][$this->name])) {
throw new InvalidArgumentException("Unable to locate factory with name [{$this->name}] [{$this->class}].");
if (! isset($this->definitions[$this->class])) {
throw new InvalidArgumentException("Unable to locate factory for [{$this->class}].");
}

$definition = call_user_func(
$this->definitions[$this->class][$this->name],
$this->definitions[$this->class],
$this->faker, $attributes
);

Expand Down Expand Up @@ -403,7 +394,7 @@ public function callAfterCreating($models)
*/
protected function callAfter(array $afterCallbacks, $models)
{
$states = array_merge([$this->name], $this->activeStates);
$states = array_merge(['default'], $this->activeStates);

$models->each(function ($model) use ($states, $afterCallbacks) {
foreach ($states as $state) {
Expand Down
17 changes: 7 additions & 10 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,24 +484,21 @@ function event(...$args)

if (! function_exists('factory')) {
/**
* Create a model factory builder for a given class, name, and amount.
* Create a model factory builder for a given class and amount.
*
* @param dynamic class|class,name|class,amount|class,name,amount
* @param string $class
* @param int $amount
* @return \Illuminate\Database\Eloquent\FactoryBuilder
*/
function factory()
function factory($class, $amount = null)
{
$factory = app(EloquentFactory::class);

$arguments = func_get_args();

if (isset($arguments[1]) && is_string($arguments[1])) {
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ?? null);
} elseif (isset($arguments[1])) {
return $factory->of($arguments[0])->times($arguments[1]);
if (isset($amount) && is_int($amount)) {
return $factory->of($class)->times($amount);
}

return $factory->of($arguments[0]);
return $factory->of($class);
}
}

Expand Down

0 comments on commit 67b814b

Please sign in to comment.