xref: /PHP-8.1/sapi/phpdbg/create-test.php (revision 01b3fc03)
1#!/usr/bin/env php
2<?php
3
4/*
5   +----------------------------------------------------------------------+
6   | Copyright (c) The PHP Group                                          |
7   +----------------------------------------------------------------------+
8   | This source file is subject to version 3.01 of the PHP license,      |
9   | that is bundled with this package in the file LICENSE, and is        |
10   | available through the world-wide-web at the following url:           |
11   | https://www.php.net/license/3_01.txt                                 |
12   | If you did not receive a copy of the PHP license and are unable to   |
13   | obtain it through the world-wide-web, please send a note to          |
14   | license@php.net so we can mail you a copy immediately.               |
15   +----------------------------------------------------------------------+
16   | Authors: Bob Weinand <bwoebi@php.net>                                |
17   +----------------------------------------------------------------------+
18*/
19
20#####
21## This is just a helper for intercepting stdin/stdout and the file and create a half-finished test.
22## The output still may need adapting to match file names etc.
23#####
24
25error_reporting(-1);
26
27$phpdbg = getenv('TEST_PHPDBG_EXECUTABLE') ?: null;
28$pass_options = ' -qbI -n -d "error_reporting=32767" -d "display_errors=1" -d "display_startup_errors=1" -d "log_errors=0"';
29$file = "";
30$cmdargv = "";
31
32if (isset($argc) && $argc > 1) {
33    $post_ddash = false;
34    for ($i = 1; $i < $argc; $i++) {
35        if ($argv[$i][0] == "-" && !$post_ddash) {
36            switch (substr($argv[$i], 1)) {
37                case "p":
38                    $phpdbg = $argv[++$i];
39                    break;
40                case "n":
41                    $pass_options .= " -n";
42                    break;
43                case "d":
44                    $pass_options .= " -d ".escapeshellarg($argv[++$i]);
45                    $ini[] = $argv[$i];
46                    break;
47                case "-":
48                    $post_ddash = true;
49                    break;
50            }
51        } else {
52            $real_argv[] = $argv[$i];
53        }
54    }
55    if (isset($real_argv[0])) {
56        $file = realpath($real_argv[0]);
57        $cmdargv = implode(" ", array_map("escapeshellarg", array_slice($real_argv, 1)));
58    }
59}
60
61$proc = proc_open("$phpdbg $pass_options $file -- $cmdargv", [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]], $pipes);
62if (!$proc) {
63    die("Couldn't start phpdbg\n");
64}
65
66$input = $output = "";
67
68stream_set_blocking(STDIN, false);
69
70do {
71    $r = [$pipes[1], STDIN];
72    $w = $e = null;
73    $n = @stream_select($r, $w, $e, null);
74
75    if ($n > 0) {
76        if ("" != $in = fread(STDIN, 1024)) {
77            $input .= $in;
78            fwrite($pipes[0], $in);
79            continue;
80        }
81
82        if (feof(STDIN)) {
83            die("stdin closed?!\n");
84        }
85
86        if (feof($pipes[1])) {
87            $n = false;
88        } else {
89            $output .= $c = fgetc($pipes[1]);
90            echo $c;
91        }
92    }
93} while ($n !== false);
94
95stream_set_blocking(STDIN, true);
96
97print "\n";
98if (!isset($name)) {
99    print "Specify the test description: ";
100    $desc = trim(fgets(STDIN));
101}
102while (!isset($testfile)) {
103    print "Specify the test file name (leave empty to write to stderr): ";
104    $testfile = trim(fgets(STDIN));
105    if ($testfile != "" && file_exists($testfile)) {
106        print "That file already exists. Type y or yes to overwrite: ";
107        $y = trim(fgets(STDIN));
108        if ($y !== "y" && $y !== "yes") {
109            unset($testfile);
110        }
111    }
112}
113
114$output = str_replace("string(".strlen($file).") \"$file\"", 'string(%d) "%s"', $output);
115$output = str_replace($file, "%s", $output);
116$input = trim($input);
117
118$testdata = <<<TEST
119--TEST--
120$desc
121--PHPDBG--
122$input
123--EXPECTF--
124$output
125TEST;
126
127if (!empty($ini)) {
128    $testdata .= "\n--INI--\n".implode("\n", $ini);
129}
130if ($cmdargv != "") {
131    $testdata .= "\n--ARGS--\n$cmdargv";
132}
133if ($file != "") {
134    $testdata .= "\n--FILE--\n".file_get_contents($file);
135}
136
137if ($testfile == "") {
138    print "\n";
139} elseif (file_put_contents($testfile, $testdata)) {
140    print "Test saved to $testfile\n";
141} else {
142    print "The test could not be saved to $testfile; outputting on stderr now\n";
143    $testfile = "";
144}
145
146if ($testfile == "") {
147    fwrite(STDERR, $testdata);
148}
149