Skip to content

Commit

Permalink
Change __call factory and relation resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Jul 6, 2020
1 parent 6209a06 commit 223293a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,18 @@ public function __call($method, $parameters)
static::throwBadMethodCallException($method);
}

$factory = static::factoryForModel(Str::singular(Str::substr($method, 3)));
$relationship = Str::camel(Str::substr($method, 3));
$related = $this->newModel()->{$relationship}()->getRelated();
$factory = static::factoryForModel(get_class($related));

if (Str::startsWith($method, 'for')) {
return $this->for($factory->state($parameters[0] ?? []));
return $this->for($factory->state($parameters[0] ?? []), $relationship);
} elseif (Str::startsWith($method, 'has')) {
return $this->has(
$factory
->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : 1)
->state(is_array($parameters[0] ?? null) ? $parameters[0] : ($parameters[1] ?? []))
->state(is_array($parameters[0] ?? null) ? $parameters[0] : ($parameters[1] ?? [])),
$relationship
);
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Eloquent\Model as Eloquent;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -242,6 +243,33 @@ public function test_sequences()
}));
}

public function test_model_has_factory()
{
Factory::guessFactoryNamesUsing(function ($model) {
return $model.'Factory';
});

$this->assertInstanceOf(FactoryTestUserFactory::class, FactoryTestUser::factory());
}

public function test_call()
{
Factory::guessFactoryNamesUsing(function ($model) {
return $model.'Factory';
});

$user = FactoryTestUserFactory::new()->hasPosts(3)->create();
$this->assertCount(3, $user->posts);

$post = FactoryTestPostFactory::new()
->forAuthor(['name' => 'Taylor Otwell'])
->hasComments(2)
->create();
$this->assertInstanceOf(FactoryTestUser::class, $post->author);
$this->assertEquals('Taylor Otwell', $post->author->name);
$this->assertCount(2, $post->comments);
}

/**
* Get a database connection instance.
*
Expand Down Expand Up @@ -277,6 +305,8 @@ public function definition()

class FactoryTestUser extends Eloquent
{
use HasFactory;

protected $table = 'users';

public function posts()
Expand Down Expand Up @@ -312,6 +342,11 @@ public function user()
return $this->belongsTo(FactoryTestUser::class, 'user_id');
}

public function author()
{
return $this->belongsTo(FactoryTestUser::class, 'user_id');
}

public function comments()
{
return $this->morphMany(FactoryTestComment::class, 'commentable');
Expand Down

0 comments on commit 223293a

Please sign in to comment.