* @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:----ROcM2YZbv4CxyIAXrE8TxU9tB7UtuHT/1ta3F5HOpJX9PFZBPQX8Q1t8yi5VHaia8FPulCdbHGkgYCoR2Oh09qKHBEl3S7uh+hkXW+XOyxL8Mr2YK3EHN+XtAJwYLwxByCw6/yYGL4e96gPJTTEbjahN6hNIIkEQwWm9Jneqyf1JEJlBIVaZ2bK6tZahTG2ZCygyFhOAb+N1G1mXnwl9SMzQYx8j2WXbocDW2El1ePeVBJYHsvq1k6GdL/R3T0DzdpStBow9YnS06CzGsUjX1vKHGZ6RAq+4fB0rekxMDjTPj3bxfT/hZgZdfVgfEUAEiCaX8zvKqOTbQbisIoXs24uABMqAta9zkqUY1GhOFcair/utFe14YJwVyhhAanZUrU0WANRgS2BRRCmMft6yctZ1q8Q6Oa/APB9vnEpVTjdRLAEYtlL+IdMPE3oCUrsJ420h3mjlO5WgsqsP3pXdB5wvCWuyDfGY749oND9PhBrAepJ2FfEF7/Q7TdQ2e0ilsZOj1zf5qZh4DgHIIJS5xOOOPLfuYQuxKWQLq4MwoKnigNXYaERrdQCc2Oz1X7jQOJpdwSzm0OMquSDWhVOS2Rv2tqLJpVv4Ov5QFrol4qZ9i748PCLSVTKpIF8K/+Dwl+yN2999FECMqAI2Jh5cparyDquN798b9lfB05hsiBc=----ATTACHMENT:----MzQ4NTQ0MDQ0NjY2MjIyMSAyMTQ2NDkyODI2MjczMjIgNzYyMDY2MTYzNDEwMzI1OA==