Files
dgarcia a655a6f3ae
Tests / PHP 8.4 (push) Failing after 3m23s
feat(console): add bin/camelia-orm with seed and make commands
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.
2026-11-15 13:31:14 -03:00

219 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Camelia\Orm\Tests\Unit\Console;
use Camelia\Orm\Console\Application;
use Camelia\Orm\Console\Command\MakeModelCommand;
use Camelia\Orm\Console\Command\MakeSeederCommand;
use Camelia\Orm\Console\Output;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class MakeCommandsTest extends TestCase
{
private string $directory = '';
private string $printed = '';
protected function setUp(): void
{
$this->directory =
sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'camelia_' . bin2hex(random_bytes(6));
}
protected function tearDown(): void
{
if ($this->directory === '' || !is_dir($this->directory)) {
return;
}
$files = glob($this->directory . DIRECTORY_SEPARATOR . '*');
foreach ($files === false ? [] : $files as $file) {
if (is_file($file)) {
unlink($file);
}
}
rmdir($this->directory);
}
private function console(): Output
{
return new Output(function (string $text): void {
$this->printed .= $text;
});
}
#[Test]
public function make_model_writes_a_model_with_the_orm_attributes(): void
{
$exit = new MakeModelCommand()->execute(
['Invoice'],
['table' => 'invoices', 'dir' => $this->directory, 'namespace' => 'App\\Models'],
$this->console(),
);
self::assertSame(Application::SUCCESS, $exit);
$contents = (string) file_get_contents(
$this->directory . DIRECTORY_SEPARATOR . 'Invoice.php',
);
self::assertStringContainsString('namespace App\\Models;', $contents);
self::assertStringContainsString("#[TableName('invoices')]", $contents);
self::assertStringContainsString('final class Invoice extends Model', $contents);
self::assertStringContainsString('#[PrimaryKey, AutoIncrement]', $contents);
}
#[Test]
public function make_model_generates_valid_php(): void
{
new MakeModelCommand()->execute(
['Invoice'],
['table' => 'invoices', 'dir' => $this->directory],
$this->console(),
);
self::assertSame(
'No syntax errors detected in '
. str_replace(
['\\', '/'],
'/',
$this->directory . DIRECTORY_SEPARATOR . 'Invoice.php',
),
$this->lint('Invoice.php'),
);
}
#[Test]
public function make_model_requires_a_table(): void
{
$exit = new MakeModelCommand()->execute(['Invoice'], [], $this->console());
self::assertSame(Application::INVALID_USAGE, $exit);
self::assertStringContainsString('--table=<table>', $this->printed);
}
#[Test]
public function make_model_requires_a_pascal_case_name(): void
{
$exit = new MakeModelCommand()->execute(
['invoice'],
['table' => 'invoices', 'dir' => $this->directory],
$this->console(),
);
self::assertSame(Application::INVALID_USAGE, $exit);
self::assertStringContainsString('PascalCase', $this->printed);
}
#[Test]
public function make_model_refuses_to_overwrite_without_force(): void
{
new MakeModelCommand()->execute(
['Invoice'],
['table' => 'invoices', 'dir' => $this->directory],
$this->console(),
);
$exit = new MakeModelCommand()->execute(
['Invoice'],
['table' => 'other', 'dir' => $this->directory],
$this->console(),
);
self::assertSame(Application::FAILURE, $exit);
self::assertStringContainsString(
"#[TableName('invoices')]",
(string) file_get_contents($this->directory . DIRECTORY_SEPARATOR . 'Invoice.php'),
);
}
#[Test]
public function make_model_overwrites_when_forced(): void
{
new MakeModelCommand()->execute(
['Invoice'],
['table' => 'invoices', 'dir' => $this->directory],
$this->console(),
);
new MakeModelCommand()->execute(
['Invoice'],
['table' => 'other', 'dir' => $this->directory, 'force' => true],
$this->console(),
);
self::assertStringContainsString(
"#[TableName('other')]",
(string) file_get_contents($this->directory . DIRECTORY_SEPARATOR . 'Invoice.php'),
);
}
#[Test]
public function make_seeder_writes_a_seeder_extending_abstract_seeder(): void
{
$exit = new MakeSeederCommand()->execute(
['RolesSeeder'],
['dir' => $this->directory, 'namespace' => 'App\\Seeders'],
$this->console(),
);
self::assertSame(Application::SUCCESS, $exit);
$contents = (string) file_get_contents(
$this->directory . DIRECTORY_SEPARATOR . 'RolesSeeder.php',
);
self::assertStringContainsString('namespace App\\Seeders;', $contents);
self::assertStringContainsString(
'final class RolesSeeder extends AbstractSeeder',
$contents,
);
self::assertStringContainsString("return 'roles';", $contents);
self::assertStringContainsString('public function run(Orm $orm): void', $contents);
}
#[Test]
public function make_seeder_generates_valid_php(): void
{
new MakeSeederCommand()->execute(
['RolesSeeder'],
['dir' => $this->directory],
$this->console(),
);
self::assertSame(
'No syntax errors detected in '
. str_replace(
['\\', '/'],
'/',
$this->directory . DIRECTORY_SEPARATOR . 'RolesSeeder.php',
),
$this->lint('RolesSeeder.php'),
);
}
/**
* `php -l` prints its verdict on the first line; the path separator differs per platform.
*/
private function lint(string $file): string
{
$output = [];
exec(
escapeshellarg(PHP_BINARY)
. ' -l '
. escapeshellarg($this->directory . DIRECTORY_SEPARATOR . $file)
. ' 2>&1',
$output,
);
$first = $output[0] ?? '';
return trim(str_replace(['\\', '/'], '/', $first));
}
}