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 {
|
|
|
|
|
2016-05-18 03:36:54 +02:00
|
|
|
protected $displayErrors = 'STDERR';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify method visibility to public for testing
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDisplayErrors()
|
|
|
|
{
|
|
|
|
// Protect manipulation of underlying php_ini values
|
|
|
|
return $this->displayErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify method visibility to public for testing
|
|
|
|
*
|
|
|
|
* @param mixed $errors
|
|
|
|
*/
|
|
|
|
public function setDisplayErrors($errors)
|
|
|
|
{
|
|
|
|
// Protect manipulation of underlying php_ini values
|
|
|
|
$this->displayErrors = $errors;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
2013-07-23 01:51:38 +02:00
|
|
|
function setUp() {
|
2016-04-01 00:03:21 +02:00
|
|
|
|
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?");
|
|
|
|
$this->skipTest = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:09:21 +02:00
|
|
|
function testErrorSuppression() {
|
|
|
|
|
2016-04-01 00:03:21 +02:00
|
|
|
// Errors disabled by default
|
2016-05-18 03:36:54 +02:00
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
$chain->setDisplayErrors('Off'); // mocks display_errors: Off
|
|
|
|
$initialValue = null;
|
2016-04-01 00:03:21 +02:00
|
|
|
$whenNotSuppressed = null;
|
|
|
|
$whenSuppressed = null;
|
2016-05-18 03:36:54 +02:00
|
|
|
$chain->then(
|
|
|
|
function(ErrorControlChainTest_Chain $chain)
|
|
|
|
use(&$initialValue, &$whenNotSuppressed, &$whenSuppressed) {
|
|
|
|
$initialValue = $chain->getDisplayErrors();
|
|
|
|
$chain->setSuppression(false);
|
|
|
|
$whenNotSuppressed = $chain->getDisplayErrors();
|
|
|
|
$chain->setSuppression(true);
|
|
|
|
$whenSuppressed = $chain->getDisplayErrors();
|
|
|
|
}
|
|
|
|
)->execute();
|
2016-04-01 00:03:21 +02:00
|
|
|
|
|
|
|
// Disabled errors never un-disable
|
2016-05-18 03:36:54 +02:00
|
|
|
$this->assertEquals(0, $initialValue); // Chain starts suppressed
|
|
|
|
$this->assertEquals(0, $whenSuppressed); // false value used internally when suppressed
|
|
|
|
$this->assertEquals('Off', $whenNotSuppressed); // false value set by php ini when suppression lifted
|
|
|
|
$this->assertEquals('Off', $chain->getDisplayErrors()); // Correctly restored after run
|
2016-04-01 00:03:21 +02:00
|
|
|
|
|
|
|
// Errors enabled by default
|
2016-05-18 03:36:54 +02:00
|
|
|
$chain = new ErrorControlChainTest_Chain();
|
|
|
|
$chain->setDisplayErrors('Yes'); // non-falsey ini value
|
|
|
|
$initialValue = null;
|
2016-04-01 00:03:21 +02:00
|
|
|
$whenNotSuppressed = null;
|
|
|
|
$whenSuppressed = null;
|
2016-05-18 03:36:54 +02:00
|
|
|
$chain->then(
|
|
|
|
function(ErrorControlChainTest_Chain $chain)
|
|
|
|
use(&$initialValue, &$whenNotSuppressed, &$whenSuppressed) {
|
|
|
|
$initialValue = $chain->getDisplayErrors();
|
|
|
|
$chain->setSuppression(true);
|
|
|
|
$whenSuppressed = $chain->getDisplayErrors();
|
|
|
|
$chain->setSuppression(false);
|
|
|
|
$whenNotSuppressed = $chain->getDisplayErrors();
|
|
|
|
}
|
|
|
|
)->execute();
|
2016-04-01 00:03:21 +02:00
|
|
|
|
|
|
|
// Errors can be suppressed an un-suppressed when initially enabled
|
2016-05-18 03:36:54 +02:00
|
|
|
$this->assertEquals(0, $initialValue); // Chain starts suppressed
|
|
|
|
$this->assertEquals(0, $whenSuppressed); // false value used internally when suppressed
|
|
|
|
$this->assertEquals('Yes', $whenNotSuppressed); // false value set by php ini when suppression lifted
|
|
|
|
$this->assertEquals('Yes', $chain->getDisplayErrors()); // Correctly restored after run
|
2016-04-01 00:03:21 +02:00
|
|
|
|
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);
|
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
|
|
|
}
|