diff --git a/config/response.php b/config/response.php index ff466cc..b571b63 100644 --- a/config/response.php +++ b/config/response.php @@ -25,7 +25,7 @@ 'error_code' => false, // lang/zh_CN/enums.php - 'locale' => 'enums',// enums.\Jiannei\Enum\Laravel\Support\Enums\HttpStatusCode::class + 'locale' => 'enums', // enums.\Jiannei\Enum\Laravel\Support\Enums\HttpStatusCode::class // You can set some attributes (eg:code/message/header/options) for the exception, and it will override the default attributes of the exception 'exception' => [ diff --git a/src/Support/Format.php b/src/Support/Format.php index e026d52..d83aac8 100644 --- a/src/Support/Format.php +++ b/src/Support/Format.php @@ -78,7 +78,7 @@ public function data($data, ?string $message, int|\BackedEnum $code, $errors = n }; return $this->formatDataFields([ - 'status' => $this->formatStatus($code,$from), + 'status' => $this->formatStatus($code, $from), 'code' => $this->formatBusinessCode($code), 'message' => $this->formatMessage($code, $message), 'data' => $data ?: (object) $data, @@ -137,14 +137,14 @@ protected function formatMessage(int|\BackedEnum $code, ?string $message): ?stri $localizationKey = join('.', [Config::get('response.locale', 'enums'), $this->formatBusinessCode($code)]); return match (true) { - !$message && Lang::has($localizationKey) => Lang::get($localizationKey), + ! $message && Lang::has($localizationKey) => Lang::get($localizationKey), $code instanceof \BackedEnum && method_exists($code, 'description') => $code->description(), default => $message }; } /** - * Format business code + * Format business code. * * @param int|\BackedEnum $code * @return int @@ -255,7 +255,7 @@ protected function formatDataFields(array $data): array $formatConfig = \config('response.format.config', []); foreach ($formatConfig as $key => $config) { - if (!Arr::has($data, $key)) { + if (! Arr::has($data, $key)) { continue; } @@ -268,7 +268,7 @@ protected function formatDataFields(array $data): array $key = $alias; } - if (!$show) { + if (! $show) { $data = Arr::except($data, $key); } } diff --git a/tests/Enums/ResponseEnum.php b/tests/Enums/ResponseEnum.php index 1c47b1e..72b9316 100644 --- a/tests/Enums/ResponseEnum.php +++ b/tests/Enums/ResponseEnum.php @@ -1,5 +1,14 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace Jiannei\Response\Laravel\Tests\Enums; use Jiannei\Enum\Laravel\Support\Traits\EnumEnhance; @@ -29,4 +38,4 @@ enum ResponseEnum: int case SYSTEM_CACHE_CONFIG_ERROR = 500003; case SYSTEM_CACHE_MISSED_ERROR = 500004; case SYSTEM_CONFIG_ERROR = 500005; -} \ No newline at end of file +} diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php index 61cd84c..bc7a86e 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/ExampleTest.php @@ -1,5 +1,14 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + test('example', function () { expect(true)->toBeTrue(); }); diff --git a/tests/Pest.php b/tests/Pest.php index cc3ee12..09758ae 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,17 +1,15 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ - uses(\Jiannei\Response\Laravel\Tests\TestCase::class)->in('Unit'); +uses(\Jiannei\Response\Laravel\Tests\TestCase::class)->in('Unit'); /* |-------------------------------------------------------------------------- diff --git a/tests/Unit/FailTest.php b/tests/Unit/FailTest.php index ad874f6..ed73554 100644 --- a/tests/Unit/FailTest.php +++ b/tests/Unit/FailTest.php @@ -1,5 +1,14 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Support\Arr; use Jiannei\Response\Laravel\Support\Facades\Response; diff --git a/tests/Unit/FormatTest.php b/tests/Unit/FormatTest.php index 5e577a4..ae16de3 100644 --- a/tests/Unit/FormatTest.php +++ b/tests/Unit/FormatTest.php @@ -1,7 +1,15 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ +use Jiannei\Response\Laravel\Support\Facades\Response; test('add extra field', function () { $response = Response::success(); @@ -9,5 +17,4 @@ expect($response->status())->toEqual(200) ->and($response->getData(true))->toHaveKey('extra') ->and($response->getData(true)['extra'])->toHaveKey('time'); - }); diff --git a/tests/Unit/SuccessTest.php b/tests/Unit/SuccessTest.php index af61e27..3c6c64b 100644 --- a/tests/Unit/SuccessTest.php +++ b/tests/Unit/SuccessTest.php @@ -1,14 +1,21 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + use Illuminate\Support\Arr; -use Jiannei\Enum\Laravel\Support\Enums\HttpStatusCode; 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; - uses(\Illuminate\Foundation\Testing\RefreshDatabase::class); test('success', function () { @@ -45,12 +52,12 @@ expect($response->status())->toEqual(202) ->and($response->getContent())->toBeJson(json_encode([ - 'status' => 'success', - 'code' => 202, - 'message' => '', - 'data' => (object) [], - 'error' => (object) [], - ])); + 'status' => 'success', + 'code' => 202, + 'message' => '', + 'data' => (object) [], + 'error' => (object) [], + ])); }); test('no content', function () { @@ -59,12 +66,12 @@ expect($response->status())->toEqual(204) ->and($response->getContent())->toBeJson(json_encode([ - 'status' => 'success', - 'code' => 204, - 'message' => '', - 'data' => (object) [], - 'error' => (object) [], - ])); + 'status' => 'success', + 'code' => 204, + 'message' => '', + 'data' => (object) [], + 'error' => (object) [], + ])); }); test('success with array data', function () { @@ -140,7 +147,7 @@ expect($response->status())->toEqual(200); - $formatData = Arr::map($users->items(), fn($item) => [ + $formatData = Arr::map($users->items(), fn ($item) => [ 'nickname' => $item->name, 'email' => $item->email, ]); @@ -210,7 +217,7 @@ expect($response->status())->toEqual(200); - $formatData = Arr::map($users->items(), fn($item) => $item->toArray()); + $formatData = Arr::map($users->items(), fn ($item) => $item->toArray()); $data = [ 'data' => $formatData, @@ -247,7 +254,7 @@ expect($response->status())->toEqual(200); - $formatData = Arr::map($users->items(), fn($item) => $item->toArray()); + $formatData = Arr::map($users->items(), fn ($item) => $item->toArray()); $data = [ 'data' => $formatData, @@ -282,7 +289,7 @@ expect($response->status())->toEqual(200); - $formatData = Arr::map($users->items(), fn($item) => $item->toArray()); + $formatData = Arr::map($users->items(), fn ($item) => $item->toArray()); $data = [ 'data' => $formatData, diff --git a/tests/lang/zh_CN/enums.php b/tests/lang/zh_CN/enums.php index de20e9a..3f5895e 100644 --- a/tests/lang/zh_CN/enums.php +++ b/tests/lang/zh_CN/enums.php @@ -13,7 +13,7 @@ use Jiannei\Response\Laravel\Tests\Enums\ResponseEnum; return [ - 200 => '成功',// 直接通过状态码取多语言 + 200 => '成功', // 直接通过状态码取多语言 // 结合 Enum 取多语言 ResponseEnum::class => [