From eaabfec34998b0f29539fba44ecf1d7fb75ae13a Mon Sep 17 00:00:00 2001 From: jiannei Date: Wed, 30 Oct 2024 17:26:18 +0800 Subject: [PATCH] test(response): add unit tests for fail response with different status codes --- src/Support/Format.php | 34 ++++++++++++++++++---------------- tests/TestCase.php | 8 ++++++++ tests/Unit/Fail200Test.php | 16 ++++++++++++++++ tests/Unit/Fail500Test.php | 16 ++++++++++++++++ 4 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 tests/Unit/Fail200Test.php create mode 100644 tests/Unit/Fail500Test.php diff --git a/src/Support/Format.php b/src/Support/Format.php index 211b012..1b39b98 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -34,9 +34,7 @@ class Format implements ResponseFormat protected int $statusCode = 200; - public function __construct(protected array $config = []) - { - } + public function __construct(protected array $config = []) {} /** * Return a new JSON response from the application. @@ -60,23 +58,25 @@ public function get(): ?array /** * Core format. * - * @param null $data - * @param null $error - * + * @param null $data + * @param null $error * @return Format */ public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static { return tap($this, function () use ($data, $message, $code, $error) { - $this->statusCode = $this->formatStatusCode($this->formatBusinessCode($code), $data); + $bizCode = $this->formatBusinessCode($code); + $this->data = $this->formatDataFields([ - 'status' => $this->formatStatus($this->statusCode), - 'code' => $this->formatBusinessCode($code), - 'message' => $this->formatMessage($this->formatBusinessCode($code), $message), + 'status' => $this->formatStatus($bizCode), + 'code' => $bizCode, + 'message' => $this->formatMessage($bizCode, $message), 'data' => $this->formatData($data), 'error' => $this->formatError($error), ]); + + $this->statusCode = $this->formatStatusCode($bizCode, $error); }); } @@ -133,7 +133,7 @@ protected function formatMessage(int $code, string $message = ''): ?string $localizationKey = implode('.', [Config::get('response.locale', 'enums'), $code]); return match (true) { - !$message && Lang::has($localizationKey) => Lang::get($localizationKey), + ! $message && Lang::has($localizationKey) => Lang::get($localizationKey), default => $message }; } @@ -149,8 +149,10 @@ protected function formatBusinessCode(int|\BackedEnum $code): int /** * Format http status description. */ - protected function formatStatus(int $statusCode): string + protected function formatStatus(int $bizCode): string { + $statusCode = (int) substr($bizCode, 0, 3); + return match (true) { ($statusCode >= 400 && $statusCode <= 499) => 'error',// client error ($statusCode >= 500 && $statusCode <= 599) => 'fail',// service error @@ -161,9 +163,9 @@ protected function formatStatus(int $statusCode): string /** * Http status code. */ - protected function formatStatusCode(int $code, $oriData): int + protected function formatStatusCode(int $code, $errors): int { - return (int) substr(is_null($oriData) ? (Config::get('response.error_code') ?: $code) : $code, 0, 3); + return (int) substr(is_null($errors) ? (Config::get('response.error_code') ?: $code) : $code, 0, 3); } /** @@ -236,7 +238,7 @@ protected function formatDataFields(array $data): array { return tap($data, function (&$item) { foreach ($this->config as $key => $config) { - if (!Arr::has($item, $key)) { + if (! Arr::has($item, $key)) { continue; } @@ -249,7 +251,7 @@ protected function formatDataFields(array $data): array $key = $alias; } - if (!$show) { + if (! $show) { $item = Arr::except($item, $key); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 0299167..4697e57 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -63,5 +63,13 @@ protected function defineEnvironment($app) ], ]); } + + if ($this instanceof \P\Tests\Unit\Fail200Test) { + $app['config']->set('response.error_code', 200); + } + + if ($this instanceof \P\Tests\Unit\Fail500Test) { + $app['config']->set('response.error_code', 500); + } } } diff --git a/tests/Unit/Fail200Test.php b/tests/Unit/Fail200Test.php new file mode 100644 index 0000000..bfe8072 --- /dev/null +++ b/tests/Unit/Fail200Test.php @@ -0,0 +1,16 @@ +status())->toEqual(200) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'fail', + 'code' => 500, + 'message' => '', + 'data' => [], + 'error' => [], + ]); +}); \ No newline at end of file diff --git a/tests/Unit/Fail500Test.php b/tests/Unit/Fail500Test.php new file mode 100644 index 0000000..a84f017 --- /dev/null +++ b/tests/Unit/Fail500Test.php @@ -0,0 +1,16 @@ +status())->toEqual(500) + ->and($response->getData(true))->toMatchArray([ + 'status' => 'fail', + 'code' => 500, + 'message' => '', + 'data' => [], + 'error' => [], + ]); +}); \ No newline at end of file