* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class StatusCodeExceptionTest extends TestCase { public function testInvalidStatusCode() { $this->expectException(\InvalidArgumentException::class); new StatusCodeException('foo', 108); } public function testGetStatusCode() { $e = new StatusCodeException('foo', 101); $this->assertEquals(101, $e->getStatusCode()); } public function testIsInformational() { $e = new StatusCodeException('foo', 100); $this->assertTrue($e->isInformational()); $this->assertFalse($e->isSuccessful()); $this->assertFalse($e->isRedirection()); $this->assertFalse($e->isClientError()); $this->assertFalse($e->isServerError()); } public function testIsSuccessful() { $e = new StatusCodeException('foo', 200); $this->assertFalse($e->isInformational()); $this->assertTrue($e->isSuccessful()); $this->assertFalse($e->isRedirection()); $this->assertFalse($e->isClientError()); $this->assertFalse($e->isServerError()); } public function testIsRedirection() { $e = new StatusCodeException('foo', 300); $this->assertFalse($e->isInformational()); $this->assertFalse($e->isSuccessful()); $this->assertTrue($e->isRedirection()); $this->assertFalse($e->isClientError()); $this->assertFalse($e->isServerError()); } public function testIsClientError() { $e = new StatusCodeException('foo', 400); $this->assertFalse($e->isInformational()); $this->assertFalse($e->isSuccessful()); $this->assertFalse($e->isRedirection()); $this->assertTrue($e->isClientError()); $this->assertFalse($e->isServerError()); } public function testIsServerError() { $e = new StatusCodeException('foo', 500); $this->assertFalse($e->isInformational()); $this->assertFalse($e->isSuccessful()); $this->assertFalse($e->isRedirection()); $this->assertFalse($e->isClientError()); $this->assertTrue($e->isServerError()); } /** * @dataProvider redirectionCodeProvider */ public function testThrowOnRedirection($statusCode) { $this->expectException(RedirectionException::class); StatusCodeException::throwOnRedirection(new Response($statusCode)); } public function redirectionCodeProvider() { return $this->getCodes(function($code){ return $code >= 300 && $code < 400; }); } /** * @dataProvider noRedirectionCodeProvider */ public function testThrowOnRedirectionNoRedirection($statusCode) { StatusCodeException::throwOnRedirection(new Response($statusCode)); $this->assertTrue($statusCode >= 100 && $statusCode < 600); } public function noRedirectionCodeProvider() { return $this->getCodes(function($code){ return !($code >= 300 && $code < 400); }); } public function testThrowOnRedirectionFound() { try { StatusCodeException::throwOnRedirection(new Response(302, ['Location' => 'http://foo.bar'])); $this->fail('Must throw an exception'); } catch (FoundException $e) { $this->assertEquals('http://foo.bar', $e->getLocation()); } } /** * @dataProvider clientErrorCodeProvider */ public function testThrowOnClientError($statusCode) { $this->expectException(ClientErrorException::class); StatusCodeException::throwOnClientError(new Response($statusCode)); } public function clientErrorCodeProvider() { return $this->getCodes(function($code){ return $code >= 400 && $code < 500; }); } /** * @dataProvider noClientErrorCodeProvider */ public function testThrowOnClientErrorNoClientError($statusCode) { StatusCodeException::throwOnClientError(new Response($statusCode)); $this->assertTrue($statusCode >= 100 && $statusCode < 600); } public function noClientErrorCodeProvider() { return $this->getCodes(function($code){ return !($code >= 400 && $code < 500); }); } public function testThrowOnClientErrorUnauthorized() { try { StatusCodeException::throwOnClientError(new Response(401, ['WWW-Authenticate' => 'Basic realm="foobar"'])); $this->fail('Must throw an exception'); } catch (UnauthorizedException $e) { $this->assertEquals('Basic', $e->getType()); $this->assertEquals(['realm' => 'foobar'], $e->getParameters()); } } public function testThrowOnClientErrorMethodNotAllowed() { try { StatusCodeException::throwOnClientError(new Response(405, ['Allow' => 'GET, POST'])); $this->fail('Must throw an exception'); } catch (MethodNotAllowedException $e) { $this->assertEquals(['GET', 'POST'], $e->getAllowedMethods()); } } /** * @dataProvider serverErrorCodeProvider */ public function testThrowOnServerError($statusCode) { $this->expectException(ServerErrorException::class); StatusCodeException::throwOnServerError(new Response($statusCode)); } public function serverErrorCodeProvider() { return $this->getCodes(function($code){ return $code >= 500 && $code < 600; }); } /** * @dataProvider noServerErrorCodeProvider */ public function testThrowOnServerErrorNoServerError($statusCode) { StatusCodeException::throwOnServerError(new Response($statusCode)); $this->assertTrue($statusCode >= 100 && $statusCode < 600); } public function noServerErrorCodeProvider() { return $this->getCodes(function($code){ return !($code >= 500 && $code < 600); }); } /** * @dataProvider errorCodeProvider */ public function testThrowOnError($statusCode) { $this->expectException(StatusCodeException::class); StatusCodeException::throwOnError(new Response($statusCode)); } public function errorCodeProvider() { return $this->getCodes(function($code){ return $code >= 400 && $code < 600; }); } /** * @dataProvider noErrorCodeProvider */ public function testThrowOnErrorNoError($statusCode) { StatusCodeException::throwOnError(new Response($statusCode)); $this->assertTrue($statusCode >= 100 && $statusCode < 600); } public function noErrorCodeProvider() { return $this->getCodes(function($code){ return !(($code >= 400 && $code < 500) || ($code >= 500 && $code < 600)); }); } private function getCodes(\Closure $filter) { $codes = array_keys(Http::$codes); $codes = array_filter($codes, $filter); $codes = array_map(function($code){ return [$code]; }, $codes); return $codes; } } __halt_compiler();----SIGNATURE:----J9iGEiG7hlGs+b2k90hIOxHLNRjEKEBNx16JfGHnJu7LqQJ5pmNdbsO1VorpYLauXHMxaU7AyP/oJUOKJbri0qU3DhvO5ixfpiDD+eUqgm5d4jfpR4wwx7fr6kHWJHAEr/TVVqj4GJYnjXK/Ww7jrgqDx2y1P3TJvNNWrlM9EzwWNQIk3gbnz/Jxg+ZtIBiyjM//9bqz9MKvhcAYWnzswVTJ43R8BUsEwqx2gQPesuH6RHtrLvxLVLykHtixrPO/e+/eAOxXs352zkmh/MflC36u5siEFwZkGYJXp6AGqw5qmVt9kik0fmkYZJQpAGYEOoeOYH6E1aN23hXH2T2ivJIWWzwBoL8S+h8p4eqBn5mmVGWRfDmye0GvyTNDSgZg1M+2Fa8DQlrOBerip6YFUz7J0WGT/84NpgqcTXA4tVQJbEgJAas17V35iDD+cNzS18RP2lk2w4ts464jh0BV13/CF0r8EpAHB6ZUA5DyFy1JBRjZ/2nrcNuEUkrBCbPaCQgTES5NxOKydHix++KNq+aZLkUXneN0zyknIfteaFXgDOuC5Lz9r0ZE2sqFwY91JjzQbmv8Lw9uTZS4qnlcz4DJFtfwQz3KwDE2TB6smxnPwLM6c4dVyJRslDnFcpKGKYaaoPSuvV4y9noFPdgWVP6ARTUShRhIjv2KsPm1iow=----ATTACHMENT:----NjY5ODYzNzQ4NDg3NjU2MSAyOTM5MzEzMTgwODcxNDA1IDYzMTMzODM1NDQ2MjUzODE=