php-censor/tests/src/HttpExceptionTest.php

77 lines
2.3 KiB
PHP
Raw Normal View History

2016-04-12 19:31:39 +02:00
<?php
2018-03-04 11:14:09 +01:00
namespace Tests\PHPCensor;
2016-04-12 19:31:39 +02:00
2018-03-04 11:14:09 +01:00
use PHPCensor\Exception\HttpException;
2016-04-12 19:31:39 +02:00
class HttpExceptionTest extends \PHPUnit\Framework\TestCase
2016-04-12 19:31:39 +02:00
{
2016-04-21 19:06:58 +02:00
public function testHttpExceptionIsException()
{
$ex = new HttpException();
2018-02-16 10:41:56 +01:00
self::assertTrue($ex instanceof \Exception);
2016-04-21 19:06:58 +02:00
}
2016-04-12 19:31:39 +02:00
2016-04-21 19:06:58 +02:00
public function testHttpException()
{
try {
throw new HttpException('Test');
} catch (HttpException $ex) {
2018-02-16 10:41:56 +01:00
self::assertTrue($ex->getMessage() == 'Test');
self::assertTrue($ex->getErrorCode() == 500);
self::assertTrue($ex->getStatusMessage() == 'Internal Server Error');
self::assertTrue($ex->getHttpHeader() == 'HTTP/1.1 500 Internal Server Error');
2016-04-21 19:06:58 +02:00
}
}
2016-04-12 19:31:39 +02:00
2016-04-21 19:06:58 +02:00
public function testBadRequestException()
{
try {
throw new HttpException\BadRequestException('Test');
} catch (HttpException $ex) {
2018-02-16 10:41:56 +01:00
self::assertTrue($ex->getErrorCode() == 400);
self::assertTrue($ex->getStatusMessage() == 'Bad Request');
2016-04-21 19:06:58 +02:00
}
}
2016-04-12 19:31:39 +02:00
2016-04-21 19:06:58 +02:00
public function testForbiddenException()
{
try {
throw new HttpException\ForbiddenException('Test');
} catch (HttpException $ex) {
2018-02-16 10:41:56 +01:00
self::assertTrue($ex->getErrorCode() == 403);
self::assertTrue($ex->getStatusMessage() == 'Forbidden');
2016-04-21 19:06:58 +02:00
}
}
2016-04-12 19:31:39 +02:00
2016-04-21 19:06:58 +02:00
public function testNotAuthorizedException()
{
try {
throw new HttpException\NotAuthorizedException('Test');
} catch (HttpException $ex) {
2018-02-16 10:41:56 +01:00
self::assertTrue($ex->getErrorCode() == 401);
self::assertTrue($ex->getStatusMessage() == 'Not Authorized');
2016-04-21 19:06:58 +02:00
}
}
2016-04-12 19:31:39 +02:00
2016-04-21 19:06:58 +02:00
public function testNotFoundException()
{
try {
throw new HttpException\NotFoundException('Test');
} catch (HttpException $ex) {
2018-02-16 10:41:56 +01:00
self::assertTrue($ex->getErrorCode() == 404);
self::assertTrue($ex->getStatusMessage() == 'Not Found');
2016-04-21 19:06:58 +02:00
}
}
2016-04-12 19:31:39 +02:00
2016-04-21 19:06:58 +02:00
public function testServerErrorException()
{
try {
throw new HttpException\ServerErrorException('Test');
} catch (HttpException $ex) {
2018-02-16 10:41:56 +01:00
self::assertTrue($ex->getErrorCode() == 500);
self::assertTrue($ex->getStatusMessage() == 'Internal Server Error');
2016-04-21 19:06:58 +02:00
}
}
2018-02-16 10:41:56 +01:00
}