diff --git a/tests/Exception/Http/HttpTest.php b/tests/Exception/Http/HttpTest.php index 1f34c7958..470437f19 100644 --- a/tests/Exception/Http/HttpTest.php +++ b/tests/Exception/Http/HttpTest.php @@ -6,7 +6,6 @@ use WpOrg\Requests\Exception\Http\Status404; use WpOrg\Requests\Exception\Http\StatusUnknown; use WpOrg\Requests\Tests\TestCase; -use WpOrg\Requests\Utility\HttpStatus; /** * @covers \WpOrg\Requests\Exception\Http @@ -103,45 +102,4 @@ public static function dataGetClass() { ], ]; } - - /** - * Test whether all HTTP exception class can be instantiated. - * - * We're making sure that if such an exception gets triggered, it will not - * fatal because of a broken exception class. - * - * @dataProvider dataInstantiateHttpException - * - * @param int status_code HTTP status code. - * @param string $expected_exception_class Exception class to expect. - * - * @return void - */ - public function testInstantiateHttpException($status_code, $expected_exception_class) { - $exception_class = Http::get_class($status_code); - $exception_object = new $exception_class('testing-1-2-3'); - $this->assertInstanceOf(Http::class, $exception_object); - $this->assertInstanceOf($expected_exception_class, $exception_object); - } - - /** - * Data provider. - * - * @return array - */ - public static function dataInstantiateHttpException() { - $all_status_codes = array_keys(HttpStatus::MAP); - $non_exception_codes = [100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 307, 308]; - - $data = []; - foreach ($all_status_codes as $status) { - $classname = in_array($status, $non_exception_codes, true) - ? StatusUnknown::class - : Http::class . '\Status' . $status; - - $data['Status ' . $status] = [$status, $classname]; - } - - return $data; - } } diff --git a/tests/Exception/Http/StatusCodeTest.php b/tests/Exception/Http/StatusCodeTest.php new file mode 100644 index 000000000..911739e7a --- /dev/null +++ b/tests/Exception/Http/StatusCodeTest.php @@ -0,0 +1,151 @@ +expectException(Http\StatusUnknown::class); + $this->expectExceptionCode($expected_code); + + throw new Http\StatusUnknown('testing-1-2-3', $data); + } + + /** + * Data provider. + * + * @return array + */ + public static function dataUnknownStatusCodes() { + $response_with_status = new Response(); + $response_with_status->status_code = 12345; + + $response_without_status = new Response(); + + return [ + 'null (or not passed)' => [ + 'expectedCode' => 0, + ], + 'integer error code as data' => [ + 'expectedCode' => 0, + 'data' => 507, + ], + 'Response object with status code' => [ + 'expectedCode' => 12345, + 'data' => $response_with_status, + ], + 'Response object without status code' => [ + 'expectedCode' => 0, + 'data' => $response_without_status, + ], + ]; + } + + /** + * Test whether all HTTP exception classes can be instantiated for known status codes. + * + * We're making sure that if such an exception gets triggered, it will not + * fatal because of a broken exception class. + * + * @dataProvider dataKnownStatusCodes + * + * @param int status_code HTTP status code. + * @param string $expected_exception_class Exception class to expect. + * + * @return void + */ + public function testKnownStatusCodes($status_code, $expected_exception_class) { + $response_with_status = new Response(); + $response_with_status->status_code = $status_code; + + $exception_class = Http::get_class($status_code); + $exception_object = new $exception_class('testing-1-2-3'); + $this->assertInstanceOf(Http::class, $exception_object); + $this->assertInstanceOf($expected_exception_class, $exception_object); + } + + /** + * Data provider. + * + * @return array + */ + public static function dataKnownStatusCodes() { + $all_status_codes = array_keys(HttpStatus::MAP); + $non_exception_codes = [100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 307, 308]; + + $data = []; + foreach ($all_status_codes as $status) { + $classname = in_array($status, $non_exception_codes, true) + ? Http\StatusUnknown::class + : Http::class . '\Status' . $status; + + $data['Status ' . $status] = [$status, $classname]; + } + + return $data; + } +} diff --git a/tests/Exception/Http/StatusUnknownTest.php b/tests/Exception/Http/StatusUnknownTest.php deleted file mode 100644 index d7bd9963a..000000000 --- a/tests/Exception/Http/StatusUnknownTest.php +++ /dev/null @@ -1,60 +0,0 @@ -expectException(StatusUnknown::class); - $this->expectExceptionCode($expected_code); - - throw new StatusUnknown('testing-1-2-3', $data); - } - - /** - * Data provider. - * - * @return array - */ - public static function dataException() { - $response_with_status = new Response(); - $response_with_status->status_code = 12345; - - $response_without_status = new Response(); - - return [ - 'null (or not passed)' => [ - 'expectedCode' => 0, - ], - 'integer error code as data' => [ - 'expectedCode' => 0, - 'data' => 507, - ], - 'Response object with status code' => [ - 'expectedCode' => 12345, - 'data' => $response_with_status, - ], - 'Response object without status code' => [ - 'expectedCode' => 0, - 'data' => $response_without_status, - ], - ]; - } -}