Skip to content
Open
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
81 changes: 29 additions & 52 deletions system/Commands/Database/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,51 @@

namespace CodeIgniter\Commands\Database;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Argument;
use CodeIgniter\Database\Seeder;
use Config\Database;
use Throwable;

/**
* Runs the specified Seeder file to populate the database
* with some data.
* Runs the specified seeder to populate known data into the database.
*/
class Seed extends BaseCommand
#[Command(
name: 'db:seed',
description: 'Runs the specified seeder to populate known data into the database.',
group: 'Database',
)]
class Seed extends AbstractCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group = 'Database';

/**
* The Command's name
*
* @var string
*/
protected $name = 'db:seed';

/**
* the Command's short description
*
* @var string
*/
protected $description = 'Runs the specified seeder to populate known data into the database.';

/**
* the Command's usage
*
* @var string
*/
protected $usage = 'db:seed <seeder_name>';

/**
* the Command's Arguments
*
* @var array<string, string>
*/
protected $arguments = [
'seeder_name' => 'The seeder name to run',
];

/**
* Passes to Seeder to populate the database.
*/
public function run(array $params)
protected function configure(): void
{
$seeder = new Seeder(new Database());
$seedName = array_shift($params);
$this->addArgument(new Argument(
name: 'seeder_name',
description: 'The seeder name to run.',
required: true,
));
}

if (empty($seedName)) {
$seedName = CLI::prompt(lang('Migrations.migSeeder'), null, 'required'); // @codeCoverageIgnore
protected function interact(array &$arguments, array &$options): void
{
if ($arguments === []) {
$arguments[] = CLI::prompt(lang('Migrations.migSeeder'), null, 'required');
}
}

protected function execute(array $arguments, array $options): int
{
$seedName = $arguments['seeder_name'];
assert(is_string($seedName));

try {
$seeder->call($seedName);
(new Seeder(new Database()))->call($seedName);

return EXIT_SUCCESS;
} catch (Throwable $e) {
$this->showError($e);
$this->renderThrowable($e);

return EXIT_ERROR;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/system/Commands/DatabaseCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@

namespace CodeIgniter\Commands;

use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Exceptions\ArgumentCountMismatchException;
use CodeIgniter\Database\MigrationRunner;
use CodeIgniter\EnvironmentDetector;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\Mock\MockInputOutput;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Services;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Database\Seeds\CITestSeeder;

/**
* @todo To figure out how to transfer this test to `tests/system/Commands/Database/` without breaking DatabaseLive group.
Expand Down Expand Up @@ -215,4 +219,30 @@ public function testSeed(): void
command('db:seed Foobar.php');
$this->assertStringContainsString('The specified seeder is not a valid file:', $this->getStreamFilterBuffer());
}

public function testSeedPromptsForSeederNameWhenMissing(): void
{
command('migrate --all');
$this->resetStreamFilterBuffer();

$io = new MockInputOutput();
$io->setInputs([CITestSeeder::class]);
CLI::setInputOutput($io);

command('db:seed');

CLI::resetInputOutput();

$output = $io->getOutput();
$this->assertStringContainsString(lang('Migrations.migSeeder'), $output);
$this->assertStringContainsString('Seeded', $output);
}

public function testSeedAbortsWhenSeederNameMissingAndNonInteractive(): void
{
$this->expectException(ArgumentCountMismatchException::class);
$this->expectExceptionMessage('Command "db:seed" is missing the following required argument: seeder_name.');

command('db:seed --no-interaction');
}
}
7 changes: 1 addition & 6 deletions utils/phpstan-baseline/empty.notAllowed.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# total 208 errors
# total 207 errors

parameters:
ignoreErrors:
-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
count: 1
path: ../../system/Commands/Database/Seed.php

-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
count: 2
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 1973 errors
# total 1972 errors

includes:
- argument.type.neon
Expand Down