Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot authored and jiannei committed Oct 13, 2023
1 parent fb2fa6f commit 82aeba6
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 38 deletions.
2 changes: 1 addition & 1 deletion config/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
10 changes: 5 additions & 5 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -268,7 +268,7 @@ protected function formatDataFields(array $data): array
$key = $alias;
}

if (!$show) {
if (! $show) {
$data = Arr::except($data, $key);
}
}
Expand Down
11 changes: 10 additions & 1 deletion tests/Enums/ResponseEnum.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
*
* 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;
Expand Down Expand Up @@ -29,4 +38,4 @@ enum ResponseEnum: int
case SYSTEM_CACHE_CONFIG_ERROR = 500003;
case SYSTEM_CACHE_MISSED_ERROR = 500004;
case SYSTEM_CONFIG_ERROR = 500005;
}
}
9 changes: 9 additions & 0 deletions tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
*
* 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();
});
18 changes: 8 additions & 10 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
*
* 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');

/*
|--------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/FailTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
*
* 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;
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/FormatTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<?php

use Jiannei\Response\Laravel\Support\Facades\Response;
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
*
* 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();

expect($response->status())->toEqual(200)
->and($response->getData(true))->toHaveKey('extra')
->and($response->getData(true)['extra'])->toHaveKey('time');

});
43 changes: 25 additions & 18 deletions tests/Unit/SuccessTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?php

/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
*
* 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 () {
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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,
]);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/lang/zh_CN/enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Jiannei\Response\Laravel\Tests\Enums\ResponseEnum;

return [
200 => '成功',// 直接通过状态码取多语言
200 => '成功', // 直接通过状态码取多语言

// 结合 Enum 取多语言
ResponseEnum::class => [
Expand Down

0 comments on commit 82aeba6

Please sign in to comment.