Skip to content

Commit

Permalink
handlers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
potfur committed Mar 21, 2015
1 parent 9b2fe6e commit 1c98b53
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 29 deletions.
24 changes: 13 additions & 11 deletions Moss/Kernel/ExceptionHandler.php
Expand Up @@ -20,7 +20,7 @@
class ExceptionHandler
{

private $details;
private $details = false;

/**
* Constructor
Expand All @@ -37,13 +37,15 @@ public function __construct($verbose = false)
*
* @param bool $verbose
*
* @return $this
* @return bool
*/
public function verbose($verbose = false)
public function verbose($verbose = null)
{
$this->details = (bool) $verbose;
if ($verbose !== null) {
$this->details = (bool) $verbose;
}

return $this;
return $this->details;
}

/**
Expand Down Expand Up @@ -75,7 +77,7 @@ public function handlerTerse(\Exception $exception)
header('HTTP/1.1 500 Internal Server Error', true, 500);
header('Content-type: text/plain; charset=UTF-8');
}
echo sprintf('Bad Moss: %s ( %s at line:%s )', $exception->getMessage(), $exception->getFile(), $exception->getLine());
echo sprintf('Bad Moss: %s ( %s at line: %u )', $exception->getMessage(), $exception->getFile(), $exception->getLine());
}

/**
Expand Down Expand Up @@ -135,8 +137,8 @@ public function handlerVerbose(\Exception $exception)
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
$this->lineNum('<br />', highlight_file($exception->getFile(), true), $exception->getLine()),
$this->colorify($exception->getTrace())
$this->lineNumbers('<br />', highlight_file($exception->getFile(), true), $exception->getLine()),
$this->prettyCode($exception->getTrace())
);
}

Expand All @@ -149,7 +151,7 @@ public function handlerVerbose(\Exception $exception)
*
* @return string
*/
protected function lineNum($lineSeparator, $source, $mark = null)
public function lineNumbers($lineSeparator, $source, $mark = null)
{
$count = count(explode($lineSeparator, $source));
$tpl = '<span %s>%u</span>';
Expand All @@ -171,12 +173,12 @@ protected function lineNum($lineSeparator, $source, $mark = null)
*
* @return string
*/
public function colorify($var)
public function prettyCode($var)
{
ob_start();

if (extension_loaded('xdebug')) {
xdebug_var_dump($var);
var_dump($var);
} else {
echo '<pre>';
var_dump($var);
Expand Down
36 changes: 18 additions & 18 deletions tests/Moss/Http/Session/SessionTest.php
@@ -1,7 +1,7 @@
<?php
namespace Moss\Http\Session;

class MockContainer
class FunctionMockSession
{
public static $ini;
public static $headersSent;
Expand All @@ -11,21 +11,21 @@ class MockContainer
public static $sessionId;
}

function ini_get($varname) { return isset(MockContainer::$ini[$varname]) ? MockContainer::$ini[$varname] : null; }
function ini_get($varname) { return isset(FunctionMockSession::$ini[$varname]) ? FunctionMockSession::$ini[$varname] : null; }

function headers_sent(&$file = null, &$line = null) { return MockContainer::$headersSent; }
function headers_sent(&$file = null, &$line = null) { return FunctionMockSession::$headersSent; }

function session_status() { return MockContainer::$sessionStatus; }
function session_status() { return FunctionMockSession::$sessionStatus; }

function session_start() { return MockContainer::$sessionStart; }
function session_start() { return FunctionMockSession::$sessionStart; }

function session_id($id = null)
{
if ($id) {
MockContainer::$sessionId = $id;
FunctionMockSession::$sessionId = $id;
}

return MockContainer::$sessionId;
return FunctionMockSession::$sessionId;
}

;
Expand All @@ -45,10 +45,10 @@ class SessionTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
MockContainer::$headersSent = false;
MockContainer::$sessionStatus = \PHP_SESSION_NONE;
MockContainer::$sessionStart = true;
MockContainer::$sessionId = 'SessionId';
FunctionMockSession::$headersSent = false;
FunctionMockSession::$sessionStatus = \PHP_SESSION_NONE;
FunctionMockSession::$sessionStart = true;
FunctionMockSession::$sessionId = 'SessionId';
}

/**
Expand All @@ -57,8 +57,8 @@ public function setUp()
*/
public function testSessionAlreadyStarted()
{
MockContainer::$sessionId = null;
MockContainer::$sessionStatus = \PHP_SESSION_ACTIVE;
FunctionMockSession::$sessionId = null;
FunctionMockSession::$sessionStatus = \PHP_SESSION_ACTIVE;

new Session();
}
Expand All @@ -69,9 +69,9 @@ public function testSessionAlreadyStarted()
*/
public function testHeadersSent()
{
MockContainer::$sessionId = null;
MockContainer::$ini['session.use_cookies'] = true;
MockContainer::$headersSent = true;
FunctionMockSession::$sessionId = null;
FunctionMockSession::$ini['session.use_cookies'] = true;
FunctionMockSession::$headersSent = true;

new Session();
}
Expand All @@ -82,8 +82,8 @@ public function testHeadersSent()
*/
public function testUnableToStartSession()
{
MockContainer::$sessionId = null;
MockContainer::$sessionStart = false;
FunctionMockSession::$sessionId = null;
FunctionMockSession::$sessionStart = false;

new Session();
}
Expand Down
87 changes: 87 additions & 0 deletions tests/Moss/Kernel/ErrorHandlerTest.php
@@ -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');
}
}
128 changes: 128 additions & 0 deletions tests/Moss/Kernel/ExceptionHandlerTest.php
@@ -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);
}
}

0 comments on commit 1c98b53

Please sign in to comment.