Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Remove Factory "types" #30867

Merged
merged 1 commit into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int|null

* @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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be just if (! is_null(amount)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, i went back and forth on this. looks like it's merged already, so feel to submit a PR.

return $factory->of($class)->times($amount);
}

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

Expand Down