Skip to content

Commit

Permalink
Zend: Add tests for exit/die
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed May 11, 2024
1 parent 188baaf commit 8983724
Show file tree
Hide file tree
Showing 24 changed files with 471 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Zend/tests/bug77339.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Foo
if (!isset($arguments[0])) {
var_dump(['getSomeWhat']);
var_dump($arguments);
exit;
exit();
}
}
echo "OK\n";
Expand Down
37 changes: 37 additions & 0 deletions Zend/tests/exit/define_class_members_exit_die.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Can define die and exit as class methods, constants and property
--FILE--
<?php

class Foo {
public $exit;
public $die;

const die = 5;
const exit = 10;

public function exit() {
return 20;
}

public function die() {
return 15;
}
}

var_dump(Foo::die);
var_dump(Foo::exit);
$o = new Foo();
var_dump($o->exit);
var_dump($o->die);
var_dump($o->exit());
var_dump($o->die());

?>
--EXPECT--
int(5)
int(10)
NULL
NULL
int(20)
int(15)
12 changes: 12 additions & 0 deletions Zend/tests/exit/define_die_constant.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Attempting to define die constant
--FILE--
<?php

const die = 5;

var_dump(die);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_die_constant_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define die constant in a namespace
--FILE--
<?php

namespace Foo;

const die = 5;

var_dump(die);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/exit/define_die_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Attempting to define die() function
--FILE--
<?php

function die() { }

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_die_function_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define die() function in a namespace
--FILE--
<?php

namespace Foo;

function die() { }

var_dump(die());

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/exit/define_exit_constant.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Attempting to define exit constant
--FILE--
<?php

const exit = 5;

var_dump(exit);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_exit_constant_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define exit constant in a namespace
--FILE--
<?php

namespace Foo;

const exit = 5;

var_dump(exit);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/exit/define_exit_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Attempting to define exit() function
--FILE--
<?php

function exit() { }

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/exit/define_exit_function_namespace.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Attempting to define exit() function in a namespace
--FILE--
<?php

namespace Foo;

function exit() { }

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting "(" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_die.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called die
--FILE--
<?php

echo "Before\n";

echo "In between\n";
die:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token ":" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_die_with_jump.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called die and jump to it
--FILE--
<?php

echo "Before\n";
goto die;
echo "In between\n";
die:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_exit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called exit
--FILE--
<?php

echo "Before\n";

echo "In between\n";
exit:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token ":" in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/exit/define_goto_label_exit_with_jump.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Attempting to define a goto label called exit and jump to it
--FILE--
<?php

echo "Before\n";
goto exit;
echo "In between\n";
exit:
echo "After\n";

?>
--EXPECTF--
Parse error: syntax error, unexpected token "exit", expecting identifier in %s on line %d
File renamed without changes.
11 changes: 11 additions & 0 deletions Zend/tests/exit/disabling_die.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Using disable_functions INI to remove die
--INI--
disable_functions=die
--FILE--
<?php

die();

?>
--EXPECT--
11 changes: 11 additions & 0 deletions Zend/tests/exit/disabling_exit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Using disable_functions INI to remove exit
--INI--
disable_functions=exit
--FILE--
<?php

exit();

?>
--EXPECT--
23 changes: 23 additions & 0 deletions Zend/tests/exit/exit_as_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
exit() as function
--FILE--
<?php

function foo(callable $fn) {
var_dump($fn);
}

$values = [
'exit',
'die',
exit(...),
die(...),
];

foreach ($values as $value) {
foo($value);
}

?>
--EXPECTF--
Parse error: syntax error, unexpected token "...", expecting ")" in %s on line %d
46 changes: 46 additions & 0 deletions Zend/tests/exit/exit_statements.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Using exit/die as a statement/constant
--FILE--
<?php

const FILE_PATH = __DIR__ . '/exit_statements.inc';
const FILE_CONTENT = <<<'TEMPLATE'
<?php
echo "Before FUNCTION";
try {
FUNCTION;
} catch (\Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

TEMPLATE;


$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
$command = $php . ' ' . escapeshellarg(FILE_PATH);

foreach (['exit', 'die'] as $value) {
echo 'Using ', $value, ' as value:', PHP_EOL;
$output = [];
$content = str_replace('FUNCTION', $value, FILE_CONTENT);
file_put_contents(FILE_PATH, $content);
exec($command, $output, $exit_status);
echo 'Exit status is: ', $exit_status, PHP_EOL,
'Output is:', PHP_EOL, join($output), PHP_EOL;
}

?>
--CLEAN--
<?php
const FILE_PATH = __DIR__ . '/exit_statements.inc';
@unlink(FILE_PATH);
?>
--EXPECT--
Using exit as value:
Exit status is: 0
Output is:
Before exit
Using die as value:
Exit status is: 0
Output is:
Before die
Loading

0 comments on commit 8983724

Please sign in to comment.