Skip to content

Commit 1b550de

Browse files
author
xiaohengjin
committed
~
1 parent 900822d commit 1b550de

23 files changed

+147
-138
lines changed

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<directory suffix="Test.php">./test/Store</directory>
1818
<directory suffix="Test.php">./test/Testing</directory>
1919
<directory suffix="Test.php">./test/Utilities</directory>
20-
<!--<directory suffix="Test.php">./test/Foundation</directory>-->
20+
<directory suffix="Test.php">./test/Foundation</directory>
2121
</testsuite>
2222
</testsuites>
2323
</phpunit>

src/Testing/TaskTest.php

+14-16
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,19 @@ class TaskTest extends UnitTest
2727

2828
public function testTasksWork()
2929
{
30-
echo "enter taskTest\n";
31-
$this->initTask();
3230
self::$nTasks++;
33-
echo self::$nTasks, "\n";
34-
$this->taskCounter++;
35-
$this->eventChain->before('test_task_num_' . $this->taskCounter, 'test_task_done');
36-
37-
$this->scanTasks();
38-
$taskCoroutine = $this->runTaskTests();
39-
40-
self::$jobs[] = $taskCoroutine;
31+
self::$jobs[] = $this;
4132

4233
if (self::$isRunningJob) {
4334
return;
4435
}
45-
$taskCoroutine = array_shift(self::$jobs);
36+
37+
$task = array_shift(self::$jobs);
38+
$task->initTask();
39+
$task->taskCounter++;
40+
$task->eventChain->before('test_task_num_' . $task->taskCounter, 'test_task_done');
41+
$task->scanTasks();
42+
$taskCoroutine = $task->runTaskTests();
4643
self::$isRunningJob = true;
4744
$context = new Context();
4845
$context->set('request_time', Time::stamp());
@@ -83,14 +80,16 @@ protected function initTask()
8380
$this->eventChain = $this->event->getEventChain();
8481

8582
$this->event->bind('test_task_done', function () {
86-
echo "task done...\n";
8783
--self::$nTasks;
88-
echo self::$nTasks, "\n";
8984
if (self::$jobs == []) {
9085
self::$isRunningJob = false;
9186
} else {
92-
echo "scheduling......\n";
93-
$taskCoroutine = array_shift(self::$jobs);
87+
$task = array_shift(self::$jobs);
88+
$task->initTask();
89+
$task->taskCounter++;
90+
$task->eventChain->before('test_task_num_' . $task->taskCounter, 'test_task_done');
91+
$task->scanTasks();
92+
$taskCoroutine = $task->runTaskTests();
9493
$context = new Context();
9594
$context->set('request_time', Time::stamp());
9695
$request_timeout = 30;
@@ -99,7 +98,6 @@ protected function initTask()
9998
return;
10099
}
101100
if (self::$nTasks == 0) {
102-
echo "exiting....\n";
103101
swoole_event_exit();
104102
}
105103
});

test/Foundation/Core/ConfigLoaderTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Zan\Framework\Foundation\Core\ConfigLoader;
1111

12-
1312
class ConfigLoaderTest extends \TestCase {
1413

1514
private $path;
@@ -20,7 +19,7 @@ public function setUp()
2019
}
2120

2221
public function test(){
23-
$config = ConfigLoader::getInstance();
22+
$config = new ConfigLoader();
2423
$result = $config->load($this->path);
2524
$this->assertEquals('test', $result['a']['config'], 'ConfigLoader::load fail');
2625
$this->assertEquals('pf', $result['pf']['b']['db'], 'ConfigLoader::load fail');

test/Foundation/Core/ConfigTest.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,34 @@
1212
use Zan\Framework\Foundation\Core\RunMode;
1313
use Zan\Framework\Foundation\Core\Path;
1414

15-
1615
class ConfigTest extends \TestCase
1716
{
17+
private $configPath;
18+
private $config;
19+
private $runMode;
20+
1821
public function setUp()
1922
{
2023
$path = __DIR__ . '/config/';
24+
$this->configPath = Path::getConfigPath();
2125
Path::setConfigPath($path);
26+
$this->runMode = RunMode::get();
27+
RunMode::set('test');
28+
$config = new Config();
29+
$this->config = $this->getProperty($config, "configMap");
30+
Config::init();
2231
}
2332

2433
public function tearDown()
2534
{
26-
Config::clear();
35+
$config = new Config();
36+
$this->setPropertyValue($config, "configMap", $this->config);
37+
RunMode::set($this->runMode);
38+
Path::setConfigPath($this->configPath);
2739
}
2840

2941
public function testGetConfigWork()
3042
{
31-
RunMode::set('test');
32-
Config::init();
3343
$data = Config::get('a.share');
3444
$this->assertEquals('share', $data, 'Config::get share get failed');
3545
$data = Config::get('a.config');

test/Foundation/Coroutine/GeneratorTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Zan\Framework\Test\Foundation\Coroutine;
1010

11-
1211
class GeneratorTest extends Base {
1312
protected function step()
1413
{

test/Foundation/Coroutine/TaskTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Zan\Framework\Test\Foundation\Coroutine\Task\AsyncJob;
1212
use Zan\Framework\Test\Foundation\Coroutine\Task\Coroutine;
1313
use Zan\Framework\Test\Foundation\Coroutine\Task\Error;
14-
use Zan\Framework\Test\Foundation\Coroutine\Task\ErrorException;
1514
use Zan\Framework\Test\Foundation\Coroutine\Task\Simple;
1615
use Zan\Framework\Test\Foundation\Coroutine\Task\Steps;
1716
use Zan\Framework\Test\Foundation\Coroutine\Task\YieldValues;

test/Network/Connection/ConnectionPoolTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
*/
88

99
namespace Zan\Framework\Test\Network\Connection;
10-
use Zan\Framework\Network\Connection\ConnectionInitiator;
1110
use Zan\Framework\Network\Connection\ConnectionManager;
1211
use Zan\Framework\Testing\TaskTest;
1312

1413
class ConnectionPoolTest extends TaskTest {
1514
public function taskPoolWork()
1615
{
17-
$pool = (yield ConnectionManager::getInstance()->get('mysql.default_write'));
18-
//$pool->close();
19-
$pool = (yield ConnectionManager::getInstance()->get('redis.default_write'));
16+
try {
17+
$conn = (yield ConnectionManager::getInstance()->get('mysql.default_write'));
18+
//$pool->close();
19+
$conn = (yield ConnectionManager::getInstance()->get('redis.default_write'));
2020
// $pool->close();
21+
} catch (\Exception $e) {
22+
var_dump($e->getMessage());
23+
}
2124
}
2225
}

test/Network/Http/ClientTest.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@
77

88
use Zan\Framework\Network\Common\Client;
99
use Zan\Framework\Network\Common\Exception\HttpClientTimeoutException;
10-
use Zan\Framework\Foundation\Coroutine\Task;
11-
use Zan\Framework\Test\Foundation\Coroutine\Context;
10+
use Zan\Framework\Testing\TaskTest;
1211

13-
class ClientTest extends \TestCase {
14-
public function testTaskCall()
15-
{
16-
$context = new Context();
17-
$task = new Task($this->makeCoroutine($context), null, 8);
18-
$task->run();
19-
}
20-
21-
private function makeCoroutine($context)
12+
class ClientTest extends TaskTest {
13+
public function taskClientCall()
2214
{
2315
try {
2416
$result = (yield Client::call('fenxiao.supplier.goods.getGoodsByKdtGoodsId', [
@@ -29,7 +21,6 @@ private function makeCoroutine($context)
2921
return;
3022
}
3123

32-
$context->set('result', $result);
3324
yield 'success';
3425
}
3526
}

test/Network/Http/DnsClientTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Date: 2017/3/6
66
* Time: 下午3:10
77
*/
8-
98
namespace Zan\Framework\Test\Network\Http;
109

1110
use Zan\Framework\Foundation\Contract\Async;

test/Network/Http/HttpClientTest.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
<?php
22
namespace Zan\Framework\Test\Network\Http;
33

4+
use Zan\Framework\Network\Common\Exception\HttpClientTimeoutException;
45
use Zan\Framework\Network\Common\HttpClient;
56
use Zan\Framework\Testing\TaskTest;
67

7-
use Zan\Framework\Foundation\Coroutine\Task;
8-
use Zan\Framework\Test\Foundation\Coroutine\Context;
9-
108
class HttpClientTest extends TaskTest
119
{
12-
public function testTaskCall()
13-
{
14-
$context = new Context();
15-
$task = new Task($this->makeCoroutine($context), null, 8);
16-
$task->run();
17-
}
18-
19-
private function makeCoroutine($context)
10+
public function taskHttpGet()
2011
{
2112
$params = [
2213
'txt' => 'aaa',
@@ -30,7 +21,13 @@ private function makeCoroutine($context)
3021
'bg_color' => 'ffffff',
3122
];
3223
$httpClient = new HttpClient('127.0.0.1', 12345);
33-
$response = (yield $httpClient->get('', $params));
24+
try {
25+
$response = (yield $httpClient->get('', $params));
26+
} catch (\Exception $e) {
27+
$this->assertInstanceOf(HttpClientTimeoutException::class, $e, $e->getMessage());
28+
return;
29+
}
30+
3431
$result = $response->getBody();
3532

3633
$this->assertEquals($result, http_build_query($params), "Http request failed");

test/Network/Http/TcpClientTest.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
use Zan\Framework\Network\Common\TcpClient;
3-
use Zan\Framework\Network\Connection\ConnectionInitiator;
43
use Zan\Framework\Network\Connection\ConnectionManager;
54
use Zan\Framework\Testing\TaskTest;
65

@@ -14,11 +13,16 @@ class TcpClientTest extends TaskTest
1413
{
1514
public function taskTcp()
1615
{
17-
$connection = (yield ConnectionManager::getInstance()->get("tcp.echo"));
18-
$tcpClient = new TcpClient($connection);
16+
try {
17+
$connection = (yield ConnectionManager::getInstance()->get("tcp.echo"));
18+
$tcpClient = new TcpClient($connection);
1919

20-
$request = "Hello TcpClientTest";
21-
$response = (yield $tcpClient->send($request));
22-
$this->assertEquals($response, $request, "tcp echo test failed");
20+
$request = "Hello TcpClientTest";
21+
$response = (yield $tcpClient->send($request));
22+
$this->assertEquals($response, $request, "tcp echo test failed");
23+
} catch (\Exception $e) {
24+
var_dump($e->getMessage());
25+
return;
26+
}
2327
}
2428
}

test/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Zan框架测试
2+
##一、运行环境准备
3+
cd zan/mockServer
4+
5+
sh ./go.sh start 启动server
6+
7+
sh ./go.sh stop 关闭server
8+
9+
##二、执行测试
10+
cd zan
11+
12+
phpunit

test/Sdk/Barcode/BarcodeTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public function taskGenerateBarcode()
2323
'hrt' => 1
2424
];
2525
$barcode = 20;
26+
try {
27+
yield Barcode::createDataUrl($text, $height, $styles, $barcode);
28+
} catch (\Exception $e) {
29+
var_dump($e->getMessage());
30+
}
2631

27-
yield Barcode::createDataUrl($text, $height, $styles, $barcode);
2832
}
2933
}

test/Sdk/Log/LogTest.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77
*/
88
namespace Zan\Framework\Test\Sdk\Log;
99

10-
use Zan\Framework\Network\Connection\ConnectionInitiator;
1110
use Zan\Framework\Testing\TaskTest;
1211
use Zan\Framework\Sdk\Log\Log;
1312

1413
class LogTest extends TaskTest
1514
{
16-
public function initTask()
17-
{
18-
//connection pool init
19-
// ConnectionInitiator::getInstance()->init('connection', null);
20-
parent::initTask();
21-
}
22-
2315
public function taskSysLog()
2416
{
25-
yield Log::make("default")->info('log to syslog');
17+
try {
18+
yield Log::make("default")->info('log to syslog');
19+
} catch (\Exception $e) {
20+
var_dump($e->getMessage());
21+
}
22+
2623
}
2724

2825
public function taskLogLog()

test/Sdk/Qrcode/QrcodeTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
namespace Zan\Framework\Test\Sdk\Qrcode;
99

1010
use Zan\Framework\Testing\TaskTest;
11-
use Zan\Framework\Sdk\Barcode\Barcode;
1211
use Zan\Framework\Utilities\Types\Qrcode;
1312

1413
class QrcodeTest extends TaskTest {
1514
public function taskGenerateQrcode()
1615
{
1716
$text = "youzan";
1817
$size = '270x270';
19-
$qrCode = (yield Qrcode::create($text, $size, true));
18+
try {
19+
$qrCode = (yield Qrcode::create($text, $size, true));
20+
} catch (\Exception $e) {
21+
var_dump($e->getMessage());
22+
}
23+
2024
$this->assertStringStartsWith("data", $qrCode, "qrCode get failed");
2125
}
2226
}

test/Sdk/Queue/NsqClientTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ class NsqClientTest extends TaskTest
1111

1212
public function taskPubSub()
1313
{
14-
// yield $this->sub();
15-
// yield $this->pub();
14+
try {
15+
yield $this->sub();
16+
yield $this->pub();
17+
} catch (\Exception $e) {
18+
var_dump($e->getMessage());
19+
}
20+
1621
}
1722

1823
private function pub()

test/Sdk/ShortURL/ShortURLTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
class ShortURLTest extends TaskTest {
1414
public function taskGetShortURL()
1515
{
16-
$shortUrl = (yield ShortUrl::get("http://koudaitong.com"));
17-
$this->assertStringStartsWith("http://kdt.im/", $shortUrl, "url: $shortUrl is invalid, int should start with http://kdt.im/");
16+
try {
17+
$shortUrl = (yield ShortUrl::get("http://koudaitong.com"));
18+
$this->assertStringStartsWith("http://kdt.im/", $shortUrl, "url: $shortUrl is invalid, int should start with http://kdt.im/");
19+
} catch (\Exception $e) {
20+
var_dump($e->getMessage());
21+
}
1822
}
1923
}

0 commit comments

Comments
 (0)