Skip to content

Commit

Permalink
Optimise pest test scripts. Remove meaningless process scripts (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
zds-s authored Mar 23, 2024
1 parent cae1fc3 commit 850c936
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 139 deletions.
84 changes: 0 additions & 84 deletions .github/workflows/codeql.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .travis/run.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ php bin/hyperf.php db:seed --path=app/System/Database/Seeders

php bin/hyperf.php mine:update

composer test
./vendor/bin/pest --parallel
1 change: 1 addition & 0 deletions app/Setting/Controller/CommonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CommonController extends MineController
#[GetMapping('getModuleList')]
public function getModuleList(): ResponseInterface
{
$this->mine->scanModule();
return $this->success($this->mine->getModuleInfo());
}
}
10 changes: 5 additions & 5 deletions config/autoload/mineadmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
'excel_drive' => 'auto',
// 是否启用 远程通用列表查询 功能
'remote_api_enabled' => true,
'http' => [
'headers' => [
'Server' => 'MineAdmin'
]
]
'http' => [
'headers' => [
'Server' => 'MineAdmin',
],
],
];
33 changes: 33 additions & 0 deletions tests/Expectations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
expect()->extend('toBeHttpSuccess', function () {
return $this->toBeArray()
->toHaveKey('requestId')
->toHaveKey('success')
->toHaveKey('message')
->toHaveKey('code')
->toHaveKey('data')
->and($this->value['code'])
->toEqual(200);
});

expect()->extend('toBeHttpFail', function () {
return $this->toBeArray()
->toHaveKey('requestId')
->toHaveKey('success')
->toHaveKey('message')
->toHaveKey('code')
->and($this->value['success'])
->toBeFalse()
->and($this->value['code'] !== 200)
->toBeTrue();
});
1 change: 1 addition & 0 deletions tests/Feature/ModuleTest/Setting/Modules/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
testSuccessResponse($this->delete($this->prefix . '/delete', [
'name' => 'Demo',
]));
FileSystem::delete(BASE_PATH . '/app/Demo');
});
3 changes: 3 additions & 0 deletions tests/Feature/ModuleTest/Setting/commonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
use Nette\Utils\FileSystem;

test('common test', function () {
FileSystem::delete(BASE_PATH . '/app/Demo');
$result = $this->get('/setting/common/getModuleList');
testSuccessResponse($result);
expect($result['data'])
Expand Down
22 changes: 21 additions & 1 deletion tests/Feature/ModuleTest/System/Logs/LogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\System\Model\SystemOperLog;
use App\System\Model\SystemQueueLog;
use Carbon\Carbon;
use Hyperf\Stringable\Str;

beforeEach(function () {
$this->prefix = '/system/logs';
Expand Down Expand Up @@ -41,6 +42,17 @@
testSuccessResponse($this->delete($this->prefix . '/deleteOperLog', [
'ids' => array_column(SystemOperLog::query()->select(['id'])->get()->toArray(), 'id'),
]));
SystemQueueLog::create([
'exchange_name' => 'xxx',
'routing_key_name' => 'xxx',
'queue_name' => 'xxx',
'queue_content' => 'xxx',
'log_content' => 'xxxx',
'produce_status' => 1,
'consume_status' => 2,
'delay_time' => 3,
]);

SystemQueueLog::create([
'exchange_name' => 'xxx',
'routing_key_name' => 'xxx',
Expand All @@ -52,8 +64,16 @@
'delay_time' => 3,
]);
testSuccessResponse($this->delete($this->prefix . '/deleteQueueLog', [
'ids' => array_column(SystemQueueLog::query()->select(['id'])->get()->toArray(), 'id'),
'ids' => array_column(SystemQueueLog::query()->limit(1)->select(['id'])->get()->toArray(), 'id'),
]));
SystemLoginLog::create([
'username' => Str::random(10),
'ip' => '127.0.0.1',
'ip_location' => 'xxx',
'login_time' => Carbon::now(),
'os' => 'xxx',
'browser' => 'xxx',
]);
testSuccessResponse($this->delete($this->prefix . '/deleteLoginLog', [
'ids' => array_column(SystemLoginLog::query()->select(['id'])->get()->toArray(), 'id'),
]));
Expand Down
14 changes: 10 additions & 4 deletions tests/HttpTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

namespace HyperfTests;

use App\System\Model\SystemUser;
use Hyperf\Redis\Redis;
use Hyperf\Testing\Client;
use Hyperf\Testing\Concerns\RunTestsInCoroutine;
use PHPUnit\Framework\TestCase;
use Xmo\JWTAuth\JWT;

/**
* Class HttpTestCase.
Expand Down Expand Up @@ -57,9 +60,12 @@ public function __call($name, $arguments)

public function getBearToken(): ?string
{
return $this->post('/system/login', [
'username' => $this->username,
'password' => $this->password,
], [])['data']['token'] ?? null;
$jwt = make(JWT::class);
$user = SystemUser::query()->whereKey(env('SUPER_ADMIN', 1))->first();
$token = $jwt->getToken($user->toArray());
$key = sprintf('%sToken:%s', config('cache.default.prefix'), $user->id);
$redis = make(Redis::class);
$redis->set($key, $token);
return $token;
}
}
36 changes: 18 additions & 18 deletions tests/MineControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,38 @@ public function saveAndUpdate(
): int {
$saveUri = $this->prefix . '/' . $uris[0];
foreach ($failParams as $param) {
testFailResponse($this->post($saveUri, $param));
expect($this->post($saveUri, $param))->toBeHttpFail();
}
$result = $this->post($saveUri, $successParams);
testSuccessResponse($result);
expect($result)->toBeHttpSuccess();
$id = Arr::get($result, 'data.id');
$updateUri = $this->prefix . '/' . $uris[1] . '/' . $id;
testSuccessResponse($this->put($updateUri, $updateSuccessParams));
expect($this->put($updateUri, $updateSuccessParams))->toBeHttpSuccess();
foreach ($updateFailParams as $param) {
testFailResponse($this->put($updateUri, $param));
expect($this->put($updateUri, $param))->toBeHttpFail();
}
return $id;
}

public function remoteTest(string $uri = 'remote')
{
testSuccessResponse($this->post($this->prefix . '/' . $uri));
expect($this->post($this->prefix . '/' . $uri))->toBeHttpSuccess();
}

public function recoveryAndDeleteTest(array $ids, array $uris = ['delete', 'realDelete', 'recovery'])
{
foreach ($uris as $url) {
if ($url === 'recovery') {
testSuccessResponse($this->put($this->prefix . '/' . $url, compact('ids')));
expect($this->put($this->prefix . '/' . $url, compact('ids')))->toBeHttpSuccess();
} else {
testSuccessResponse($this->delete($this->prefix . '/' . $url, compact('ids')));
expect($this->delete($this->prefix . '/' . $url, compact('ids')))->toBeHttpSuccess();
}
}
}

public function changeStatusTest(int|string $id, int $status = 1, string $uri = 'changeStatus')
{
testSuccessResponse($this->put($this->prefix . '/' . $uri, compact('id', 'status')));
expect($this->put($this->prefix . '/' . $uri, compact('id', 'status')))->toBeHttpSuccess();
}

public function getNoParamsTest(string $route, ?\Closure $customer = null): void
Expand All @@ -92,60 +92,60 @@ public function getNoParamsTest(string $route, ?\Closure $customer = null): void
if ($customer !== null) {
$customer($result);
} else {
testSuccessResponse($result);
expect($result)->toBeHttpSuccess();
}
}

public function numberOperationTest(int|string $id, string $numberName = 'created_by', int $numberValue = 1, string $uri = 'numberOperation')
{
testSuccessResponse($this->put($this->prefix . '/' . $uri, compact('id', 'numberName', 'numberValue')));
expect($this->put($this->prefix . '/' . $uri, compact('id', 'numberName', 'numberValue')))->toBeHttpSuccess();
}

public function getParamsMockTest(string $route, array|\Closure $customer = null, ?\Closure $customerTest = null): void
public function getParamsMockTest(string $route, null|array|\Closure $customer = null, ?\Closure $customerTest = null): void
{
$uri = $this->getUri($route);
$params = $customer instanceof \Closure ? $customer() : $customer;
$result = $this->get($uri, $params);
if ($customerTest !== null) {
$customerTest($result);
} else {
testSuccessResponse($result);
expect($result)->toBeHttpSuccess();
}
}

public function postParamsMockTest(string $route, array|\Closure $customer = null, ?\Closure $customerTest = null): void
public function postParamsMockTest(string $route, null|array|\Closure $customer = null, ?\Closure $customerTest = null): void
{
$uri = $this->getUri($route);
$params = $customer instanceof \Closure ? $customer() : $customer;
$result = $this->post($uri, $params);
if ($customerTest !== null) {
$customerTest($result);
} else {
testSuccessResponse($result);
expect($result)->toBeHttpSuccess();
}
}

public function delParamsMockTest(string $route, array|\Closure $customer = null, ?\Closure $customerTest = null): void
public function delParamsMockTest(string $route, null|array|\Closure $customer = null, ?\Closure $customerTest = null): void
{
$uri = $this->getUri($route);
$params = $customer instanceof \Closure ? $customer() : $customer;
$result = $this->delete($uri, $params);
if ($customerTest !== null) {
$customerTest($result);
} else {
testSuccessResponse($result);
expect($result)->toBeHttpSuccess();
}
}

public function putParamsMockTest(string $route, array|\Closure $customer = null, ?\Closure $customerTest = null): void
public function putParamsMockTest(string $route, null|array|\Closure $customer = null, ?\Closure $customerTest = null): void
{
$uri = $this->getUri($route);
$params = $customer instanceof \Closure ? $customer() : $customer;
$result = $this->put($uri, $params);
if ($customerTest !== null) {
$customerTest($result);
} else {
testSuccessResponse($result);
expect($result)->toBeHttpSuccess();
}
}

Expand Down
Loading

0 comments on commit 850c936

Please sign in to comment.