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