Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
246 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the moss-framework package | ||
* | ||
* (c) Michal Wachowski <wachowski.michal@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Moss\Kernel; | ||
|
||
class FunctionMockErrorHandler | ||
{ | ||
public static $level; | ||
public static $restored = false; | ||
public static $handler_callback; | ||
public static $handler_types; | ||
} | ||
|
||
function error_reporting($level) { FunctionMockErrorHandler::$level = $level; } | ||
|
||
function set_error_handler($callback, $types) | ||
{ | ||
FunctionMockErrorHandler::$handler_callback = $callback; | ||
FunctionMockErrorHandler::$handler_types = $types; | ||
} | ||
|
||
function restore_error_handler() { FunctionMockErrorHandler::$restored = true; } | ||
|
||
class ErrorHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider levelProvider | ||
*/ | ||
public function testRegister($level) | ||
{ | ||
$handler = new ErrorHandler(true, $level); | ||
$handler->register(); | ||
|
||
$this->assertEquals($level, FunctionMockErrorHandler::$level); | ||
$this->assertEquals([$handler, 'handler'], FunctionMockErrorHandler::$handler_callback); | ||
$this->assertEquals($level, FunctionMockErrorHandler::$handler_types); | ||
} | ||
|
||
public function levelProvider() | ||
{ | ||
return [ | ||
[-1], | ||
[E_ERROR], | ||
[E_WARNING], | ||
[E_PARSE], | ||
[E_NOTICE], | ||
[E_CORE_ERROR], | ||
[E_CORE_WARNING], | ||
[E_COMPILE_ERROR], | ||
[E_COMPILE_WARNING], | ||
[E_USER_ERROR], | ||
[E_USER_WARNING], | ||
[E_USER_NOTICE], | ||
[E_STRICT], | ||
[E_RECOVERABLE_ERROR], | ||
[E_DEPRECATED], | ||
[E_USER_DEPRECATED], | ||
[E_ALL], | ||
]; | ||
} | ||
|
||
public function testUnregister() | ||
{ | ||
$handler = new ErrorHandler(true, -1); | ||
$handler->register(); | ||
$handler->unregister(); | ||
|
||
$this->assertTrue(FunctionMockErrorHandler::$restored); | ||
} | ||
|
||
/** | ||
* @expectedException \ErrorException | ||
*/ | ||
public function testHandler() | ||
{ | ||
$handler = new ErrorHandler(true, -1); | ||
$handler->handler(1, 'str', 'file', '123'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the moss-framework package | ||
* | ||
* (c) Michal Wachowski <wachowski.michal@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Moss\Kernel; | ||
|
||
class FunctionMockExceptionHandler | ||
{ | ||
public static $handler_callback; | ||
public static $handler_restored = false; | ||
|
||
public static $xdebug_extension = false; | ||
public static $xdebug_var_dump = false; | ||
} | ||
|
||
function set_exception_handler($callback) { FunctionMockExceptionHandler::$handler_callback = $callback; } | ||
|
||
function restore_exception_handler() { FunctionMockExceptionHandler::$handler_restored = true; } | ||
|
||
function extension_loaded() { return FunctionMockExceptionHandler::$xdebug_extension; } | ||
|
||
function var_dump($var) { echo $var; } | ||
|
||
function headers_sent() { return false; } | ||
|
||
function header($header) { echo $header . PHP_EOL; } | ||
|
||
class ExceptionHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testVerbose() | ||
{ | ||
$handler = new ExceptionHandler(); | ||
$this->assertFalse($handler->verbose()); | ||
|
||
$handler->verbose(true); | ||
$this->assertTrue($handler->verbose()); | ||
} | ||
|
||
public function testRegisterTerse() | ||
{ | ||
$handler = new ExceptionHandler(false); | ||
$handler->register(); | ||
|
||
$this->assertEquals([$handler, 'handlerTerse'], FunctionMockExceptionHandler::$handler_callback); | ||
} | ||
|
||
public function testRegisterVerbose() | ||
{ | ||
$handler = new ExceptionHandler(true); | ||
$handler->register(); | ||
|
||
$this->assertEquals([$handler, 'handlerVerbose'], FunctionMockExceptionHandler::$handler_callback); | ||
} | ||
|
||
public function testUnregister() | ||
{ | ||
$handler = new ExceptionHandler(); | ||
$handler->register(); | ||
$handler->unregister(); | ||
|
||
$this->assertTrue(FunctionMockExceptionHandler::$handler_restored); | ||
} | ||
|
||
public function testTerseHandler() | ||
{ | ||
$exception = new \Exception('Exception message'); | ||
|
||
$handler = new ExceptionHandler(); | ||
$handler->handlerTerse($exception); | ||
|
||
$expected = [ | ||
'HTTP/1.1 500 Internal Server Error', | ||
'Content-type: text/plain; charset=UTF-8', | ||
'Bad Moss: Exception message ( ' . $exception->getFile() . ' at line: ' . $exception->getLine() . ' )' | ||
]; | ||
|
||
$this->expectOutputString(implode(PHP_EOL, $expected)); | ||
} | ||
|
||
public function testVerboseHandler() | ||
{ | ||
$exception = new \Exception('Exception message'); | ||
|
||
$handler = new ExceptionHandler(); | ||
$handler->handlerTerse($exception); | ||
|
||
$this->expectOutputRegex('/HTTP\/1.1 500 Internal Server Error/'); | ||
$this->expectOutputRegex('/Content-type: text\/plain; charset=UTF-8/'); | ||
$this->expectOutputRegex('/Bad Moss: Exception message \( ' . preg_quote($exception->getFile()) . ' at line: ' . $exception->getLine() . ' \)/'); | ||
} | ||
|
||
public function testLineNumbers() | ||
{ | ||
$handler = new ExceptionHandler(); | ||
$result = $handler->lineNumbers(',', 'a,b,c,d', 3); | ||
|
||
$expected = '<table><tr><td><span >1</span>,<span >2</span>,<span id="mark">3</span>,<span >4</span></td><td>a,b,c,d</td></tr></table>'; | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testPrettyCodeWithXdebug() | ||
{ | ||
FunctionMockExceptionHandler::$xdebug_extension = true; | ||
|
||
$handler = new ExceptionHandler(); | ||
$result = $handler->prettyCode('var'); | ||
|
||
$this->assertEquals('var', $result); | ||
} | ||
|
||
public function testPrettyCodeWithoutXdebug() | ||
{ | ||
FunctionMockExceptionHandler::$xdebug_extension = false; | ||
|
||
$handler = new ExceptionHandler(); | ||
$result = $handler->prettyCode('var'); | ||
|
||
$this->assertEquals('<pre>var</pre>', $result); | ||
} | ||
} |