Skip to content

Commit cce3ee3

Browse files
committed
add more unit tests examples
1 parent b8f1e7a commit cce3ee3

19 files changed

+353
-25
lines changed

app/Common/MyBean.php

+5
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public function myMethod(): array
2727

2828
return ['hi'];
2929
}
30+
31+
public function myMethod2(): string
32+
{
33+
return __METHOD__;
34+
}
3035
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of Swoft.
4+
*
5+
* @link https://swoft.org
6+
* @document https://swoft.org/docs
7+
* @contact [email protected]
8+
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
9+
*/
10+
11+
namespace App\Http\Controller;
12+
13+
use App\Process\MonitorProcess;
14+
use Swoft\Http\Server\Annotation\Mapping\Controller;
15+
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
16+
use function bean;
17+
use function vdump;
18+
19+
/**
20+
* Class ProcController
21+
*
22+
* @since 2.0
23+
*
24+
* @Controller()
25+
*/
26+
class ProcController
27+
{
28+
/**
29+
* @RequestMapping("test")
30+
*
31+
* @return string
32+
*/
33+
public function test(): string
34+
{
35+
$mp = bean(MonitorProcess::class);
36+
vdump($mp->getSwooleProcess()->exportSocket());
37+
38+
return 'hello';
39+
}
40+
}

app/Model/Logic/MonitorLogic.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ class MonitorLogic
3434
*/
3535
public function monitor(Process $process): void
3636
{
37-
$process->name('swoft-monitor');
37+
// \vdump($process->exportSocket());
38+
// $process->name('swoft-monitor');
3839

3940
while (true) {
4041
$connections = context()->getServer()->getSwooleServer()->connections;
4142
CLog::info('monitor = ' . json_encode($connections));
4243

4344
// Database
44-
$user = User::find(1)->toArray();
45-
CLog::info('user=' . json_encode($user));
46-
47-
// Redis
48-
Redis::set('test', 'ok');
49-
CLog::info('test=' . Redis::get('test'));
45+
// $user = User::find(1)->toArray();
46+
// CLog::info('user=' . json_encode($user));
47+
//
48+
// // Redis
49+
// Redis::set('test', 'ok');
50+
// CLog::info('test=' . Redis::get('test'));
5051

5152
Coroutine::sleep(3);
5253
}

app/bean.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
// 'tcp' => bean('tcpServer'),
4242
],
4343
'process' => [
44-
// 'monitor' => bean(MonitorProcess::class)
45-
// 'crontab' => bean(CrontabProcess::class)
44+
// 'monitor' => bean(\App\Process\MonitorProcess::class)
45+
// 'crontab' => bean(CrontabProcess::class)
4646
],
4747
'on' => [
48-
// SwooleEvent::TASK => bean(SyncTaskListener::class), // Enable sync task
48+
// SwooleEvent::TASK => bean(SyncTaskListener::class), // Enable sync task
4949
SwooleEvent::TASK => bean(TaskListener::class), // Enable task must task and finish event
5050
SwooleEvent::FINISH => bean(FinishListener::class)
5151
],
@@ -130,6 +130,9 @@
130130
],
131131
'rpcServer' => [
132132
'class' => ServiceServer::class,
133+
'listener' => [
134+
'http' => bean('httpServer'),
135+
]
133136
],
134137
'wsServer' => [
135138
'class' => WebSocketServer::class,

phpunit.xml

+13-10
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnFailure="false">
11-
<testsuites>
12-
<testsuite name="Tests">
13-
<directory suffix="Test.php">./test</directory>
14-
</testsuite>
15-
</testsuites>
16-
<filter>
17-
<whitelist processUncoveredFilesFromWhitelist="true">
18-
<directory suffix=".php">./app</directory>
19-
</whitelist>
20-
</filter>
11+
<testsuites>
12+
<testsuite name="unitTests">
13+
<directory suffix="Test.php">./test/unit</directory>
14+
</testsuite>
15+
<testsuite name="apiTests">
16+
<directory suffix="Test.php">./test/api</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist processUncoveredFilesFromWhitelist="true">
21+
<directory suffix=".php">./app</directory>
22+
</whitelist>
23+
</filter>
2124
</phpunit>

test/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Tests
2+
3+
## Http tests
4+
5+
**If you want to run http client tests, you must start the http server.**
6+
7+
### Run http tests
8+
9+
Can only be run inside phpstorm.
10+
11+
![http-client-tests](testdata/http-client-tests.png)
12+
13+
## API tests
14+
15+
**If you want to run api tests, you must start the http server.**
16+
17+
## Start server
18+
19+
```bash
20+
php bin/swoft http:start
21+
# OR
22+
php bin/swoft http:start -d
23+
```
24+
25+
### Run api tests
26+
27+
- use vendor phpunit
28+
29+
```bash
30+
vendor/bin/phpunit --testsuite apiTests
31+
```
32+
33+
- use global installed phpunit
34+
35+
```bash
36+
phpunit --testsuite apiTests
37+
```
38+
39+
## Unit tests
40+
41+
### Run unit tests
42+
43+
- use vendor phpunit
44+
45+
```bash
46+
vendor/bin/phpunit --testsuite unitTests
47+
```
48+
49+
- use global installed phpunit
50+
51+
```bash
52+
phpunit --testsuite unitTests
53+
```
File renamed without changes.

test/api/ExampleApiTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of Swoft.
4+
*
5+
* @link https://swoft.org
6+
* @document https://swoft.org/docs
7+
* @contact [email protected]
8+
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
9+
*/
10+
11+
namespace AppTest\Api;
12+
13+
use PHPUnit\Framework\TestCase;
14+
use Swoft\Swlib\HttpClient;
15+
16+
/**
17+
* Class ExampleApiTest
18+
*
19+
* @package AppTest\Api
20+
*/
21+
class ExampleApiTest extends TestCase
22+
{
23+
/**
24+
* @var HttpClient
25+
*/
26+
private $http;
27+
28+
public function setUp(): void
29+
{
30+
$this->http = new HttpClient();
31+
}
32+
33+
public function testHi(): void
34+
{
35+
$w = $this->http->get('http://127.0.0.1/hi');
36+
37+
$this->assertSame('hi', $w->getBody()->getContents());
38+
}
39+
}

test/api/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Http tests
2+
3+
**If you want to run these tests, you must start the http server.**
4+
5+
## Start server
6+
7+
```bash
8+
php bin/swoft http:start
9+
```

test/bootstrap.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@
88
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
99
*/
1010

11-
use SwoftTest\Testing\TestApplication;
11+
use AppTest\Testing\TestApplication;
1212

1313
$baseDir = dirname(__DIR__);
1414
$vendor = dirname(__DIR__) . '/vendor';
1515

1616
/** @var \Composer\Autoload\ClassLoader $loader */
1717
$loader = require dirname(__DIR__) . '/vendor/autoload.php';
18-
$loader->addPsr4('SwoftTest\\Testing\\', $vendor . '/swoft/framework/test/testing/');
1918

20-
$application = new TestApplication($baseDir);
21-
$application->setBeanFile($baseDir . '/app/bean.php');
22-
$application->run();
19+
$swoftFwDir = $vendor . '/swoft/framework';
20+
21+
// in framework developing
22+
if (file_exists($vendor . '/swoft/component/src/framework')) {
23+
$swoftFwDir = $vendor . '/swoft/component/src/framework';
24+
}
25+
26+
$loader->addPsr4('AppTest\\Unit\\', $baseDir . '/test/unit/');
27+
$loader->addPsr4('AppTest\\Testing\\', $baseDir . '/test/testing/');
28+
$loader->addPsr4('SwoftTest\\Testing\\', $swoftFwDir . '/test/testing/');
29+
30+
$app = new TestApplication($baseDir);
31+
$app->run();

test/httptest/.keep

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
If you want run api test, must start an server before run tests.

test/httptest/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Http tests
2+
3+
**If you want to run these tests, you must start the http server.**
4+
5+
## Config
6+
7+
Please see http-client.env.json
8+
9+
## Start server
10+
11+
```bash
12+
php bin/swoft http:start
13+
```
14+
15+
## Run
16+
17+
Can only be run inside phpstorm

test/httptest/http-client.env.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"development": {
3+
"host": "127.0.0.1:10106",
4+
"bid": 10006,
5+
"id-value": 12345,
6+
"username": "",
7+
"password": "",
8+
"my-var": "my-dev-value",
9+
"form-type": "application/x-www-form-urlencoded"
10+
},
11+
"testing": {
12+
"host": "",
13+
"bid": 10001,
14+
"id-value": 12345,
15+
"username": "",
16+
"password": "",
17+
"my-var": "my-dev-value",
18+
"form-type": "application/x-www-form-urlencoded"
19+
},
20+
"production": {
21+
"host": "example.com",
22+
"bid": 10001,
23+
"id-value": 6789,
24+
"username": "",
25+
"password": "",
26+
"my-var": "my-prod-value"
27+
}
28+
}

test/httptest/sample.http

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### test api
2+
GET http://{{host}}/hi
3+
Accept: text/plain
4+
5+
### test api2
6+
GET http://{{host}}/hello
7+
Accept: text/plain
8+
9+
###

test/testdata/.keep

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
storage same temp files for tests

test/testdata/http-client-tests.png

107 KB
Loading

0 commit comments

Comments
 (0)