diff --git a/examples/001-SimpleWeb/example.php b/examples/001-SimpleWeb/example.php index b7aef423..90b8feef 100644 --- a/examples/001-SimpleWeb/example.php +++ b/examples/001-SimpleWeb/example.php @@ -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 ''; echo '

Hello ', htmlspecialchars($name), "

\n"; diff --git a/test/unit/PhpcCliTest.php b/test/unit/PhpcCliTest.php index c6164dee..dcf5ae3d 100644 --- a/test/unit/PhpcCliTest.php +++ b/test/unit/PhpcCliTest.php @@ -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 $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'], @@ -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'] + ); } /**