Skip to content

Commit 5c7f87b

Browse files
committed
Zend: Add tests for exit/die
1 parent 6303d1f commit 5c7f87b

13 files changed

+363
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Attempting to define die constant
3+
--FILE--
4+
<?php
5+
6+
const die = 5;
7+
8+
var_dump(die);
9+
10+
?>
11+
--EXPECTF--
12+
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Attempting to define die constant in a namespace
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
const die = 5;
9+
10+
var_dump(die);
11+
12+
?>
13+
--EXPECTF--
14+
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Attempting to define die() function
3+
--FILE--
4+
<?php
5+
6+
function die() { }
7+
8+
?>
9+
--EXPECTF--
10+
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Attempting to define die() function in a namespace
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
function die() { }
9+
10+
var_dump(die());
11+
12+
?>
13+
--EXPECTF--
14+
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Attempting to define exit constant
3+
--FILE--
4+
<?php
5+
6+
const exit = 5;
7+
8+
var_dump(exit);
9+
10+
?>
11+
--EXPECTF--
12+
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Attempting to define exit constant in a namespace
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
const exit = 5;
9+
10+
var_dump(exit);
11+
12+
?>
13+
--EXPECTF--
14+
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Attempting to define die constant
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public $exit;
8+
public $die;
9+
10+
const die = 5;
11+
const exit = 10;
12+
13+
public function exit() {
14+
return 20;
15+
}
16+
17+
public function die() {
18+
return 15;
19+
}
20+
}
21+
22+
var_dump(Foo::die);
23+
var_dump(Foo::exit);
24+
$o = new Foo();
25+
var_dump($o->exit);
26+
var_dump($o->die);
27+
var_dump($o->exit());
28+
var_dump($o->die());
29+
30+
?>
31+
--EXPECT--
32+
int(5)
33+
int(10)
34+
NULL
35+
NULL
36+
int(20)
37+
int(15)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Attempting to define exit() function
3+
--FILE--
4+
<?php
5+
6+
function exit() { }
7+
8+
?>
9+
--EXPECTF--
10+
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Attempting to define exit() function in a namespace
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
function exit() { }
9+
10+
?>
11+
--EXPECTF--
12+
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d

Zend/tests/exit/disabling_die.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Using disable_functions INI to remove die
3+
--INI--
4+
disable_functions=die
5+
--FILE--
6+
<?php
7+
8+
die();
9+
10+
?>
11+
--EXPECT--

Zend/tests/exit/disabling_exit.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Using disable_functions INI to remove exit
3+
--INI--
4+
disable_functions=exit
5+
--FILE--
6+
<?php
7+
8+
exit();
9+
10+
?>
11+
--EXPECT--

Zend/tests/exit/exit_as_function.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
exit() as function
3+
--FILE--
4+
<?php
5+
6+
function foo(callable $fn) {
7+
var_dump($fn);
8+
}
9+
10+
$values = [
11+
'exit',
12+
'die',
13+
exit(...),
14+
die(...),
15+
];
16+
17+
foreach ($values as $value) {
18+
foo($value);
19+
}
20+
21+
?>
22+
--EXPECTF--
23+
Parse error: syntax error, unexpected token "...", expecting ")" in %s on line %d

Zend/tests/exit/exit_values.phpt

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
--TEST--
2+
exit(false);
3+
--FILE--
4+
<?php
5+
6+
function zend_test_var_export($value) {
7+
if ($value === PHP_INT_MIN) {
8+
return "PHP_INT_MIN";
9+
}
10+
if ($value === PHP_INT_MAX) {
11+
return "PHP_INT_MAX";
12+
}
13+
if (is_array($value)) {
14+
return "[]";
15+
}
16+
if (is_resource($value)) {
17+
return "STDERR";
18+
}
19+
if ($value instanceof stdClass) {
20+
return "new stdClass()";
21+
}
22+
return var_export($value, true);
23+
}
24+
25+
$values = [
26+
null,
27+
false,
28+
true,
29+
0,
30+
1,
31+
20,
32+
10.0,
33+
15.5,
34+
"Hello world",
35+
[],
36+
STDERR,
37+
new stdClass(),
38+
];
39+
40+
const FILE_PATH = __DIR__ . '/exit_values.inc';
41+
const FILE_CONTENT = <<<'TEMPLATE'
42+
<?php
43+
try {
44+
exit(VALUE);
45+
} catch (\Throwable $e) {
46+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
47+
}
48+
49+
TEMPLATE;
50+
51+
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
52+
$command = $php . ' ' . escapeshellarg(FILE_PATH);
53+
54+
foreach ([FILE_CONTENT, str_replace('exit', 'die', FILE_CONTENT)] as $code) {
55+
foreach ($values as $value) {
56+
echo 'Using ', zend_test_var_export($value), ' as value:', PHP_EOL;
57+
$output = [];
58+
$content = str_replace('VALUE', zend_test_var_export($value), $code);
59+
file_put_contents(FILE_PATH, $content);
60+
exec($command, $output, $exit_status);
61+
echo 'Exit status is: ', $exit_status, PHP_EOL,
62+
'Output is:', PHP_EOL, join($output), PHP_EOL;
63+
}
64+
65+
echo 'As a statement:', PHP_EOL;
66+
$output = [];
67+
$content = str_replace('(VALUE)', '', $code);
68+
exec($command, $output, $exit_status);
69+
echo 'Exit status is: ', $exit_status, PHP_EOL,
70+
'Output is:', PHP_EOL, join($output), PHP_EOL;
71+
}
72+
73+
?>
74+
--CLEAN--
75+
<?php
76+
const FILE_PATH = __DIR__ . '/exit_values.inc';
77+
@unlink(FILE_PATH);
78+
?>
79+
--EXPECTF--
80+
Using NULL as value:
81+
Exit status is: 0
82+
Output is:
83+
84+
Using false as value:
85+
Exit status is: 0
86+
Output is:
87+
88+
Using true as value:
89+
Exit status is: 0
90+
Output is:
91+
1
92+
Using 0 as value:
93+
Exit status is: 0
94+
Output is:
95+
96+
Using 1 as value:
97+
Exit status is: 1
98+
Output is:
99+
100+
Using 20 as value:
101+
Exit status is: 20
102+
Output is:
103+
104+
Using 10.0 as value:
105+
Exit status is: 0
106+
Output is:
107+
10
108+
Using 15.5 as value:
109+
Exit status is: 0
110+
Output is:
111+
15.5
112+
Using 'Hello world' as value:
113+
Exit status is: 0
114+
Output is:
115+
Hello world
116+
Using [] as value:
117+
Exit status is: 0
118+
Output is:
119+
Warning: Array to string conversion in %s on line 3Array
120+
Using STDERR as value:
121+
Exit status is: 0
122+
Output is:
123+
Resource id #3
124+
Using new stdClass() as value:
125+
Exit status is: 0
126+
Output is:
127+
Error: Object of class stdClass could not be converted to string
128+
As a statement:
129+
Exit status is: 0
130+
Output is:
131+
Error: Object of class stdClass could not be converted to string
132+
Using NULL as value:
133+
Exit status is: 0
134+
Output is:
135+
136+
Using false as value:
137+
Exit status is: 0
138+
Output is:
139+
140+
Using true as value:
141+
Exit status is: 0
142+
Output is:
143+
1
144+
Using 0 as value:
145+
Exit status is: 0
146+
Output is:
147+
148+
Using 1 as value:
149+
Exit status is: 1
150+
Output is:
151+
152+
Using 20 as value:
153+
Exit status is: 20
154+
Output is:
155+
156+
Using 10.0 as value:
157+
Exit status is: 0
158+
Output is:
159+
10
160+
Using 15.5 as value:
161+
Exit status is: 0
162+
Output is:
163+
15.5
164+
Using 'Hello world' as value:
165+
Exit status is: 0
166+
Output is:
167+
Hello world
168+
Using [] as value:
169+
Exit status is: 0
170+
Output is:
171+
Warning: Array to string conversion in %s on line 3Array
172+
Using STDERR as value:
173+
Exit status is: 0
174+
Output is:
175+
Resource id #3
176+
Using new stdClass() as value:
177+
Exit status is: 0
178+
Output is:
179+
Error: Object of class stdClass could not be converted to string
180+
As a statement:
181+
Exit status is: 0
182+
Output is:
183+
Error: Object of class stdClass could not be converted to string

0 commit comments

Comments
 (0)