Skip to content

Commit

Permalink
调整单元测试结构,添加路由中间件单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
hongweipeng committed Apr 13, 2024
1 parent f560dc8 commit 2c30dd5
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 29 deletions.
42 changes: 36 additions & 6 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use PHPUnit\Framework\TestCase;

abstract class Base extends TestCase {
static $ROOT_PATH = __DIR__ . "/../vendor/topthink/think";
static $RUNTIME_PATH = __DIR__ . "/../runtime/";
static string $ROOT_PATH = __DIR__ . "/../vendor/topthink/think";
static string $RUNTIME_PATH = __DIR__ . "/../runtime/";

protected $app;
protected $throttle_config = [];
protected $middleware_file = __DIR__ . "/config/global-middleware.php";
protected $middleware_type = 'global';
protected \think\App $app;
protected array $throttle_config = [];
protected string $middleware_file = __DIR__ . "/config/global-middleware.php";
protected string $middleware_type = 'global';

/**
* thinkphp 一般运行在 php-fpm 模式下,每次处理请求都要重新加载配置文件
Expand All @@ -33,6 +33,36 @@ function get_response(\think\Request $request): \think\Response {
return $response;
}

/**
* @param string $uri
* @param string $method
* @param string $host
* @param array $data
* @param array $headers
* @return \think\Request
*/
function create_request($uri = '/', $method = 'GET', $host = '127.0.0.1', $data = [], $headers = []): \think\Request
{
$request = new \think\Request();
$request->setMethod($method);
$request->setHost($host);
$request->setDomain($host);
$request->setUrl(sprintf('http://%s/%s', $host, $uri));

// uri 中提取 path info
$path = strpos($uri, '?') ? strstr($uri, '?', true) : $uri;
$request->setBaseUrl($path);
$path_info = empty($path) || '/' == $path ? '' : ltrim($path, '/');
$request->setPathinfo($path_info);
return $request;
}

function visit_with_http_code(\think\Request $request, int $http_code = 200): bool
{
$response = $this->get_response($request);
return $response->getCode() == $http_code;
}

protected function tearDown(): void
{
parent::tearDown();
Expand Down
8 changes: 2 additions & 6 deletions tests/CustomCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ function visit(int $count): int
{
$allowCount = 0;
for ($i = 0; $i < $count; $i++) {
$request = new \think\Request();
$request->setMethod('GET');
$request->setUrl('/');

$response = $this->get_response($request);
if ($response->getCode() == 200) {
$request = $this->create_request('/');
if ($this->visit_with_http_code($request)) {
$allowCount++;
}
}
Expand Down
60 changes: 60 additions & 0 deletions tests/RouteThrottleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace tests;


/**
* 路由节流器单元测试
* Class ResidentMemoryTest
* @package tests
*/
class RouteThrottleTest extends Base
{
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$config = $this->get_default_throttle_config();
$config['key'] = '__CONTROLLER__/__ACTION__/__IP__';
$this->set_throttle_config($config);
}

public function test_route_middleware_type()
{
$this->middleware_type = 'route'; // 路由中间件
$allowCount1 = 0;
$allowCount2 = 0;
for ($i = 0; $i < 200; $i++) {
$request = $this->create_request('/hello/name');
if ($this->visit_with_http_code($request)) {
$allowCount1++;
}
$request = $this->create_request('/');
if ($this->visit_with_http_code($request)) {
$allowCount2++;
}
}
$this->assertEquals(100, $allowCount1);
$this->assertEquals(100, $allowCount2);
}

public function test_global_middleware_type()
{
$this->middleware_type = 'global'; // 全局中间件
$allowCount1 = 0;
$allowCount2 = 0;
// 默认 100/m ,所以两个路由都是 50 次成功
for ($i = 0; $i < 200; $i++) {
$request = $this->create_request('/hello/name');
if ($this->visit_with_http_code($request)) {
$allowCount1++;
}
$request = $this->create_request('/');
if ($this->visit_with_http_code($request)) {
$allowCount2++;
}
}
$this->assertEquals(50, $allowCount1);
$this->assertEquals(50, $allowCount2);
}
}
16 changes: 4 additions & 12 deletions tests/ThrottleDefaultConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ function test_visit_rate()
// 默认的访问频率为 '100/m'
$allowCount = 0;
for ($i = 0; $i < 200; $i++) {
$request = new \think\Request();
$request->setMethod('GET');
$request->setUrl('/');

$response = $this->get_response($request);
if ($response->getCode() == 200) {
$request = $this->create_request('/');
if ($this->visit_with_http_code($request)) {
$allowCount++;
}
}
Expand All @@ -38,12 +34,8 @@ function test_unlimited_request_method()
// 默认只限制了 ['GET', 'HEAD'] ,对 POST 不做限制
$allowCount = 0;
for ($i = 0; $i < 200; $i++) {
$request = new \think\Request();
$request->setMethod('POST');
$request->setUrl('/');

$response = $this->get_response($request);
if ($response->getCode() == 200) {
$request = $this->create_request('/','POST');
if ($this->visit_with_http_code($request)) {
$allowCount++;
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/VisitRateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function is_visit_allow(string $uri): bool
function test_custom_visit_rate() {
$config = $this->get_default_throttle_config();
$config['key'] = function(Throttle $throttle, \think\Request $request) {
$path = $request->url();
$path = $request->baseUrl();
if ($path === '/path1') {
$throttle->setRate('10/m');
} else if ($path === '/path2') {
Expand All @@ -41,16 +41,16 @@ function test_custom_visit_rate() {
$allowCount2 = 0;
$allowCount3 = 0;
for ($i = 0; $i < 200; $i++) {
if ($this->is_visit_allow('/')) {
if ($this->visit_with_http_code($this->create_request('/'))) {
$allowCount0++;
}
if ($this->is_visit_allow('/path1')) {
if ($this->visit_with_http_code($this->create_request('/path1'), 404)) {
$allowCount1++;
}
if ($this->is_visit_allow('/path2')) {
if ($this->visit_with_http_code($this->create_request('/path2'), 404)) {
$allowCount2++;
}
if ($this->is_visit_allow('/path3')) {
if ($this->visit_with_http_code($this->create_request('/path3'), 404)) {
$allowCount3++;
}
}
Expand Down

0 comments on commit 2c30dd5

Please sign in to comment.