Skip to content

Commit 0cfbbf2

Browse files
committed
单元测试
1 parent 9825d5b commit 0cfbbf2

File tree

9 files changed

+87
-22
lines changed

9 files changed

+87
-22
lines changed

.github/workflows/test.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches:
6+
- 5.0
7+
pull_request:
8+
9+
jobs:
10+
linux_tests:
11+
runs-on: ubuntu-22.04
12+
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: [ubuntu-latest, macos-latest, windows-latest]
17+
php: ["8.1", "8.2", "8.3", "8.4"]
18+
stability: [prefer-lowest, prefer-stable]
19+
20+
name: PHP ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v4
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php }}
30+
extensions: json, posix, pcntl
31+
ini-values: error_reporting=E_ALL
32+
tools: composer:v2
33+
coverage: xdebug
34+
35+
- name: Install dependencies
36+
uses: nick-fields/retry@v3
37+
with:
38+
timeout_minutes: 5
39+
max_attempts: 5
40+
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --ansi
41+
42+
- name: Static analysis
43+
run: composer analyze
44+
45+
- name: Execute tests
46+
run: composer test

composer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"require-dev": {
3131
"pestphp/pest": "^3.7",
3232
"mockery/mockery": "^1.6",
33-
"guzzlehttp/guzzle": "^7.0"
33+
"guzzlehttp/guzzle": "^7.0",
34+
"phpstan/phpstan": "^2.0"
3435
},
3536
"autoload-dev": {
3637
"psr-4": {
@@ -41,5 +42,9 @@
4142
"allow-plugins": {
4243
"pestphp/pest-plugin": true
4344
}
45+
},
46+
"scripts": {
47+
"analyze": "phpstan --memory-limit=1G",
48+
"test": "pest --colors=always"
4449
}
4550
}

phpstan.neon.dist

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- src
5+
- tests
6+
ignoreErrors:
7+
- '#Function root_path not found.#'
8+
- '#Function env not found.#'
9+
- '#Function app_path not found.#'
10+
- '#Function config_path not found.#'
11+
- '#Function public_path not found.#'
12+
- '#Function json not found.#'

phpunit.xml phpunit.xml.dist

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
44
bootstrap="vendor/autoload.php"
55
colors="true"
66
>
@@ -9,9 +9,10 @@
99
<directory suffix="Test.php">./tests</directory>
1010
</testsuite>
1111
</testsuites>
12-
<coverage processUncoveredFiles="true">
12+
<coverage/>
13+
<source>
1314
<include>
1415
<directory suffix=".php">./src</directory>
1516
</include>
16-
</coverage>
17+
</source>
1718
</phpunit>

src/Http.php

-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace think\worker;
44

5-
/**
6-
* Class Http
7-
* @package think\swoole
8-
* @property $request
9-
*/
105
class Http extends \think\Http
116
{
127

src/Sandbox.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class Sandbox
2626
{
2727
use ModifyProperty;
2828

29-
/** @var WorkerApp */
29+
/** @var WorkerApp|null */
3030
protected $snapshot;
3131

32-
/** @var WorkerApp */
32+
/** @var Container */
3333
protected $app;
3434

3535
/** @var Config */

src/concerns/WithContainer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function getContainer()
3131
/**
3232
* 获取配置
3333
* @param string $name
34-
* @param null $default
34+
* @param mixed $default
3535
* @return mixed
3636
*/
3737
public function getConfig(string $name, $default = null)
@@ -42,7 +42,7 @@ public function getConfig(string $name, $default = null)
4242
/**
4343
* 触发事件
4444
* @param string $event
45-
* @param null $params
45+
* @param mixed $params
4646
*/
4747
public function triggerEvent(string $event, $params = null): void
4848
{

src/response/File.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ public function setFile($file, ?string $contentDisposition = null, bool $autoEta
6767

6868
public function setAutoContentType()
6969
{
70-
$finfo = finfo_open(FILEINFO_MIME_TYPE);
70+
if (extension_loaded('fileinfo')) {
71+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
7172

72-
$mimeType = finfo_file($finfo, $this->file->getPathname());
73-
if ($mimeType) {
74-
$this->header['Content-Type'] = $mimeType;
73+
$mimeType = finfo_file($finfo, $this->file->getPathname());
74+
if ($mimeType) {
75+
$this->header['Content-Type'] = $mimeType;
76+
}
7577
}
7678
}
7779

@@ -88,8 +90,12 @@ public function setContentDisposition(string $disposition, string $filename = ''
8890

8991
public function setAutoLastModified()
9092
{
91-
$date = DateTime::createFromFormat('U', $this->file->getMTime());
92-
return $this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
93+
$mTime = $this->file->getMTime();
94+
if ($mTime) {
95+
$date = DateTime::createFromFormat('U', (string) $mTime);
96+
$this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
97+
}
98+
return $this;
9399
}
94100

95101
public function setAutoEtag()

tests/HttpTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,6 @@
138138
->toBe(200)
139139
->and($response->getBody()->getContents())
140140
->toBe('hot');
141-
142-
unlink(__DIR__ . '/stub/route/hot.php');
143-
});
141+
})->after(function () {
142+
@unlink(__DIR__ . '/stub/route/hot.php');
143+
})->skipOnWindows();

0 commit comments

Comments
 (0)