diff --git a/system/Commands/Database/Seed.php b/system/Commands/Database/Seed.php index 97b6f946310a..5b102dfb284e 100644 --- a/system/Commands/Database/Seed.php +++ b/system/Commands/Database/Seed.php @@ -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 '; - - /** - * the Command's Arguments - * - * @var array - */ - 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; } diff --git a/tests/system/Commands/DatabaseCommandsTest.php b/tests/system/Commands/DatabaseCommandsTest.php index a3d09dd9870d..577b6abb9604 100644 --- a/tests/system/Commands/DatabaseCommandsTest.php +++ b/tests/system/Commands/DatabaseCommandsTest.php @@ -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. @@ -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'); + } } diff --git a/utils/phpstan-baseline/empty.notAllowed.neon b/utils/phpstan-baseline/empty.notAllowed.neon index a0d6082b83a9..3bbd27098973 100644 --- a/utils/phpstan-baseline/empty.notAllowed.neon +++ b/utils/phpstan-baseline/empty.notAllowed.neon @@ -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 diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index f8abc8f9dc70..29a83b578073 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 1973 errors +# total 1972 errors includes: - argument.type.neon