Skip to content

Commit

Permalink
Add raw() method to factory
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightwatson committed Sep 11, 2020
1 parent 2cf5739 commit 66ee018
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Expand Up @@ -282,6 +282,17 @@ public function make($attributes = [], ?Model $parent = null)
return $instances;
}

/**
* Get the raw attributes generated from the factory.
*
* @param array $attributes
* @return array
*/
public function raw($attributes = [])
{
return $this->state($attributes)->getExpandedAttributes(null);
}

/**
* Make an instance of the model with the given attributes.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Expand Up @@ -125,6 +125,27 @@ public function test_make_creates_unpersisted_model_instance()
$this->assertCount(0, FactoryTestUser::all());
}

public function test_basic_model_attributes_can_be_created()
{
$user = FactoryTestUserFactory::new()->raw();
$this->assertIsArray($user);

$user = FactoryTestUserFactory::new()->raw(['name' => 'Taylor Otwell']);
$this->assertIsArray($user);
$this->assertEquals('Taylor Otwell', $user['name']);
}

public function test_expanded_model_attributes_can_be_created()
{
$post = FactoryTestPostFactory::new()->raw();
$this->assertIsArray($post);

$post = FactoryTestPostFactory::new()->raw(['title' => 'Test Title']);
$this->assertIsArray($post);
$this->assertIsInt($post['user_id']);
$this->assertEquals('Test Title', $post['title']);
}

public function test_after_creating_and_making_callbacks_are_called()
{
$user = FactoryTestUserFactory::new()
Expand Down

0 comments on commit 66ee018

Please sign in to comment.