Skip to content

Commit f5b9bbc

Browse files
committed
tests: use app call :sparkles
1 parent e1f254d commit f5b9bbc

File tree

6 files changed

+33
-15
lines changed

6 files changed

+33
-15
lines changed

cli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ foreach ($argv as $v) {
6969
}
7070

7171
// 设置cli模式
72-
putenv('IS_CLI=true');
72+
putenv('IS_CLI=yes');
7373
$method = $input['method'];
7474
$method = explode('.', $method);
7575
$input['module'] = $method[0];

framework/App.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ class App
6868
*/
6969
public static $app;
7070

71+
/**
72+
* 是否输出响应结果
73+
*
74+
* 默认输出
75+
*
76+
* cli模式 访问路径为空 不输出
77+
*
78+
* @var boolean
79+
*/
80+
private $notOutput = false;
81+
7182
/**
7283
* 服务容器
7384
*
@@ -199,7 +210,7 @@ public function callSelf($method = '', $uri = '')
199210
}
200211
$request = self::$container->getSingle('request');
201212
$request->method = $method;
202-
$router = self::$container->getSingle('router');
213+
$router = self::$container->getSingle('router');
203214
$router->moduleName = $requestUri[0];
204215
$router->controllerName = $requestUri[1];
205216
$router->actionName = $requestUri[2];
@@ -233,6 +244,9 @@ public function run(Closure $request)
233244
*/
234245
public function response(Closure $closure)
235246
{
247+
if ($this->notOutput === true) {
248+
return;
249+
}
236250
// $closure()->reponse($this->responseData);
237251
$closure()->restSuccess($this->responseData);
238252
}

framework/Request.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ class Request
109109
public function __construct(App $app)
110110
{
111111
$this->serverParams = $_SERVER;
112-
$this->method = strtolower($_SERVER['REQUEST_METHOD']);
113-
$this->serverIp = $_SERVER['REMOTE_ADDR'];
114-
$this->clientIp = $_SERVER['SERVER_ADDR'];
115-
$this->beginTime = $_SERVER['REQUEST_TIME_FLOAT'];
116-
if ($app->isCli === 'true') {
112+
$this->method = isset($_SERVER['REQUEST_METHOD'])? strtolower($_SERVER['REQUEST_METHOD']) : 'get';
113+
$this->serverIp = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR'] : '';
114+
$this->clientIp = isset($_SERVER['SERVER_ADDR'])? $_SERVER['SERVER_ADDR'] : '';
115+
$this->beginTime = isset($_SERVER['REQUEST_TIME_FLOAT'])? $_SERVER['REQUEST_TIME_FLOAT'] : time(true);
116+
if ($app->isCli === 'yes') {
117117
// cli 模式
118118
$this->requestParams = $_REQUEST['argv'];
119119
$this->getParams = $_REQUEST['argv'];

framework/handles/RouterHandle.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function register(App $app)
222222

223223
/* 路由策略 */
224224
$this->routeStrategy = 'pathinfo';
225-
if (strpos($this->requestUri, 'index.php') || $app->isCli === 'true') {
225+
if (strpos($this->requestUri, 'index.php') || $app->isCli === 'yes') {
226226
$this->routeStrategy = 'general';
227227
}
228228

@@ -308,10 +308,12 @@ public function pathinfo()
308308
preg_match_all('/^\/(.*)/', $this->requestUri, $uri);
309309
}
310310

311+
// 使用默认模块/控制器/操作逻辑
311312
if (!isset($uri[1][0]) || empty($uri[1][0])) {
312-
/*
313-
* 使用默认模块/控制器/操作逻辑
314-
*/
313+
// CLI 模式不输出
314+
if ($this->app->isCli === 'yes') {
315+
$this->app->notOutput = true;
316+
}
315317
return;
316318
}
317319
$uri = $uri[1][0];
@@ -382,6 +384,9 @@ private function userDefined()
382384
}
383385
$map = $this->$method;
384386
$this->app->responseData = $map[$uri]($app);
387+
if ($this->app->isCli === 'yes') {
388+
$this->app->notOutput = false;
389+
}
385390
return true;
386391
}
387392

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
</filter>
2222
<php>
2323
<env name="APP_ENV" value="testing"/>
24+
<env name="IS_CLI" value="yes"/>
2425
</php>
2526
</phpunit>

tests/demo/DemoTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Tests\Demo;
1313

1414
use Tests\TestCase;
15-
use App\Demo\Controllers\Index;
15+
use Framework\App;
1616

1717
/**
1818
* 单元测试 示例
@@ -26,11 +26,9 @@ class DemoTest extends TestCase
2626
*/
2727
public function testDemo()
2828
{
29-
$index = new Index();
30-
3129
$this->assertEquals(
3230
'Hello Easy PHP',
33-
$index->hello()
31+
App::$app->get('demo/index/hello')
3432
);
3533
}
3634
}

0 commit comments

Comments
 (0)