Skip to content
Closed
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
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function call($class, $silent = false, array $parameters = [])
foreach ($classes as $class) {
$seeder = $this->resolve($class);

if (! $seeder->shouldRun()) {
continue;
}

$name = get_class($seeder);

if ($silent === false && isset($this->command)) {
Expand Down Expand Up @@ -140,6 +144,16 @@ protected function resolve($class)
return $instance;
}

/**
* Determine if this seeder should run.
*
* @return bool
*/
public function shouldRun(): bool
{
return true;
}

/**
* Set the IoC container instance.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseSeederTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ public function testCallResolveTheClassAndCallsRun()
$container->shouldReceive('make')->once()->with('ClassName')->andReturn($child = m::mock(Seeder::class));
$child->shouldReceive('setContainer')->once()->with($container)->andReturn($child);
$child->shouldReceive('setCommand')->once()->with($command)->andReturn($child);
$child->shouldReceive('shouldRun')->once()->andReturn(true);
$child->shouldReceive('__invoke')->once();

$seeder->call('ClassName');
}

public function testCallShouldntRun()
{
$seeder = new TestSeeder;
$seeder->setContainer($container = m::mock(Container::class));
$container->shouldReceive('make')->once()->with('ClassName')->andReturn($child = m::mock(Seeder::class));
$child->shouldReceive('setContainer')->once()->with($container)->andReturn($child);
$child->shouldReceive('shouldRun')->once()->andReturn(false);
$child->shouldNotReceive('__invoke');

$seeder->call('ClassName');
}

public function testSetContainer()
{
$seeder = new TestSeeder;
Expand Down