Skip to content

Commit

Permalink
Add alias for calling with single state (#24937)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightwatson authored and taylorotwell committed Jul 23, 2018
1 parent eddbd6e commit c8682e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ public function times($amount)
return $this;
}

/**
* Set the state to be applied to the model.
*
* @param string $state
* @return $this
*/
public function state($state)
{
return $this->states([$state]);
}

/**
* Set the states to be applied to the model.
*
Expand Down
20 changes: 10 additions & 10 deletions tests/Integration/Database/EloquentFactoryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function getEnvironmentSetUp($app)

$factory->afterMakingState(FactoryBuildableUser::class, 'with_callable_server', function (FactoryBuildableUser $user, Generator $faker) {
$server = factory(FactoryBuildableServer::class)
->states('callable')
->state('callable')
->make(['user_id' => $user->id]);

$user->servers->push($server);
Expand Down Expand Up @@ -91,7 +91,7 @@ protected function getEnvironmentSetUp($app)

$factory->afterCreatingState(FactoryBuildableUser::class, 'with_callable_server', function (FactoryBuildableUser $user, Generator $faker) {
$server = factory(FactoryBuildableServer::class)
->states('callable')
->state('callable')
->create(['user_id' => $user->id]);
});

Expand Down Expand Up @@ -186,11 +186,11 @@ public function creating_collection_of_models()
/**
* @test
*/
public function creating_models_with_callable_states()
public function creating_models_with_callable_state()
{
$server = factory(FactoryBuildableServer::class)->create();

$callableServer = factory(FactoryBuildableServer::class)->states('callable')->create();
$callableServer = factory(FactoryBuildableServer::class)->state('callable')->create();

$this->assertEquals('active', $server->status);
$this->assertEquals(['Storage', 'Data'], $server->tags);
Expand All @@ -200,11 +200,11 @@ public function creating_models_with_callable_states()
/**
* @test
*/
public function creating_models_with_inline_states()
public function creating_models_with_inline_state()
{
$server = factory(FactoryBuildableServer::class)->create();

$inlineServer = factory(FactoryBuildableServer::class)->states('inline')->create();
$inlineServer = factory(FactoryBuildableServer::class)->state('inline')->create();

$this->assertEquals('active', $server->status);
$this->assertEquals('inline', $inlineServer->status);
Expand Down Expand Up @@ -249,9 +249,9 @@ public function creating_models_with_after_callback()
}

/** @test **/
public function creating_models_with_after_callback_states()
public function creating_models_with_after_callback_state()
{
$user = factory(FactoryBuildableUser::class)->states('with_callable_server')->create();
$user = factory(FactoryBuildableUser::class)->state('with_callable_server')->create();

$this->assertNotNull($user->profile);
$this->assertNotNull($user->servers->where('status', 'callable')->first());
Expand All @@ -276,9 +276,9 @@ public function making_models_with_after_callback()
}

/** @test **/
public function making_models_with_after_callback_states()
public function making_models_with_after_callback_state()
{
$user = factory(FactoryBuildableUser::class)->states('with_callable_server')->make();
$user = factory(FactoryBuildableUser::class)->state('with_callable_server')->make();

$this->assertNotNull($user->profile);
$this->assertNotNull($user->servers->where('status', 'callable')->first());
Expand Down

0 comments on commit c8682e1

Please sign in to comment.