66 lines
2 KiB
PHP
66 lines
2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Phred\Tests\Feature;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class StubCompilationTest extends TestCase
|
|
{
|
|
public function testStubsCompile(): void
|
|
{
|
|
$stubRoot = dirname(__DIR__, 2) . '/src/stubs';
|
|
$stubs = $this->findStubs($stubRoot);
|
|
|
|
foreach ($stubs as $stubPath) {
|
|
$content = file_get_contents($stubPath);
|
|
$compiled = $this->interpolateStub($content);
|
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'stub_test_');
|
|
file_put_contents($tmpFile, $compiled);
|
|
|
|
$output = [];
|
|
$returnVar = 0;
|
|
exec("php -l " . escapeshellarg($tmpFile) . " 2>&1", $output, $returnVar);
|
|
|
|
$this->assertSame(0, $returnVar, "Syntax error in stub: $stubPath\n" . implode("\n", $output));
|
|
|
|
unlink($tmpFile);
|
|
}
|
|
}
|
|
|
|
private function findStubs(string $dir): array
|
|
{
|
|
$files = [];
|
|
$items = scandir($dir);
|
|
foreach ($items as $item) {
|
|
if ($item === '.' || $item === '..') continue;
|
|
$path = $dir . '/' . $item;
|
|
if (is_dir($path)) {
|
|
$files = array_merge($files, $this->findStubs($path));
|
|
} elseif (str_ends_with($item, '.stub')) {
|
|
$files[] = $path;
|
|
}
|
|
}
|
|
return $files;
|
|
}
|
|
|
|
private function interpolateStub(string $content): string
|
|
{
|
|
$replacements = [
|
|
'{{namespace}}' => 'App\\Test',
|
|
'{{viewNamespace}}' => 'App\\Views\\TestView',
|
|
'{{class}}' => 'TestClass',
|
|
'{{name}}' => 'TestName',
|
|
'{{moduleName}}' => 'TestModule',
|
|
'{{template}}' => 'test_template',
|
|
'{{params}}' => '$request',
|
|
'{{body}}' => 'return $request;',
|
|
'{{useView}}' => 'use App\\Views\\TestView;',
|
|
'$dollar' => '$', // For some stubs that use $dollar
|
|
];
|
|
|
|
return strtr($content, $replacements);
|
|
}
|
|
}
|