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; } }; } }