Files
hdk-skeleton/tests/HttpTestCase.php

66 lines
2.0 KiB
PHP
Raw Normal View History

2023-03-21 16:56:42 +08:00
<?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 Tests;
2023-03-21 16:56:42 +08:00
use function Hyperf\Config\config;
2023-03-21 16:56:42 +08:00
use Hyperf\Testing\Client;
use Lmc\HttpConstants\Header;
2023-03-21 16:56:42 +08:00
use PHPUnit\Framework\TestCase;
use Singularity\HDK\Core\Enumerations\Http\Header\RFCs\RFC7617;
2023-03-21 16:56:42 +08:00
use function Hyperf\Support\make;
2023-03-21 16:56:42 +08:00
/**
* Class HttpTestCase.
* @method get($uri, $data = [], $headers = [], $uid = 'TDD_MOCK_PERSONAL_USER_1')
* @method post($uri, $data = [], $headers = [], $uid = 'TDD_MOCK_PERSONAL_USER_1')
* @method put($uri, $data = [], $headers = [], $uid = 'TDD_MOCK_PERSONAL_USER_1')
* @method patch($uri, $data = [], $headers = [], $uid = 'TDD_MOCK_PERSONAL_USER_1')
* @method delete($uri, $data = [], $headers = [], $uid = 'TDD_MOCK_PERSONAL_USER_1')
* @method json($uri, $data = [], $headers = [], $uid = 'TDD_MOCK_PERSONAL_USER_1')
* @method file($uri, $data = [], $headers = [], $uid = 'TDD_MOCK_PERSONAL_USER_1')
2023-03-21 16:56:42 +08:00
* @method request($method, $path, $options = [])
*/
abstract class HttpTestCase extends TestCase
{
protected Client $client;
public function __construct($name = null, array $data = [], $dataName = '')
2023-03-21 16:56:42 +08:00
{
parent::__construct($name, $data, $dataName);
$this->client = make(Client::class);
}
public function __call($name, $arguments)
2023-03-21 16:56:42 +08:00
{
$uri = $arguments[0];
$data = $arguments[1] ?? [];
$headers = $arguments[2] ?? [];
$uid = $arguments[3] ?? 'TDD_MOCK_PERSONAL_USER_1';
$headers[Header::ACCEPT] ??= 'application/json';
$headers[Header::AUTHORIZATION] ??= sprintf(
'%s %s',
RFC7617::BASIC,
base64_encode(
sprintf(
'%s:%s',
$uid,
md5($uid . config('common.token.basic.salt'))
)
)
);
return $this->client->{$name}($uri, $data, $headers);
2023-03-21 16:56:42 +08:00
}
}