Skip to content

Commit b4ff269

Browse files
committed
refactor: migrate db:seed as a modern command
1 parent 874a820 commit b4ff269

4 files changed

Lines changed: 60 additions & 59 deletions

File tree

system/Commands/Database/Seed.php

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,51 @@
1313

1414
namespace CodeIgniter\Commands\Database;
1515

16-
use CodeIgniter\CLI\BaseCommand;
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
1718
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\CLI\Input\Argument;
1820
use CodeIgniter\Database\Seeder;
1921
use Config\Database;
2022
use Throwable;
2123

2224
/**
23-
* Runs the specified Seeder file to populate the database
24-
* with some data.
25+
* Runs the specified seeder to populate known data into the database.
2526
*/
26-
class Seed extends BaseCommand
27+
#[Command(
28+
name: 'db:seed',
29+
description: 'Runs the specified seeder to populate known data into the database.',
30+
group: 'Database',
31+
)]
32+
class Seed extends AbstractCommand
2733
{
28-
/**
29-
* The group the command is lumped under
30-
* when listing commands.
31-
*
32-
* @var string
33-
*/
34-
protected $group = 'Database';
35-
36-
/**
37-
* The Command's name
38-
*
39-
* @var string
40-
*/
41-
protected $name = 'db:seed';
42-
43-
/**
44-
* the Command's short description
45-
*
46-
* @var string
47-
*/
48-
protected $description = 'Runs the specified seeder to populate known data into the database.';
49-
50-
/**
51-
* the Command's usage
52-
*
53-
* @var string
54-
*/
55-
protected $usage = 'db:seed <seeder_name>';
56-
57-
/**
58-
* the Command's Arguments
59-
*
60-
* @var array<string, string>
61-
*/
62-
protected $arguments = [
63-
'seeder_name' => 'The seeder name to run',
64-
];
65-
66-
/**
67-
* Passes to Seeder to populate the database.
68-
*/
69-
public function run(array $params)
34+
protected function configure(): void
7035
{
71-
$seeder = new Seeder(new Database());
72-
$seedName = array_shift($params);
36+
$this->addArgument(new Argument(
37+
name: 'seeder_name',
38+
description: 'The seeder name to run.',
39+
required: true,
40+
));
41+
}
7342

74-
if (empty($seedName)) {
75-
$seedName = CLI::prompt(lang('Migrations.migSeeder'), null, 'required'); // @codeCoverageIgnore
43+
protected function interact(array &$arguments, array &$options): void
44+
{
45+
if ($arguments === []) {
46+
$arguments[] = CLI::prompt(lang('Migrations.migSeeder'), null, 'required');
7647
}
48+
}
49+
50+
protected function execute(array $arguments, array $options): int
51+
{
52+
$seedName = $arguments['seeder_name'];
53+
assert(is_string($seedName));
7754

7855
try {
79-
$seeder->call($seedName);
56+
(new Seeder(new Database()))->call($seedName);
8057

8158
return EXIT_SUCCESS;
8259
} catch (Throwable $e) {
83-
$this->showError($e);
60+
$this->renderThrowable($e);
8461

8562
return EXIT_ERROR;
8663
}

tests/system/Commands/DatabaseCommandsTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313

1414
namespace CodeIgniter\Commands;
1515

16+
use CodeIgniter\CLI\CLI;
17+
use CodeIgniter\CLI\Exceptions\ArgumentCountMismatchException;
1618
use CodeIgniter\Database\MigrationRunner;
1719
use CodeIgniter\EnvironmentDetector;
1820
use CodeIgniter\Test\CIUnitTestCase;
1921
use CodeIgniter\Test\DatabaseTestTrait;
22+
use CodeIgniter\Test\Mock\MockInputOutput;
2023
use CodeIgniter\Test\StreamFilterTrait;
2124
use Config\Services;
2225
use PHPUnit\Framework\Attributes\Group;
@@ -215,4 +218,30 @@ public function testSeed(): void
215218
command('db:seed Foobar.php');
216219
$this->assertStringContainsString('The specified seeder is not a valid file:', $this->getStreamFilterBuffer());
217220
}
221+
222+
public function testSeedPromptsForSeederNameWhenMissing(): void
223+
{
224+
command('migrate --all');
225+
$this->resetStreamFilterBuffer();
226+
227+
$io = new MockInputOutput();
228+
$io->setInputs(['Tests\\Support\\Database\\Seeds\\CITestSeeder']);
229+
CLI::setInputOutput($io);
230+
231+
command('db:seed');
232+
233+
CLI::resetInputOutput();
234+
235+
$output = $io->getOutput();
236+
$this->assertStringContainsString(lang('Migrations.migSeeder'), $output);
237+
$this->assertStringContainsString('Seeded', $output);
238+
}
239+
240+
public function testSeedAbortsWhenSeederNameMissingAndNonInteractive(): void
241+
{
242+
$this->expectException(ArgumentCountMismatchException::class);
243+
$this->expectExceptionMessage('Command "db:seed" is missing the following required argument: seeder_name.');
244+
245+
command('db:seed --no-interaction');
246+
}
218247
}

utils/phpstan-baseline/empty.notAllowed.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
# total 208 errors
1+
# total 207 errors
22

33
parameters:
44
ignoreErrors:
5-
-
6-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
7-
count: 1
8-
path: ../../system/Commands/Database/Seed.php
9-
105
-
116
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
127
count: 2

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1973 errors
1+
# total 1972 errors
22

33
includes:
44
- argument.type.neon

0 commit comments

Comments
 (0)