Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/001-SimpleWeb/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* $_SERVER['REQUEST_METHOD'] and $_REQUEST are populated automatically (see lib/Web/Superglobals.php).
*/
// AOT: set QUERY_STRING (or use phpc serve --aot); compile-time -q is optional.
$name = $_GET['name'];
$name = $_REQUEST['name'];
header('Content-Type: text/html; charset=UTF-8');
echo '<!DOCTYPE html><html><head><link rel="stylesheet" href="/style.css"></head><body>';
echo '<h1>Hello ', htmlspecialchars($name), "</h1>\n";
Expand Down
96 changes: 66 additions & 30 deletions test/unit/PhpcCliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,63 @@
use PHPUnit\Framework\TestCase;

/**
* Unified phpc CLI dispatcher (issue #159).
* Unified phpc CLI dispatcher (issue #159, #294).
*/
final class PhpcCliTest extends TestCase
{
public function testHelpListsSubcommands(): void
{
$repoRoot = dirname(__DIR__, 2);
$cmd = array_merge(self::phpCommand(), [$repoRoot.'/bin/phpc.php', 'help']);
$descriptorSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$proc = proc_open($cmd, $descriptorSpec, $pipes, $repoRoot);
$this->assertIsResource($proc);
fclose($pipes[0]);
$out = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($proc);
$this->assertStringContainsString('phpc serve', $out !== false ? $out : '');
$this->assertStringContainsString('phpc serve --aot', $out !== false ? $out : '');
$this->assertStringContainsString('phpc run', $out !== false ? $out : '');
$this->assertStringContainsString('phpc build', $out !== false ? $out : '');
$this->assertStringContainsString('phpc test', $out !== false ? $out : '');
$this->assertStringContainsString('phpc lint', $out !== false ? $out : '');
$this->assertStringContainsString('-q', $out !== false ? $out : '');
$this->assertStringContainsString('$_GET', $out !== false ? $out : '');
$result = $this->runPhpc(['help']);
$this->assertSame(0, $result['exit']);
$this->assertStringContainsString('phpc serve', $result['stdout']);
$this->assertStringContainsString('phpc serve --aot', $result['stdout']);
$this->assertStringContainsString('phpc run', $result['stdout']);
$this->assertStringContainsString('phpc build', $result['stdout']);
$this->assertStringContainsString('phpc test', $result['stdout']);
$this->assertStringContainsString('phpc lint', $result['stdout']);
$this->assertStringContainsString('-q', $result['stdout']);
$this->assertStringContainsString('$_GET', $result['stdout']);
}

public function testRunSimpleWebWithQueryFlag(): void
{
$repoRoot = dirname(__DIR__, 2);
$script = $repoRoot.'/examples/001-SimpleWeb/example.php';
$script = $this->repoRoot().'/examples/001-SimpleWeb/example.php';
$result = $this->runPhpc(['run', '-q', 'name=Dev', $script]);
$this->assertSuccessfulRun($result);
$this->assertStringContainsString('Hello Dev', $result['stdout']);
}

public function testRunSimpleWebWithPostFlag(): void
{
$script = $this->repoRoot().'/examples/001-SimpleWeb/example.php';
$result = $this->runPhpc(['run', '-p', 'name=Post', $script]);
$this->assertSuccessfulRun($result);
$this->assertStringContainsString('Hello Post', $result['stdout']);
}

public function testRunMissingScriptExitsNonZero(): void
{
$result = $this->runPhpc(['run']);
$this->assertNotSame(0, $result['exit']);
$this->assertStringContainsString('missing script.php', $result['stderr']);
}

private function repoRoot(): string
{
return dirname(__DIR__, 2);
}

/**
* @param list<string> $phpcArgs arguments after bin/phpc.php
*
* @return array{exit: int, stdout: string, stderr: string}
*/
private function runPhpc(array $phpcArgs): array
{
$repoRoot = $this->repoRoot();
$cmd = array_merge(
self::phpCommand(),
[$repoRoot.'/bin/phpc.php', 'run', '-q', 'name=Dev', $script]
[$repoRoot.'/bin/phpc.php', ...$phpcArgs]
);
$descriptorSpec = [
0 => ['pipe', 'r'],
Expand All @@ -53,13 +73,29 @@ public function testRunSimpleWebWithQueryFlag(): void
$proc = proc_open($cmd, $descriptorSpec, $pipes, $repoRoot);
$this->assertIsResource($proc);
fclose($pipes[0]);
$out = stream_get_contents($pipes[1]);
$err = stream_get_contents($pipes[2]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$exit = proc_close($proc);
$this->assertSame(0, $exit, $err !== false ? $err : '');
$this->assertStringContainsString('Hello Dev', $out !== false ? $out : '');

return [
'exit' => is_int($exit) ? $exit : 1,
'stdout' => false !== $stdout ? $stdout : '',
'stderr' => false !== $stderr ? $stderr : '',
];
}

/**
* @param array{exit: int, stdout: string, stderr: string} $result
*/
private function assertSuccessfulRun(array $result): void
{
$this->assertSame(0, $result['exit'], trim($result['stderr']."\n".$result['stdout']));
$this->assertDoesNotMatchRegularExpression(
'/\b(Fatal error|Parse error|phpc run:)\b/',
$result['stderr']
);
}

/**
Expand Down
Loading