Skip to content

[12.x] Update the db:seed command to accept multiple seeders separated by commas #56466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
46 changes: 39 additions & 7 deletions src/Illuminate/Database/Console/Seeds/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Prohibitable;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -67,9 +68,9 @@ public function handle()

$this->resolver->setDefaultConnection($this->getDatabase());

Model::unguarded(function () {
$this->getSeeder()->__invoke();
});
foreach ($this->getSeeders() as $seeder) {
Model::unguarded(fn () => $seeder->__invoke());
}

if ($previousConnection) {
$this->resolver->setDefaultConnection($previousConnection);
Expand All @@ -79,14 +80,29 @@ public function handle()
}

/**
* Get a seeder instance from the container.
* Get a number of seeder instances from the container.
*
* @return \Illuminate\Database\Seeder
*/
protected function getSeeder()
protected function getSeeders()
{
$class = $this->input->getArgument('class') ?? $this->input->getOption('class');
$seeders = [];

foreach ($this->getClasses() as $class) {
$seeders[] = $this->getSeeder($class);
}

return $seeders;
}

/**
* Get a seeder instance from a class.
*
* @param string $class
* @return \Illuminate\Database\Seeder
*/
protected function getSeeder($class)
{
if (! str_contains($class, '\\')) {
$class = 'Database\\Seeders\\'.$class;
}
Expand All @@ -101,6 +117,22 @@ protected function getSeeder()
->setCommand($this);
}

/**
* Get seeder classes.
*
* @return array
*/
protected function getClasses()
{
$classes = $this->input->getArgument('class') ?? $this->input->getOption('class');

if (str_contains($classes, ',')) {
return explode(',', $classes);
}

return Arr::wrap($classes);
}

/**
* Get the name of the database connection to use.
*
Expand Down Expand Up @@ -133,7 +165,7 @@ protected function getArguments()
protected function getOptions()
{
return [
['class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', 'Database\\Seeders\\DatabaseSeeder'],
['class', null, InputOption::VALUE_OPTIONAL, 'The class names of a number of seeders', 'Database\\Seeders\\DatabaseSeeder'],
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed'],
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
];
Expand Down
63 changes: 63 additions & 0 deletions tests/Database/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,59 @@ public function testWithoutModelEvents()
$container->shouldHaveReceived('call')->with([$command, 'handle']);
}

public function testClassOptionWithMultipleSeeders()
{
$input = new ArrayInput([
'--force' => true,
'--database' => 'sqlite',
'--class' => UserWithoutModelEventsSeeder::class.','.AnotherUserWithoutModelEventsSeeder::class,
]);
$output = new NullOutput;
$outputStyle = new OutputStyle($input, $output);

$firstInstance = new UserWithoutModelEventsSeeder();

$firstSeeder = m::mock($firstInstance);
$firstSeeder->shouldReceive('setContainer')->once()->andReturnSelf();
$firstSeeder->shouldReceive('setCommand')->once()->andReturnSelf();

$secondInstance = new AnotherUserWithoutModelEventsSeeder();

$secondSeeder = m::mock($secondInstance);
$secondSeeder->shouldReceive('setContainer')->once()->andReturnSelf();
$secondSeeder->shouldReceive('setCommand')->once()->andReturnSelf();

$resolver = m::mock(ConnectionResolverInterface::class);
$resolver->shouldReceive('getDefaultConnection')->once();
$resolver->shouldReceive('setDefaultConnection')->once()->with('sqlite');

$container = m::mock(Container::class);
$container->shouldReceive('call');
$container->shouldReceive('environment')->once()->andReturn('testing');
$container->shouldReceive('runningUnitTests')->andReturn('true');
$container->shouldReceive('make')->with(UserWithoutModelEventsSeeder::class)->andReturn($firstSeeder);
$container->shouldReceive('make')->with(AnotherUserWithoutModelEventsSeeder::class)->andReturn($secondSeeder);
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
$outputStyle
);
$container->shouldReceive('make')->with(Factory::class, m::any())->andReturn(
new Factory($outputStyle)
);

$command = new SeedCommand($resolver);
$command->setLaravel($container);

Model::setEventDispatcher($dispatcher = m::mock(Dispatcher::class));

// call run to set up IO, then fire manually.
$command->run($input, $output);
$command->handle();

Assert::assertSame($dispatcher, Model::getEventDispatcher());

$container->shouldHaveReceived('call')->with([$command, 'handle']);
}

public function testProhibitable()
{
$input = new ArrayInput([]);
Expand Down Expand Up @@ -152,3 +205,13 @@ public function run()
Assert::assertInstanceOf(NullDispatcher::class, Model::getEventDispatcher());
}
}

class AnotherUserWithoutModelEventsSeeder extends Seeder
{
use WithoutModelEvents;

public function run()
{
Assert::assertInstanceOf(NullDispatcher::class, Model::getEventDispatcher());
}
}