Skip to content

Commit

Permalink
perf: test fail
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 13, 2023
1 parent d468bd4 commit 19bfdf7
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 87 deletions.
11 changes: 5 additions & 6 deletions src/Support/Facades/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Support/Traits/ExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ 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');
$code = $exceptionConfig['code'] ?? ($isHttpException ? $e->getStatusCode() : 500);
$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);
}

/**
Expand Down
43 changes: 16 additions & 27 deletions src/Support/Traits/JsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
144 changes: 98 additions & 46 deletions tests/Unit/FailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
]);
});
6 changes: 3 additions & 3 deletions tests/Unit/SuccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 19bfdf7

Please sign in to comment.