config->get('error')->fuzeworks_error_reporting = false; Logger::$Logs = array(); } public function testGetLogger() { $this->assertInstanceOf('FuzeWorks\Logger', new Logger); Factory::getInstance()->config->get('error')->php_error_reporting = true; $this->assertInstanceOf('FuzeWorks\Logger', new Logger); } public function testErrorHandler() { Logger::errorHandler(E_ERROR, 'Example error', __FILE__, 1, 'data'); $this->assertCount(1, Logger::$Logs); $log = Logger::$Logs[0]; $this->assertEquals('ERROR', $log['type']); $this->assertEquals('Example error', $log['message']); $this->assertEquals(__FILE__, $log['logFile']); $this->assertEquals(1, $log['logLine']); $this->assertEquals('data', $log['context']); } /** * @depends testErrorHandler */ public function testErrorHandlerTypes() { $types = array( E_ERROR => 'ERROR', E_WARNING => 'WARNING', E_PARSE => 'ERROR', E_NOTICE => 'WARNING', E_CORE_ERROR => 'ERROR', E_CORE_WARNING => 'WARNING', E_COMPILE_ERROR => 'ERROR', E_COMPILE_WARNING => 'WARNING', E_USER_ERROR => 'ERROR', E_USER_WARNING => 'WARNING', E_USER_NOTICE => 'WARNING', E_USER_DEPRECATED => 'WARNING', E_STRICT => 'ERROR', E_RECOVERABLE_ERROR => 'ERROR', E_DEPRECATED => 'WARNING', 'UNKNOWN' => 'Unknown error: UNKNOWN' ); foreach ($types as $errorType => $output) { // Clear the log entries Logger::$Logs = array(); // Log the error Logger::errorHandler($errorType, 'Log message'); // Fetch the error $log = Logger::$Logs[0]; // Check the type $this->assertEquals($output, $log['type']); } } public function testExceptionHandler() { // Create the exception $exception = new LoggerException(); // Log the exception ob_start(); Logger::exceptionHandler($exception); // Check the output $this->assertEquals('

500

Internal Server Error

', ob_get_clean()); // Check the logs $log = Logger::$Logs[0]; } public function testLog() { // Log the message Logger::log('Log message', 'core_loggerTest', __FILE__, 1); // Fetch the message $log = Logger::$Logs[0]; // Assert data $this->assertEquals('INFO', $log['type']); $this->assertEquals('Log message', $log['message']); $this->assertEquals(__FILE__, $log['logFile']); $this->assertEquals(1, $log['logLine']); $this->assertEquals('core_loggerTest', $log['context']); } /** * @depends testLog */ public function testLogTypes() { $types = array( 'newLevel' => 'LEVEL_START', 'stopLevel' => 'LEVEL_STOP', 'logError' => 'ERROR', 'logWarning' => 'WARNING', 'logDebug' => 'DEBUG', 'mark' => 'BMARK' ); foreach ($types as $method => $returnValue) { // Clear the log entries Logger::$Logs = array(); // Log the entry Logger::{$method}('Log message', 'core_loggerTest', __FILE__, 1); // Fetch the entry $log = Logger::$Logs[0]; // Assert the entry $this->assertEquals($returnValue, $log['type']); } } public function testHttpError() { $http_codes = array( 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a teapot', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', 509 => 'Bandwidth Limit Exceeded', 510 => 'Not Extended', 511 => 'Network Authentication Required', ); // Test all error codes foreach ($http_codes as $code => $description) { // Fire the error ob_start(); Logger::http_error($code); // Check the output $this->assertEquals('

'.$code.'

'.$description.'

', ob_get_clean()); } } /** * @depends testHttpError */ public function testHttpErrorWithoutLayout() { $this->assertFalse(Logger::http_error(500, '', false)); } public function testEnableDisable() { // First enable Logger::enable(); $this->assertTrue(Logger::isEnabled()); // Then disable Logger::disable(); $this->assertFalse(Logger::isEnabled()); } public function tearDown() { Logger::disable(); Logger::$Logs = array(); } }