From 618f12224ea1fc7068e447eb7d08591c369b4e04 Mon Sep 17 00:00:00 2001 From: Anton Terekhov Date: Sun, 11 Nov 2012 18:31:57 +0400 Subject: [PATCH] Revorked Error404Exception . New ErrorHTTPException introduced. --- exception/Error404Exception.php | 9 ++- exception/ErrorHTTPException.php | 39 +++++++++++ tests/app/CliControllerTest.php | 1 + tests/app/FrontControllerTest.php | 1 + tests/exception/Error404ExceptionTest.php | 11 ++- tests/exception/ErrorHTTPExceptionTest.php | 106 +++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 exception/ErrorHTTPException.php create mode 100644 tests/exception/ErrorHTTPExceptionTest.php diff --git a/exception/Error404Exception.php b/exception/Error404Exception.php index b6690fe..454b500 100644 --- a/exception/Error404Exception.php +++ b/exception/Error404Exception.php @@ -5,8 +5,11 @@ * @package Majestic * @subpackage exception * @since 2010-02-26 - * @version SVN: $Id$ - * @filesource $URL$ */ -class Error404Exception extends GeneralException {} \ No newline at end of file +class Error404Exception extends ErrorHTTPException { + function __construct($message = '', $code = null, Exception $previous = NULL ) + { + parent::__construct($message, 404, $previous); + } +} \ No newline at end of file diff --git a/exception/ErrorHTTPException.php b/exception/ErrorHTTPException.php new file mode 100644 index 0000000..0a52257 --- /dev/null +++ b/exception/ErrorHTTPException.php @@ -0,0 +1,39 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage exception + * @since 2012-11-11 + */ + +class ErrorHTTPException extends GeneralException +{ + protected $http_headers; + + public function __construct($message = "", $code = 0, Exception $previous = null) + { + $this->http_headers = array( + 400 => 'HTTP/1.0 400 Bad Request', + 402 => 'HTTP/1.0 402 Payment Required', + 403 => 'HTTP/1.0 403 Forbidden', + 404 => 'HTTP/1.0 404 Not Found', + 410 => 'HTTP/1.0 410 Gone', + 500 => 'HTTP/1.0 500 Internal Server Error', + 501 => 'HTTP/1.0 501 Not Implemented', + 503 => 'HTTP/1.0 503 Service Unavailable', + ); + + if ($code === 200) { + throw new GeneralException('Can\'t send 200 via ErrorHTTPException', 0, $this); + } elseif (!array_key_exists($code, $this->http_headers)) { + throw new GeneralException('Incorrect HTTP code ' . $code . '.', 0, $this); + } + parent::__construct($message, $code, $previous); + } + + public function getHTTPHeader() + { + return $this->http_headers[$this->code]; + } +} \ No newline at end of file diff --git a/tests/app/CliControllerTest.php b/tests/app/CliControllerTest.php index 7e6f719..bfbabfb 100644 --- a/tests/app/CliControllerTest.php +++ b/tests/app/CliControllerTest.php @@ -10,6 +10,7 @@ require_once __DIR__ . '/../../util/profiler/Profiler.php'; require_once __DIR__ . '/../../exception/GeneralException.php'; +require_once __DIR__ . '/../../exception/ErrorHTTPException.php'; require_once __DIR__ . '/../../exception/Error404Exception.php'; require_once __DIR__ . '/../../exception/ErrorHandler.php'; require_once __DIR__ . '/../../app/CliController.php'; diff --git a/tests/app/FrontControllerTest.php b/tests/app/FrontControllerTest.php index 426cdfa..0385889 100644 --- a/tests/app/FrontControllerTest.php +++ b/tests/app/FrontControllerTest.php @@ -17,6 +17,7 @@ require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php'; require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php'; require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; +require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php'; require_once dirname(__FILE__) . '/../../exception/Error404Exception.php'; require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php'; require_once dirname(__FILE__) . '/../../app/router/Route.php'; diff --git a/tests/exception/Error404ExceptionTest.php b/tests/exception/Error404ExceptionTest.php index 1c53fff..8b2a1bf 100644 --- a/tests/exception/Error404ExceptionTest.php +++ b/tests/exception/Error404ExceptionTest.php @@ -11,20 +11,27 @@ */ require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; +require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php'; require_once dirname(__FILE__) . '/../../exception/Error404Exception.php'; class Error404ExceptionTest extends PHPUnit_Framework_TestCase { public function testError404Exception() { - $this->setExpectedException('Error404Exception'); + $this->setExpectedException('Error404Exception', 404); throw new Error404Exception(); } public function testError404ExceptionMessage() { - $this->setExpectedException('Error404Exception', 'Error404Exception message', 10); + $this->setExpectedException('Error404Exception', 'Error404Exception message', 404); throw new Error404Exception('Error404Exception message', 10); } + + public function testError404ExceptionWithPrevious() + { + $this->setExpectedException('Error404Exception', 'Error404Exception message', 404); + throw new Error404Exception('Error404Exception message', 10, new ErrorException('Error message')); + } } \ No newline at end of file diff --git a/tests/exception/ErrorHTTPExceptionTest.php b/tests/exception/ErrorHTTPExceptionTest.php new file mode 100644 index 0000000..5aa418b --- /dev/null +++ b/tests/exception/ErrorHTTPExceptionTest.php @@ -0,0 +1,106 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-10-11 + * + * Unit tests for Error404Exception class + */ + +require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; +require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php'; +require_once dirname(__FILE__) . '/../../exception/Error404Exception.php'; + +class ErrorHTTPExceptionTest extends PHPUnit_Framework_TestCase +{ + public function testErrorHTTPException() + { + $this->setExpectedException('ErrorHTTPException', 404); + throw new ErrorHTTPException('Some error occurred', 400); + } + + public function testErrorHTTPExceptionMessage() + { + $this->setExpectedException('ErrorHTTPException', 'ErrorHTTPException message', 410); + throw new ErrorHTTPException('ErrorHTTPException message', 410); + } + + public function testErrorHTTPExceptionWithPrevious() + { + $this->setExpectedException('ErrorHTTPException', 'ErrorHTTPException message', 500); + throw new ErrorHTTPException('ErrorHTTPException message', 500, new ErrorException('Error message')); + } + + /** + * @dataProvider providerHTTPCode + */ + public function testGetHTTPHeader($message, $code, $header) + { + try { + throw new ErrorHTTPException($message, $code, new ErrorException('Error message')); + } + catch (ErrorHTTPException $e) { + $this->assertSame($header, $e->getHTTPHeader()); + $this->assertSame($message, $e->getMessage()); + $this->assertSame($code, $e->getCode()); + $this->assertEquals(new ErrorException('Error message'), $e->getPrevious()); + } + } + + public function providerHTTPCode() + { + return array( + array('ErrorHTTPException message for 400', 400, 'HTTP/1.0 400 Bad Request'), + array('ErrorHTTPException message for 402', 402, 'HTTP/1.0 402 Payment Required'), + array('ErrorHTTPException message for 403', 403, 'HTTP/1.0 403 Forbidden'), + array('ErrorHTTPException message for 404', 404, 'HTTP/1.0 404 Not Found'), + array('ErrorHTTPException message for 410', 410, 'HTTP/1.0 410 Gone'), + array('ErrorHTTPException message for 500', 500, 'HTTP/1.0 500 Internal Server Error'), + array('ErrorHTTPException message for 501', 501, 'HTTP/1.0 501 Not Implemented'), + array('ErrorHTTPException message for 503', 503, 'HTTP/1.0 503 Service Unavailable'), + ); + } + + public function testErrorHTTPException200() + { + try { + throw new ErrorHTTPException('ErrorHTTPException message', 200, new ErrorException('Error message')); + } + catch (ErrorHTTPException $e) { + $this->fail(); + } + catch (GeneralException $e) { + $this->assertSame('Can\'t send 200 via ErrorHTTPException', $e->getMessage()); + } + } + + public function testErrorHTTPExceptionBadCode() + { + try { + throw new ErrorHTTPException('ErrorHTTPException message', 100, new ErrorException('Error message')); + } + catch (ErrorHTTPException $e) { + $this->fail(); + } + catch (GeneralException $e) { + $this->assertSame('Incorrect HTTP code 100.', $e->getMessage()); + } + } + + public function testErrorHTTPExceptionGoodCode() + { + try { + throw new ErrorHTTPException('ErrorHTTPException message', 400, new ErrorException('Error message')); + } + catch (ErrorHTTPException $e) { + $this->assertSame(400, $e->getCode()); + } + catch (GeneralException $e) { + $this->fail(); + } + } +} +