2013-07-18 07:09:21 +02:00
|
|
|
<?php
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
/**
|
|
|
|
* An extension of ErrorControlChain that runs the chain in a subprocess.
|
|
|
|
*
|
|
|
|
* We need this because ErrorControlChain only suppresses uncaught fatal errors, and
|
|
|
|
* that would kill PHPUnit execution
|
|
|
|
*/
|
|
|
|
class ErrorControlChainTest_Chain extends ErrorControlChain {
|
|
|
|
|
2013-07-31 23:44:36 +02:00
|
|
|
// Change function visibility to be testable directly
|
|
|
|
public function translateMemstring($memstring) {
|
|
|
|
return parent::translateMemstring($memstring);
|
|
|
|
}
|
|
|
|
|
2013-12-21 01:17:47 +01:00
|
|
|
function executeInSubprocess($includeStderr = false) {
|
2013-07-23 01:51:38 +02:00
|
|
|
// Get the path to the ErrorControlChain class
|
|
|
|
$classpath = SS_ClassLoader::instance()->getItemPath('ErrorControlChain');
|
|
|
|
$suppression = $this->suppression ? 'true' : 'false';
|
|
|
|
|
|
|
|
// Start building a PHP file that will execute the chain
|
|
|
|
$src = '<'."?php
|
|
|
|
require_once '$classpath';
|
|
|
|
|
|
|
|
\$chain = new ErrorControlChain();
|
|
|
|
|
|
|
|
\$chain->setSuppression($suppression);
|
|
|
|
|
|
|
|
\$chain
|
|
|
|
";
|
|
|
|
|
|
|
|
// For each step, use reflection to pull out the call, stick in the the PHP source we're building
|
|
|
|
foreach ($this->steps as $step) {
|
|
|
|
$func = new ReflectionFunction($step['callback']);
|
|
|
|
$source = file($func->getFileName());
|
|
|
|
|
|
|
|
$start_line = $func->getStartLine() - 1;
|
|
|
|
$end_line = $func->getEndLine();
|
|
|
|
$length = $end_line - $start_line;
|
|
|
|
|
|
|
|
$src .= implode("", array_slice($source, $start_line, $length)) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally add a line to execute the chain
|
|
|
|
$src .= "->execute();";
|
|
|
|
|
|
|
|
// Now stick it in a temporary file & run it
|
|
|
|
$codepath = TEMP_FOLDER.'/ErrorControlChainTest_'.sha1($src).'.php';
|
|
|
|
|
2013-12-21 01:17:47 +01:00
|
|
|
if($includeStderr) {
|
|
|
|
$null = '&1';
|
|
|
|
} else {
|
|
|
|
$null = is_writeable('/dev/null') ? '/dev/null' : 'NUL';
|
|
|
|
}
|
2013-07-23 01:51:38 +02:00
|
|
|
|
|
|
|
file_put_contents($codepath, $src);
|
2013-12-21 01:17:47 +01:00
|
|
|
exec("php $codepath 2>$null", $stdout, $errcode);
|
2013-07-23 01:51:38 +02:00
|
|
|
unlink($codepath);
|
|
|
|
|
|
|
|
return array(implode("\n", $stdout), $errcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
class ErrorControlChainTest extends SapphireTest {
|
|
|
|
|
2016-04-01 00:03:21 +02:00
|
|
|
protected $displayErrors = null;
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
function setUp() {
|
2016-04-01 00:03:21 +02:00
|
|
|
$this->displayErrors = (bool)ini_get('display_errors');
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
// Check we can run PHP at all
|
|
|
|
$null = is_writeable('/dev/null') ? '/dev/null' : 'NUL';
|
|
|
|
exec("php -v 2> $null", $out, $rv);
|
|
|
|
|
|
|
|
if ($rv != 0) {
|
|
|
|
$this->markTestSkipped("Can't run PHP from the command line - is it in your path?");
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:03:21 +02:00
|
|
|
public function tearDown() {
|
|
|
|
if($this->displayErrors !== null) {
|
|
|
|
ini_set('display_errors', $this->displayErrors);
|
|
|
|
$this->displayErrors = null;
|
|
|
|
}
|
|
|
|
parent::tearDown(); // TODO: Change the autogenerated stub
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
function testErrorSuppression() {
|
|
|
|
|
2016-04-01 00:03:21 +02:00
|
|
|
// Errors disabled by default
|
|
|
|
ini_set('display_errors', false);
|
|
|
|
$chain = new ErrorControlChain();
|
|
|
|
$whenNotSuppressed = null;
|
|
|
|
$whenSuppressed = null;
|
|
|
|
$chain->then(function($chain) use(&$whenNotSuppressed, &$whenSuppressed) {
|
|
|
|
$chain->setSuppression(true);
|
|
|
|
$whenSuppressed = ini_get('display_errors');
|
|
|
|
$chain->setSuppression(false);
|
|
|
|
$whenNotSuppressed = ini_get('display_errors');
|
|
|
|
})->execute();
|
|
|
|
|
|
|
|
// Disabled errors never un-disable
|
|
|
|
$this->assertFalse((bool)$whenNotSuppressed);
|
|
|
|
$this->assertFalse((bool)$whenSuppressed);
|
|
|
|
|
|
|
|
// Errors enabled by default
|
|
|
|
ini_set('display_errors', true);
|
|
|
|
$chain = new ErrorControlChain();
|
|
|
|
$whenNotSuppressed = null;
|
|
|
|
$whenSuppressed = null;
|
|
|
|
$chain->then(function($chain) use(&$whenNotSuppressed, &$whenSuppressed) {
|
|
|
|
$chain->setSuppression(true);
|
|
|
|
$whenSuppressed = ini_get('display_errors');
|
|
|
|
$chain->setSuppression(false);
|
|
|
|
$whenNotSuppressed = ini_get('display_errors');
|
|
|
|
})->execute();
|
|
|
|
|
|
|
|
// Errors can be suppressed an un-suppressed when initially enabled
|
|
|
|
$this->assertTrue((bool)$whenNotSuppressed);
|
|
|
|
$this->assertFalse((bool)$whenSuppressed);
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
// Fatal error
|
|
|
|
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
2013-07-18 07:09:21 +02:00
|
|
|
->then(function(){
|
2013-07-23 01:51:38 +02:00
|
|
|
Foo::bar(); // Non-existant class causes fatal error
|
|
|
|
})
|
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Done";
|
2013-07-18 07:09:21 +02:00
|
|
|
})
|
2013-07-23 01:51:38 +02:00
|
|
|
->executeInSubprocess();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
$this->assertEquals('Done', $out);
|
|
|
|
|
|
|
|
// User error
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
list($out, $code) = $chain
|
2013-07-18 07:09:21 +02:00
|
|
|
->then(function(){
|
2013-07-23 01:51:38 +02:00
|
|
|
user_error('Error', E_USER_ERROR);
|
2013-07-18 07:09:21 +02:00
|
|
|
})
|
2013-07-23 01:51:38 +02:00
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Done";
|
2013-07-18 07:09:21 +02:00
|
|
|
})
|
2013-07-23 01:51:38 +02:00
|
|
|
->executeInSubprocess();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
$this->assertEquals('Done', $out);
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
// Recoverable error
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
2013-07-18 07:09:21 +02:00
|
|
|
->then(function(){
|
2013-07-23 01:51:38 +02:00
|
|
|
$x = function(ErrorControlChain $foo){ };
|
|
|
|
$x(1); // Calling against type
|
2013-07-18 07:09:21 +02:00
|
|
|
})
|
2013-07-23 01:51:38 +02:00
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Done";
|
|
|
|
})
|
|
|
|
->executeInSubprocess();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
$this->assertEquals('Done', $out);
|
2013-07-31 23:44:36 +02:00
|
|
|
|
|
|
|
// Memory exhaustion
|
|
|
|
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function(){
|
|
|
|
ini_set('memory_limit', '10M');
|
|
|
|
$a = array();
|
|
|
|
while(1) $a[] = 1;
|
|
|
|
})
|
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Done";
|
|
|
|
})
|
|
|
|
->executeInSubprocess();
|
|
|
|
|
|
|
|
$this->assertEquals('Done', $out);
|
2014-09-26 06:54:34 +02:00
|
|
|
|
|
|
|
// Exceptions
|
|
|
|
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function(){
|
|
|
|
throw new Exception("bob");
|
|
|
|
})
|
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Done";
|
|
|
|
})
|
|
|
|
->executeInSubprocess();
|
|
|
|
|
|
|
|
$this->assertEquals('Done', $out);
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
function testExceptionSuppression() {
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
list($out, $code) = $chain
|
2013-07-18 07:09:21 +02:00
|
|
|
->then(function(){
|
|
|
|
throw new Exception('This exception should be suppressed');
|
|
|
|
})
|
2013-07-23 01:51:38 +02:00
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Done";
|
2013-07-18 07:09:21 +02:00
|
|
|
})
|
2013-07-23 01:51:38 +02:00
|
|
|
->executeInSubprocess();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
$this->assertEquals('Done', $out);
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testErrorControl() {
|
2013-07-23 01:51:38 +02:00
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function() { echo 'preThen,'; })
|
|
|
|
->thenIfErrored(function() { echo 'preThenIfErrored,'; })
|
|
|
|
->thenAlways(function() { echo 'preThenAlways,'; })
|
2013-07-18 07:09:21 +02:00
|
|
|
|
|
|
|
->then(function(){ user_error('An error', E_USER_ERROR); })
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
->then(function() { echo 'postThen,'; })
|
|
|
|
->thenIfErrored(function() { echo 'postThenIfErrored,'; })
|
|
|
|
->thenAlways(function() { echo 'postThenAlways,'; })
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
->executeInSubprocess();
|
2013-07-18 07:09:21 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2013-07-23 01:51:38 +02:00
|
|
|
"preThen,preThenAlways,postThenIfErrored,postThenAlways,",
|
|
|
|
$out
|
2013-07-18 07:09:21 +02:00
|
|
|
);
|
2013-07-23 01:51:38 +02:00
|
|
|
}
|
2013-07-18 07:09:21 +02:00
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
function testSuppressionControl() {
|
|
|
|
// Turning off suppression before execution
|
|
|
|
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
$chain->setSuppression(false);
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function($chain){
|
|
|
|
Foo::bar(); // Non-existant class causes fatal error
|
|
|
|
})
|
2013-12-21 01:17:47 +01:00
|
|
|
->executeInSubprocess(true);
|
2013-07-23 01:51:38 +02:00
|
|
|
|
2013-12-21 01:17:47 +01:00
|
|
|
$this->assertContains('Fatal error', $out);
|
|
|
|
$this->assertContains('Foo', $out);
|
2013-07-23 01:51:38 +02:00
|
|
|
|
|
|
|
// Turning off suppression during execution
|
|
|
|
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function($chain){
|
|
|
|
$chain->setSuppression(false);
|
|
|
|
Foo::bar(); // Non-existent class causes fatal error
|
|
|
|
})
|
2013-12-21 01:17:47 +01:00
|
|
|
->executeInSubprocess(true);
|
2013-07-23 01:51:38 +02:00
|
|
|
|
2013-12-21 01:17:47 +01:00
|
|
|
$this->assertContains('Fatal error', $out);
|
|
|
|
$this->assertContains('Foo', $out);
|
2013-07-18 07:09:21 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
function testDoesntAffectNonFatalErrors() {
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function(){
|
|
|
|
$array = null;
|
|
|
|
if (@$array['key'] !== null) user_error('Error', E_USER_ERROR);
|
|
|
|
})
|
|
|
|
->then(function(){
|
|
|
|
echo "Good";
|
|
|
|
})
|
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Bad";
|
|
|
|
})
|
|
|
|
->executeInSubprocess();
|
|
|
|
|
|
|
|
$this->assertContains("Good", $out);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDoesntAffectCaughtExceptions() {
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function(){
|
|
|
|
try {
|
|
|
|
throw new Exception('Error');
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
echo "Good";
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Bad";
|
|
|
|
})
|
|
|
|
->executeInSubprocess();
|
|
|
|
|
|
|
|
$this->assertContains("Good", $out);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDoesntAffectHandledErrors() {
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
list($out, $code) = $chain
|
|
|
|
->then(function(){
|
|
|
|
set_error_handler(function(){ /* NOP */ });
|
|
|
|
user_error('Error', E_USER_ERROR);
|
|
|
|
})
|
|
|
|
->then(function(){
|
|
|
|
echo "Good";
|
|
|
|
})
|
|
|
|
->thenIfErrored(function(){
|
|
|
|
echo "Bad";
|
|
|
|
})
|
|
|
|
->executeInSubprocess();
|
|
|
|
|
|
|
|
$this->assertContains("Good", $out);
|
|
|
|
}
|
2013-07-31 23:44:36 +02:00
|
|
|
|
|
|
|
function testMemoryConversion() {
|
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
|
|
|
|
$this->assertEquals(200, $chain->translateMemstring('200'));
|
|
|
|
$this->assertEquals(300, $chain->translateMemstring('300'));
|
|
|
|
|
|
|
|
$this->assertEquals(2 * 1024, $chain->translateMemstring('2k'));
|
|
|
|
$this->assertEquals(3 * 1024, $chain->translateMemstring('3K'));
|
|
|
|
|
|
|
|
$this->assertEquals(2 * 1024 * 1024, $chain->translateMemstring('2m'));
|
|
|
|
$this->assertEquals(3 * 1024 * 1024, $chain->translateMemstring('3M'));
|
|
|
|
|
|
|
|
$this->assertEquals(2 * 1024 * 1024 * 1024, $chain->translateMemstring('2g'));
|
|
|
|
$this->assertEquals(3 * 1024 * 1024 * 1024, $chain->translateMemstring('3G'));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $chain->translateMemstring('200foo'));
|
|
|
|
$this->assertEquals(300, $chain->translateMemstring('300foo'));
|
|
|
|
}
|
2013-12-21 01:17:47 +01:00
|
|
|
}
|