Tests / PHP 8.4 (push) Failing after 3m23s
A command line for the library, kept deliberately small: no console component, no dependency added to the consumer's tree. - Application/Command/Input/Output: parsing, dispatch, exit codes and capturable output. Commands return an exit code instead of calling exit(), so every one is testable. - seed: runs the registered SeederRunner inside one transaction, with --list and --only=name[,name]. - doctor: one round-trip that fails loudly when the DSN is wrong. - make:model and make:seeder: live scaffolding into the consumer's code, sharing name validation, the overwrite guard and directory creation through MakeCommand. The binary ships an empty runner; an application composes SeedCommand into its own Application to register its seeders.
160 lines
4.8 KiB
PHP
160 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit\Console;
|
|
|
|
use Camelia\Orm\Console\Application;
|
|
use Camelia\Orm\Console\Command\SeedCommand;
|
|
use Camelia\Orm\Console\Output;
|
|
use Camelia\Orm\Orm;
|
|
use Camelia\Orm\Seeding\AbstractSeeder;
|
|
use Camelia\Orm\Seeding\SeederRunner;
|
|
use Camelia\Orm\Tests\Support\FakeConnection;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class SeedCommandTest extends TestCase
|
|
{
|
|
private string $printed = '';
|
|
|
|
private function console(): Output
|
|
{
|
|
return new Output(function (string $text): void {
|
|
$this->printed .= $text;
|
|
});
|
|
}
|
|
|
|
private function orm(): Orm
|
|
{
|
|
return Orm::fromConnection(new FakeConnection());
|
|
}
|
|
|
|
private function command(): SeedCommand
|
|
{
|
|
return new SeedCommand(
|
|
$this->orm(...),
|
|
static fn(Orm $orm): SeederRunner => new SeederRunner($orm),
|
|
);
|
|
}
|
|
|
|
#[Test]
|
|
public function list_prints_registered_seeder_names(): void
|
|
{
|
|
$command = new SeedCommand(
|
|
$this->orm(...),
|
|
static fn(Orm $orm): SeederRunner => new SeederRunner($orm)->addMany([
|
|
new class extends AbstractSeeder {
|
|
public function name(): string
|
|
{
|
|
return 'permissions';
|
|
}
|
|
|
|
public function run(Orm $orm): void {}
|
|
},
|
|
new class extends AbstractSeeder {
|
|
public function name(): string
|
|
{
|
|
return 'roles';
|
|
}
|
|
|
|
public function run(Orm $orm): void {}
|
|
},
|
|
]),
|
|
);
|
|
|
|
$exit = $command->execute([], ['list' => true], $this->console());
|
|
|
|
self::assertSame(Application::SUCCESS, $exit);
|
|
self::assertStringContainsString('permissions', $this->printed);
|
|
self::assertStringContainsString('roles', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_reports_no_seeders_registered(): void
|
|
{
|
|
$exit = $this->command()->execute([], [], $this->console());
|
|
|
|
self::assertSame(Application::SUCCESS, $exit);
|
|
self::assertStringContainsString('No seeders registered.', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_runs_only_the_seeders_named_in_only(): void
|
|
{
|
|
$ran = new \ArrayObject();
|
|
|
|
$command = new SeedCommand($this->orm(...), static function (Orm $orm) use (
|
|
$ran,
|
|
): SeederRunner {
|
|
$a = new class($ran) extends AbstractSeeder {
|
|
/** @param \ArrayObject<int, string> $ran */
|
|
public function __construct(
|
|
private readonly \ArrayObject $ran,
|
|
) {}
|
|
|
|
public function name(): string
|
|
{
|
|
return 'permissions';
|
|
}
|
|
|
|
public function run(Orm $orm): void
|
|
{
|
|
$this->ran->append('permissions');
|
|
}
|
|
};
|
|
|
|
$b = new class($ran) extends AbstractSeeder {
|
|
/** @param \ArrayObject<int, string> $ran */
|
|
public function __construct(
|
|
private readonly \ArrayObject $ran,
|
|
) {}
|
|
|
|
public function name(): string
|
|
{
|
|
return 'roles';
|
|
}
|
|
|
|
public function run(Orm $orm): void
|
|
{
|
|
$this->ran->append('roles');
|
|
}
|
|
};
|
|
|
|
return new SeederRunner($orm)->addMany([$a, $b]);
|
|
});
|
|
|
|
$exit = $command->execute([], ['only' => 'roles'], $this->console());
|
|
|
|
self::assertSame(Application::SUCCESS, $exit);
|
|
self::assertSame(['roles'], $ran->getArrayCopy());
|
|
self::assertStringContainsString('roles: 0 inserted, 0 skipped', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_reports_a_seeder_failure_without_a_stack_trace(): void
|
|
{
|
|
$command = new SeedCommand(
|
|
$this->orm(...),
|
|
static fn(Orm $orm): SeederRunner => new SeederRunner($orm)->add(new class extends
|
|
AbstractSeeder {
|
|
public function name(): string
|
|
{
|
|
return 'boom';
|
|
}
|
|
|
|
public function run(Orm $orm): void
|
|
{
|
|
throw new \RuntimeException('table missing');
|
|
}
|
|
}),
|
|
);
|
|
|
|
$exit = $command->execute([], [], $this->console());
|
|
|
|
self::assertSame(Application::FAILURE, $exit);
|
|
self::assertStringContainsString('Seeding failed, nothing was written', $this->printed);
|
|
self::assertStringContainsString('table missing', $this->printed);
|
|
}
|
|
}
|