Skip to content

Commit

Permalink
Merge pull request #437 from thephpleague/seed
Browse files Browse the repository at this point in the history
Support seeding without saving
  • Loading branch information
GrahamCampbell committed Feb 17, 2020
2 parents a27a71d + f97258c commit c864297
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/FactoryMuffin.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,18 @@ public function __construct(StoreInterface $store = null, GeneratorFactory $fact
* @param int $times The number of models to create.
* @param string $name The model definition name.
* @param array $attr The model attributes.
* @param bool $save Are we saving, or just creating an instance?
*
* @return object[]
*/
public function seed($times, $name, array $attr = [])
public function seed($times, $name, array $attr = [], $save = true)
{
$seeds = [];

$method = $save ? 'create' : 'instance';

for ($i = 0; $i < $times; $i++) {
$seeds[] = $this->create($name, $attr);
$seeds[] = $this->{$method}($name, $attr);
}

return $seeds;
Expand Down
39 changes: 38 additions & 1 deletion tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testGetDefinitions()
{
$definitions = static::$fm->getDefinitions();

$this->assertCount(39, $definitions);
$this->assertCount(40, $definitions);
}

public function testBasicDefinitionFunctions()
Expand Down Expand Up @@ -269,6 +269,30 @@ public function testSeed()
$this->assertNotEquals($users[0], $users[1]);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot save user model.
*/
public function testSeedWithSaving()
{
$users = static::$fm->seed(2, 'CrashingUserModelStub', [], true);

$this->assertCount(2, $users);
$this->assertInstanceOf('CrashingUserModelStub', $users[0]);
$this->assertInstanceOf('CrashingUserModelStub', $users[1]);
$this->assertNotEquals($users[0], $users[1]);
}

public function testSeedWithoutSaving()
{
$users = static::$fm->seed(2, 'CrashingUserModelStub', [], false);

$this->assertCount(2, $users);
$this->assertInstanceOf('CrashingUserModelStub', $users[0]);
$this->assertInstanceOf('CrashingUserModelStub', $users[1]);
$this->assertNotEquals($users[0], $users[1]);
}

public function testInstance()
{
$user = static::$fm->instance('UserModelStub');
Expand Down Expand Up @@ -382,6 +406,19 @@ public function delete()
}
}

class CrashingUserModelStub
{
public function save()
{
throw new RuntimeException('Cannot save user model.');
}

public function delete()
{
throw new RuntimeException('Cannot delete user model.');
}
}

class ProfileModelStub
{
public function save()
Expand Down
8 changes: 8 additions & 0 deletions tests/factories/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
$obj->test = 'foo';
});

$fm->define('CrashingUserModelStub')->setDefinitions([
'name' => Faker::word(),
'active' => Faker::boolean(),
'email' => Faker::email(),
'age' => Faker::numberBetween(18, 35),
'profile' => 'factory|ProfileModelStub',
]);

$fm->define('group:UserModelStub')->addDefinition('address', Faker::address());

$fm->define('anothergroup:UserModelStub')->setDefinitions([
Expand Down

0 comments on commit c864297

Please sign in to comment.