From 19bfdf7f86327379e8cf765bb5b96c9881d4ebbb Mon Sep 17 00:00:00 2001 From: jiannei Date: Sat, 14 Oct 2023 03:04:25 +0800 Subject: [PATCH] perf: test fail --- src/Support/Facades/Response.php | 11 +- src/Support/Traits/ExceptionTrait.php | 6 +- src/Support/Traits/JsonResponseTrait.php | 43 ++---- tests/{Repositories => }/Models/User.php | 2 +- .../Resources/UserCollection.php | 2 +- .../Resources/UserResource.php | 2 +- tests/Unit/FailTest.php | 144 ++++++++++++------ tests/Unit/SuccessTest.php | 6 +- tests/database/factories/UserFactory.php | 2 +- 9 files changed, 131 insertions(+), 87 deletions(-) rename tests/{Repositories => }/Models/User.php (93%) rename tests/{Repositories => }/Resources/UserCollection.php (90%) rename tests/{Repositories => }/Resources/UserResource.php (88%) diff --git a/src/Support/Facades/Response.php b/src/Support/Facades/Response.php index c728ff3..e75baf4 100644 --- a/src/Support/Facades/Response.php +++ b/src/Support/Facades/Response.php @@ -21,12 +21,11 @@ * @method static JsonResponse localize(int|\BackedEnum $code = 200) * @method static JsonResponse ok(string $message = '', int|\BackedEnum $code = 200) * @method static JsonResponse success($data = null, string $message = '', int|\BackedEnum $code = 200) - * @method static void errorBadRequest(?string $message = '') - * @method static void errorUnauthorized(string $message = '') - * @method static void errorForbidden(string $message = '') - * @method static void errorNotFound(string $message = '') - * @method static void errorMethodNotAllowed(string $message = '') - * @method static void errorInternal(string $message = '') + * @method static JsonResponse errorBadRequest(?string $message = '') + * @method static JsonResponse errorUnauthorized(string $message = '') + * @method static JsonResponse errorForbidden(string $message = '') + * @method static JsonResponse errorNotFound(string $message = '') + * @method static JsonResponse errorMethodNotAllowed(string $message = '') * @method static JsonResponse fail(string $message = '', int|\BackedEnum $code = 500, $errors = null) * * @see \Jiannei\Response\Laravel\Response diff --git a/src/Support/Traits/ExceptionTrait.php b/src/Support/Traits/ExceptionTrait.php index 7dae090..06c3f81 100644 --- a/src/Support/Traits/ExceptionTrait.php +++ b/src/Support/Traits/ExceptionTrait.php @@ -36,6 +36,8 @@ protected function prepareJsonResponse($request, $e) // 要求请求头 header 中包含 /json 或 +json,如:Accept:application/json // 或者是 ajax 请求,header 中包含 X-Requested-With:XMLHttpRequest; $exceptionConfig = Arr::get(Config::get('response.exception'), get_class($e)); + + /** @var \Illuminate\Foundation\Exceptions\Handler $this */ $isHttpException = $this->isHttpException($e); $message = $exceptionConfig['message'] ?? ($isHttpException ? $e->getMessage() : 'Server Error'); @@ -43,7 +45,9 @@ protected function prepareJsonResponse($request, $e) $header = $exceptionConfig['header'] ?? ($isHttpException ? $e->getHeaders() : []); $options = $exceptionConfig['options'] ?? (JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); - return Response::fail($message, $code, $this->convertExceptionToArray($e), $header, $options); + return Response::fail($message, $code, $this->convertExceptionToArray($e)) + ->withHeaders($header) + ->setEncodingOptions($options); } /** diff --git a/src/Support/Traits/JsonResponseTrait.php b/src/Support/Traits/JsonResponseTrait.php index a35b34b..751a0fd 100644 --- a/src/Support/Traits/JsonResponseTrait.php +++ b/src/Support/Traits/JsonResponseTrait.php @@ -91,60 +91,55 @@ public function localize(int|\BackedEnum $code = 200): JsonResponse * Return a 400 bad request error. * * @param string $message + * @return JsonResponse */ - public function errorBadRequest(string $message = ''): void + public function errorBadRequest(string $message = ''): JsonResponse { - $this->fail($message, 400); + return $this->fail($message, 400); } /** * Return a 401 unauthorized error. * * @param string $message + * @return JsonResponse */ - public function errorUnauthorized(string $message = ''): void + public function errorUnauthorized(string $message = ''): JsonResponse { - $this->fail($message, 401); + return $this->fail($message, 401); } /** * Return a 403 forbidden error. * * @param string $message + * @return JsonResponse */ - public function errorForbidden(string $message = ''): void + public function errorForbidden(string $message = ''): JsonResponse { - $this->fail($message, 403); + return $this->fail($message, 403); } /** * Return a 404 not found error. * * @param string $message + * @return JsonResponse */ - public function errorNotFound(string $message = ''): void + public function errorNotFound(string $message = ''): JsonResponse { - $this->fail($message, 404); + return $this->fail($message, 404); } /** * Return a 405 method not allowed error. * * @param string $message + * @return JsonResponse */ - public function errorMethodNotAllowed(string $message = ''): void - { - $this->fail($message, 405); - } - - /** - * Return a 500 internal server error. - * - * @param string $message - */ - public function errorInternal(string $message = ''): void + public function errorMethodNotAllowed(string $message = ''): JsonResponse { - $this->fail($message); + return $this->fail($message, 405); } /** @@ -157,13 +152,7 @@ public function errorInternal(string $message = ''): void */ public function fail(string $message = '', int|\BackedEnum $code = 500, $errors = null): JsonResponse { - $response = Format::data(compact('message', 'code', 'errors'))->response(); - - if (is_null($errors)) { - $response->throwResponse(); - } - - return $response; + return Format::data(compact('message', 'code', 'errors'))->response(); } /** diff --git a/tests/Repositories/Models/User.php b/tests/Models/User.php similarity index 93% rename from tests/Repositories/Models/User.php rename to tests/Models/User.php index f7f27e0..b0c5cd8 100644 --- a/tests/Repositories/Models/User.php +++ b/tests/Models/User.php @@ -9,7 +9,7 @@ * with this source code in the file LICENSE. */ -namespace Jiannei\Response\Laravel\Tests\Repositories\Models; +namespace Jiannei\Response\Laravel\Tests\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; diff --git a/tests/Repositories/Resources/UserCollection.php b/tests/Resources/UserCollection.php similarity index 90% rename from tests/Repositories/Resources/UserCollection.php rename to tests/Resources/UserCollection.php index ebf3956..ed4ef45 100644 --- a/tests/Repositories/Resources/UserCollection.php +++ b/tests/Resources/UserCollection.php @@ -9,7 +9,7 @@ * with this source code in the file LICENSE. */ -namespace Jiannei\Response\Laravel\Tests\Repositories\Resources; +namespace Jiannei\Response\Laravel\Tests\Resources; use Illuminate\Http\Resources\Json\ResourceCollection; diff --git a/tests/Repositories/Resources/UserResource.php b/tests/Resources/UserResource.php similarity index 88% rename from tests/Repositories/Resources/UserResource.php rename to tests/Resources/UserResource.php index d72b044..5475afb 100644 --- a/tests/Repositories/Resources/UserResource.php +++ b/tests/Resources/UserResource.php @@ -9,7 +9,7 @@ * with this source code in the file LICENSE. */ -namespace Jiannei\Response\Laravel\Tests\Repositories\Resources; +namespace Jiannei\Response\Laravel\Tests\Resources; use Illuminate\Http\Resources\Json\JsonResource; diff --git a/tests/Unit/FailTest.php b/tests/Unit/FailTest.php index ff8ce27..f32a35f 100644 --- a/tests/Unit/FailTest.php +++ b/tests/Unit/FailTest.php @@ -14,56 +14,108 @@ use Jiannei\Response\Laravel\Tests\Enums\ResponseEnum; test('fail', function () { - try { - // 方式一:Controller 中直接返回失败,这里本质上是通过 JsonResponse 是抛出了一个 HttpResponseException,需要捕获异常后才能拿到真实响应 - // 不需要在前面加 return - Response::fail(); - } catch (HttpResponseException $e) { - $response = $e->getResponse(); - - expect($response->getStatusCode())->toEqual(500) - ->and($response->getContent())->toBeJson(json_encode([ - 'status' => 'fail', - 'code' => 500, - 'message' => '', - 'data' => (object) [], - 'error' => (object) [], - ])); - } + // Controller 中直接返回失败 + $response = Response::fail(); + + expect($response->status())->toEqual(500) + ->and($response->getData(true))->toEqual([ + 'status' => 'fail', + 'code' => 500, + 'message' => '', + 'data' => [], + 'error' => [], + ]); }); test('fail with message', function () { - try { - // 方式二:Controller 中返回指定的 Message - Response::fail('操作失败'); - } catch (HttpResponseException $e) { - $response = $e->getResponse(); - - expect($response->getStatusCode())->toEqual(500) - ->and($response->getContent())->toBeJson(json_encode([ - 'status' => 'fail', - 'code' => 500, - 'message' => '操作失败', - 'data' => (object) [], - 'error' => (object) [], - ])); - } + // Controller 中返回指定的 Message + $response = Response::fail('操作失败'); + + expect($response->status())->toEqual(500) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'fail', + 'code' => 500, + 'message' => '操作失败', + 'data' => [], + 'error' => [], + ]); }); test('fail with custom code and message', function () { - try { - // 方式三:Controller 中返回预先定义的业务错误码和错误描述 - Response::fail(code: ResponseEnum::SERVICE_LOGIN_ERROR); - } catch (HttpResponseException $e) { - $response = $e->getResponse(); - - expect($response->getStatusCode())->toEqual(500) - ->and($response->getContent())->toBeJson(json_encode([ - 'status' => 'fail', - 'code' => ResponseEnum::SERVICE_LOGIN_ERROR->value, // 预期返回指定的业务错误码 - 'message' => ResponseEnum::fromValue(ResponseEnum::SERVICE_LOGIN_ERROR->value)->description(), // 预期根据业务码取相应的错误描述 - 'data' => (object) [], - 'error' => (object) [], - ])); - } + // Controller 中返回预先定义的业务错误码和错误描述 + $response = Response::fail(code: ResponseEnum::SERVICE_LOGIN_ERROR); + + expect($response->status())->toEqual(500) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'fail', + 'code' => 500102, // 预期返回指定的业务错误码 + 'message' => '登录失败', // 预期根据业务码取相应的错误描述 + 'data' => [], + 'error' => [], + ]); +}); + +test('error bad request', function () { + $response = Response::errorBadRequest('非法请求'); + + expect($response->status())->toEqual(400) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'error', + 'code' => 400, + 'message' => '非法请求', + 'data' => [], + 'error' => [], + ]); +}); + +test('error unauthorized', function () { + $response = Response::errorUnauthorized(); + + expect($response->status())->toEqual(401) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'error', + 'code' => 401, + 'message' => '授权失败', + 'data' => [], + 'error' => [], + ]); +}); + +test('error forbidden', function () { + $response = Response::errorForbidden(); + + expect($response->status())->toEqual(403) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'error', + 'code' => 403, + 'message' => '', + 'data' => [], + 'error' => [], + ]); +}); + +test('error not found', function () { + $response = Response::errorNotFound(); + + expect($response->status())->toEqual(404) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'error', + 'code' => 404, + 'message' => '', + 'data' => [], + 'error' => [], + ]); +}); + +test('error method not allowed', function () { + $response = Response::errorMethodNotAllowed(); + + expect($response->status())->toEqual(405) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'error', + 'code' => 405, + 'message' => '', + 'data' => [], + 'error' => [], + ]); }); diff --git a/tests/Unit/SuccessTest.php b/tests/Unit/SuccessTest.php index e298b8d..ff402f8 100644 --- a/tests/Unit/SuccessTest.php +++ b/tests/Unit/SuccessTest.php @@ -13,9 +13,9 @@ use Jiannei\Response\Laravel\Support\Facades\Format; use Jiannei\Response\Laravel\Support\Facades\Response; use Jiannei\Response\Laravel\Tests\Enums\ResponseEnum; -use Jiannei\Response\Laravel\Tests\Repositories\Models\User; -use Jiannei\Response\Laravel\Tests\Repositories\Resources\UserCollection; -use Jiannei\Response\Laravel\Tests\Repositories\Resources\UserResource; +use Jiannei\Response\Laravel\Tests\Models\User; +use Jiannei\Response\Laravel\Tests\Resources\UserCollection; +use Jiannei\Response\Laravel\Tests\Resources\UserResource; uses(\Illuminate\Foundation\Testing\RefreshDatabase::class); diff --git a/tests/database/factories/UserFactory.php b/tests/database/factories/UserFactory.php index 235d2f8..e866a74 100644 --- a/tests/database/factories/UserFactory.php +++ b/tests/database/factories/UserFactory.php @@ -12,7 +12,7 @@ namespace Jiannei\Response\Laravel\Tests\database\factories; use Illuminate\Database\Eloquent\Factories\Factory; -use Jiannei\Response\Laravel\Tests\Repositories\Models\User; +use Jiannei\Response\Laravel\Tests\Models\User; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>