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=
', $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));
}
}