test(pay): 添加账户信息查询功能测试

- 新增 QueryAccountInformationTest 以测试账户信息查询功能
- 实现 getAccount 方法的单元测试
- 添加测试引导文件和 HTTP 测试用例基类
- 更新 composer.json 和 composer.lock 文件
This commit is contained in:
李东云
2025-08-18 14:08:30 +08:00
parent 3d91f76dc0
commit 336cb3a9b9
7 changed files with 146 additions and 23 deletions

View File

@@ -8,6 +8,11 @@
"Singularity\\HDK\\Pay\\": "src/" "Singularity\\HDK\\Pay\\": "src/"
} }
}, },
"autoload-dev": {
"psr-4": {
"Singularity\\HDK\\Test\\Pay\\": "tests/"
}
},
"extra": { "extra": {
"hyperf": { "hyperf": {
"config": "Singularity\\HDK\\Pay\\ConfigProvider" "config": "Singularity\\HDK\\Pay\\ConfigProvider"
@@ -22,7 +27,7 @@
"hyperf/contract": "~3.1.0", "hyperf/contract": "~3.1.0",
"hyperf/di": "~3.1.0", "hyperf/di": "~3.1.0",
"hyperf/guzzle": "3.1.*", "hyperf/guzzle": "3.1.*",
"hyperf/http-server": "3.1.*", "hyperf/http-server": "^3.1",
"moneyphp/money": "^4.1", "moneyphp/money": "^4.1",
"nesbot/carbon": "*", "nesbot/carbon": "*",
"singularity/hdk-core": "^1.0.0" "singularity/hdk-core": "^1.0.0"

2
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "5c422249d0f82f3e0b7725e8763d1b23", "content-hash": "1502e30908a8b64e02da6b75783f3598",
"packages": [ "packages": [
{ {
"name": "carbonphp/carbon-doctrine-types", "name": "carbonphp/carbon-doctrine-types",

View File

@@ -42,19 +42,22 @@ final class AccountBalanceRepo implements AccountRepoInterface
{ {
$response = $this->requestService->requestGet( $response = $this->requestService->requestGet(
url: "/rpc/v2/account/$uid/balance", url: "/rpc/v2/account/$uid/balance",
params: [
'type' => join(',', PointType::values())
],
options: [ options: [
'headers' => [ 'headers' => [
Header::CONTENT_TYPE => 'application/json', Header::CONTENT_TYPE => 'application/json',
Header::ACCEPT_LANGUAGE => $locale?->value ?? config('translation.locale', Languages::EN), Header::ACCEPT_LANGUAGE => $locale?->value ?? config('translation.locale', Languages::EN->value),
'X-SP-ID' => config('app_name'), 'X-SP-ID' => config('app_name'),
'X-SITE' => match (strtoupper(config('site', Site::NorthAmerica))) { 'X-SITE' => (match (strtoupper(config('site', Site::NorthAmerica->value))) {
'CN' => Site::ChinaMainland, 'CN' => Site::ChinaMainland,
default => Site::NorthAmerica, default => Site::NorthAmerica,
}, })->value,
'X-ENV' => match (strtolower(config('app_env', Environment::Dev))) { 'X-ENV' => (match (strtolower(config('app_env', Environment::Dev->value))) {
'prod', 'production' => Environment::Prod, 'prod', 'production' => Environment::Prod,
default => Environment::Dev, default => Environment::Dev,
}, })->value,
], ],
], ],
); );

View File

@@ -0,0 +1,24 @@
<?php
/**
* QueryAccountInformationTest.php@Pay
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2025/8/18
*/
use Singularity\HDK\Pay\Domain\Account\Aggregate\AccountBalance;
use Singularity\HDK\Pay\Infratracture\Repository\AccountBalanceRepo;
use function Hyperf\Support\make;
it('should be able to query account information', function () {
$uid = '123456';
$repo = make(AccountBalanceRepo::class);
$result = $repo->getAccount($uid);
expect($result)
->toBeInstanceOf(AccountBalance::class)
->and($result->getUid())->tobe($uid);
});

53
tests/HttpTestCase.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Singularity\HDK\Test\Pay;
use Hyperf\Testing\Client;
use PHPUnit\Framework\TestCase;
use function Hyperf\Support\make;
/**
* Class HttpTestCase.
* @method get($uri, $data = [], $headers = [])
* @method post($uri, $data = [], $headers = [])
* @method json($uri, $data = [], $headers = [])
* @method file($uri, $data = [], $headers = [])
* @method request($method, $path, $options = [])
*/
abstract class HttpTestCase extends TestCase
{
/**
* @var Client
*/
protected Client $client;
/**
* @inheritDoc
*/
public function __construct(?string $name = null)
{
parent::__construct($name);
$this->client = make(Client::class);
}
/**
* @param string $name
* @param array<string, mixed> $arguments
* @return array<string, mixed>
*/
public function __call(string $name, array $arguments): array
{
return $this->client->{$name}(...$arguments);
}
}

32
tests/Pest.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
use Singularity\HDK\Test\Pay\HttpTestCase;
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your tests functions is always bound to a specific PHPUnit tests
| 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.
|
*/
// uses(HttpTestCase::class)->in('Feature');
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your tests files.
|
*/
/*function something()
{
// ..
}*/

View File

@@ -2,19 +2,16 @@
declare(strict_types=1); declare(strict_types=1);
/** use Hyperf\Config\Config;
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\Context\ApplicationContext; use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\ClassLoader;
use Hyperf\Di\Container; use Hyperf\Di\Container;
use Hyperf\Di\Definition\DefinitionSource; use Hyperf\Di\Definition\DefinitionSource;
use Psr\Container\ContainerInterface; use Swoole\Runtime;
use function Hyperf\Support\env;
use function Hyperf\Support\make;
ini_set('display_errors', 'on'); ini_set('display_errors', 'on');
ini_set('display_startup_errors', 'on'); ini_set('display_startup_errors', 'on');
@@ -22,16 +19,25 @@ ini_set('display_startup_errors', 'on');
error_reporting(E_ALL); error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai'); date_default_timezone_set('Asia/Shanghai');
!defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); !defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
!defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL); !defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);
Swoole\Runtime::enableCoroutine(true); Runtime::enableCoroutine(SWOOLE_HOOK_FLAGS);
require BASE_PATH . '/vendor/autoload.php'; require BASE_PATH . '/vendor/autoload.php';
Hyperf\Di\ClassLoader::init(); ClassLoader::init();
$container = new Container(new DefinitionSource([])); $container = new Container(new DefinitionSource([]));
if (!$container instanceof ContainerInterface) {
throw new RuntimeException('The dependency injection container is invalid.');
}
$container = ApplicationContext::setContainer($container); $container = ApplicationContext::setContainer($container);
/** @var Config $config */
$config = make(Config::class, [
[
'dependencies' => [],
'payment' => [
'base_uri' => env('LUX_PAY_BASE_URI', 'http://test-pay.luxcreo.com'),
],
],
]);
ApplicationContext::getContainer()->set(ConfigInterface::class, $config);