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.
172 lines
5.3 KiB
PHP
172 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Tests\Unit\Console;
|
|
|
|
use Camelia\Orm\Console\Application;
|
|
use Camelia\Orm\Console\Command;
|
|
use Camelia\Orm\Console\Input;
|
|
use Camelia\Orm\Console\Output;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class ApplicationTest extends TestCase
|
|
{
|
|
private string $printed = '';
|
|
|
|
private function console(): Output
|
|
{
|
|
return new Output(function (string $text): void {
|
|
$this->printed .= $text;
|
|
});
|
|
}
|
|
|
|
#[Test]
|
|
public function it_lists_registered_commands_with_their_description(): void
|
|
{
|
|
$application = new Application()->add($this->command('seed', 'Runs the seeders.'));
|
|
|
|
$exit = $application->run([], $this->console());
|
|
|
|
self::assertSame(Application::SUCCESS, $exit);
|
|
self::assertStringContainsString('seed', $this->printed);
|
|
self::assertStringContainsString('Runs the seeders.', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_dispatches_to_the_named_command(): void
|
|
{
|
|
$application = new Application()->add($this->command('seed', 'desc'));
|
|
|
|
$exit = $application->run(['bin', 'seed'], $this->console());
|
|
|
|
self::assertSame(Application::SUCCESS, $exit);
|
|
self::assertStringContainsString('executed:seed', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_passes_positional_arguments(): void
|
|
{
|
|
$application = new Application()->add($this->command('make:model', 'desc'));
|
|
|
|
$application->run(['bin', 'make:model', 'Invoice'], $this->console());
|
|
|
|
self::assertStringContainsString('args:Invoice', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_passes_long_options_with_and_without_values(): void
|
|
{
|
|
$application = new Application()->add($this->command('seed', 'desc'));
|
|
|
|
$application->run(['bin', 'seed', '--only=roles', '--list'], $this->console());
|
|
|
|
self::assertStringContainsString('only=roles', $this->printed);
|
|
self::assertStringContainsString('list=1', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function an_unknown_command_fails_with_a_non_zero_code(): void
|
|
{
|
|
$application = new Application()->add($this->command('seed', 'desc'));
|
|
|
|
$exit = $application->run(['bin', 'nope'], $this->console());
|
|
|
|
self::assertSame(Application::INVALID_USAGE, $exit);
|
|
self::assertStringContainsString('Unknown command "nope".', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function help_prints_the_usage_of_one_command(): void
|
|
{
|
|
$application = new Application()->add($this->command('seed', 'desc'));
|
|
|
|
$application->run(['bin', 'help', 'seed'], $this->console());
|
|
|
|
self::assertStringContainsString('Usage: seed [--x]', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function dash_dash_help_prints_the_usage_instead_of_running(): void
|
|
{
|
|
$application = new Application()->add($this->command('seed', 'desc'));
|
|
|
|
$application->run(['bin', 'seed', '--help'], $this->console());
|
|
|
|
self::assertStringContainsString('Usage: seed [--x]', $this->printed);
|
|
self::assertStringNotContainsString('executed:seed', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function dash_dash_version_prints_the_version(): void
|
|
{
|
|
$application = new Application('camelia-orm', '9.9.9');
|
|
|
|
$application->run(['bin', '--version'], $this->console());
|
|
|
|
self::assertStringContainsString('camelia-orm 9.9.9', $this->printed);
|
|
}
|
|
|
|
#[Test]
|
|
public function input_splits_options_from_arguments(): void
|
|
{
|
|
$input = Input::fromArgv(['bin', 'seed', 'extra', '--only=a,b', '--list']);
|
|
|
|
self::assertSame('seed', $input->command);
|
|
self::assertSame(['extra'], $input->arguments);
|
|
self::assertSame('a,b', $input->option('only'));
|
|
self::assertTrue($input->hasOption('list'));
|
|
self::assertSame('', $input->option('list'));
|
|
}
|
|
|
|
#[Test]
|
|
public function input_splits_a_comma_separated_list_option(): void
|
|
{
|
|
$input = Input::fromArgv(['bin', 'seed', '--only=roles, admin-user ,']);
|
|
|
|
self::assertSame(['roles', 'admin-user'], $input->listOption('only'));
|
|
self::assertSame([], $input->listOption('missing'));
|
|
}
|
|
|
|
private function command(string $name, string $description): Command
|
|
{
|
|
return new class($name, $description) implements Command {
|
|
public function __construct(
|
|
private readonly string $label,
|
|
private readonly string $text,
|
|
) {}
|
|
|
|
public function name(): string
|
|
{
|
|
return $this->label;
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return $this->text;
|
|
}
|
|
|
|
public function usage(): string
|
|
{
|
|
return 'Usage: ' . $this->label . ' [--x]';
|
|
}
|
|
|
|
public function execute(array $arguments, array $options, Output $output): int
|
|
{
|
|
$output->writeln('executed:' . $this->label);
|
|
|
|
if ($arguments !== []) {
|
|
$output->writeln('args:' . implode(',', $arguments));
|
|
}
|
|
|
|
foreach ($options as $key => $value) {
|
|
$output->writeln($key . '=' . ($value === true ? '1' : $value));
|
|
}
|
|
|
|
return Application::SUCCESS;
|
|
}
|
|
};
|
|
}
|
|
}
|