You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.7 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-11
  8. *
  9. * Unit tests for Error404Exception class
  10. */
  11. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  12. require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
  13. require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
  14. class ErrorHTTPExceptionTest extends PHPUnit_Framework_TestCase
  15. {
  16. public function testErrorHTTPException()
  17. {
  18. $this->setExpectedException('ErrorHTTPException', 404);
  19. throw new ErrorHTTPException('Some error occurred', 400);
  20. }
  21. public function testErrorHTTPExceptionMessage()
  22. {
  23. $this->setExpectedException('ErrorHTTPException', 'ErrorHTTPException message', 410);
  24. throw new ErrorHTTPException('ErrorHTTPException message', 410);
  25. }
  26. public function testErrorHTTPExceptionWithPrevious()
  27. {
  28. $this->setExpectedException('ErrorHTTPException', 'ErrorHTTPException message', 500);
  29. throw new ErrorHTTPException('ErrorHTTPException message', 500, new ErrorException('Error message'));
  30. }
  31. /**
  32. * @dataProvider providerHTTPCode
  33. */
  34. public function testGetHTTPHeader($message, $code, $header)
  35. {
  36. try {
  37. throw new ErrorHTTPException($message, $code, new ErrorException('Error message'));
  38. }
  39. catch (ErrorHTTPException $e) {
  40. $this->assertSame($header, $e->getHTTPHeader());
  41. $this->assertSame($message, $e->getMessage());
  42. $this->assertSame($code, $e->getCode());
  43. $this->assertEquals(new ErrorException('Error message'), $e->getPrevious());
  44. }
  45. }
  46. public function providerHTTPCode()
  47. {
  48. return array(
  49. array('ErrorHTTPException message for 400', 400, 'HTTP/1.0 400 Bad Request'),
  50. array('ErrorHTTPException message for 402', 402, 'HTTP/1.0 402 Payment Required'),
  51. array('ErrorHTTPException message for 403', 403, 'HTTP/1.0 403 Forbidden'),
  52. array('ErrorHTTPException message for 404', 404, 'HTTP/1.0 404 Not Found'),
  53. array('ErrorHTTPException message for 410', 410, 'HTTP/1.0 410 Gone'),
  54. array('ErrorHTTPException message for 500', 500, 'HTTP/1.0 500 Internal Server Error'),
  55. array('ErrorHTTPException message for 501', 501, 'HTTP/1.0 501 Not Implemented'),
  56. array('ErrorHTTPException message for 503', 503, 'HTTP/1.0 503 Service Unavailable'),
  57. );
  58. }
  59. public function testErrorHTTPException200()
  60. {
  61. try {
  62. throw new ErrorHTTPException('ErrorHTTPException message', 200, new ErrorException('Error message'));
  63. }
  64. catch (ErrorHTTPException $e) {
  65. $this->fail();
  66. }
  67. catch (GeneralException $e) {
  68. $this->assertSame('Can\'t send 200 via ErrorHTTPException', $e->getMessage());
  69. }
  70. }
  71. public function testErrorHTTPExceptionBadCode()
  72. {
  73. try {
  74. throw new ErrorHTTPException('ErrorHTTPException message', 100, new ErrorException('Error message'));
  75. }
  76. catch (ErrorHTTPException $e) {
  77. $this->fail();
  78. }
  79. catch (GeneralException $e) {
  80. $this->assertSame('Incorrect HTTP code 100.', $e->getMessage());
  81. }
  82. }
  83. public function testErrorHTTPExceptionGoodCode()
  84. {
  85. try {
  86. throw new ErrorHTTPException('ErrorHTTPException message', 400, new ErrorException('Error message'));
  87. }
  88. catch (ErrorHTTPException $e) {
  89. $this->assertSame(400, $e->getCode());
  90. }
  91. catch (GeneralException $e) {
  92. $this->fail();
  93. }
  94. }
  95. }