Skip to content

Commit

Permalink
test(response): add unit tests for fail response with different statu…
Browse files Browse the repository at this point in the history
…s codes
  • Loading branch information
jiannei committed Oct 30, 2024
1 parent a42871f commit eaabfec
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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
};
}
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

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

if (!$show) {
if (! $show) {
$item = Arr::except($item, $key);
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Fail200Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Jiannei\Response\Laravel\Support\Facades\Response;

test('fail with 200 status code', function () {
$response = Response::fail();

expect($response->status())->toEqual(200)
->and($response->getData(true))->toMatchArray([
'status' => 'fail',
'code' => 500,
'message' => '',
'data' => [],
'error' => [],
]);
});
16 changes: 16 additions & 0 deletions tests/Unit/Fail500Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Jiannei\Response\Laravel\Support\Facades\Response;

test('fail with 500 status code', function () {
$response = Response::fail();

expect($response->status())->toEqual(500)
->and($response->getData(true))->toMatchArray([
'status' => 'fail',
'code' => 500,
'message' => '',
'data' => [],
'error' => [],
]);
});

0 comments on commit eaabfec

Please sign in to comment.