Skip to content

Commit

Permalink
单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Dec 26, 2024
1 parent 9825d5b commit 0cfbbf2
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 22 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: tests

on:
push:
branches:
- 5.0
pull_request:

jobs:
linux_tests:
runs-on: ubuntu-22.04

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
php: ["8.1", "8.2", "8.3", "8.4"]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: json, posix, pcntl
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: xdebug

- name: Install dependencies
uses: nick-fields/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --ansi

- name: Static analysis
run: composer analyze

- name: Execute tests
run: composer test
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"require-dev": {
"pestphp/pest": "^3.7",
"mockery/mockery": "^1.6",
"guzzlehttp/guzzle": "^7.0"
"guzzlehttp/guzzle": "^7.0",
"phpstan/phpstan": "^2.0"
},
"autoload-dev": {
"psr-4": {
Expand All @@ -41,5 +42,9 @@
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"scripts": {
"analyze": "phpstan --memory-limit=1G",
"test": "pest --colors=always"
}
}
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 5
paths:
- src
- tests
ignoreErrors:
- '#Function root_path not found.#'
- '#Function env not found.#'
- '#Function app_path not found.#'
- '#Function config_path not found.#'
- '#Function public_path not found.#'
- '#Function json not found.#'
7 changes: 4 additions & 3 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
Expand All @@ -9,9 +9,10 @@
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</source>
</phpunit>
5 changes: 0 additions & 5 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

namespace think\worker;

/**
* Class Http
* @package think\swoole
* @property $request
*/
class Http extends \think\Http
{

Expand Down
4 changes: 2 additions & 2 deletions src/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class Sandbox
{
use ModifyProperty;

/** @var WorkerApp */
/** @var WorkerApp|null */
protected $snapshot;

/** @var WorkerApp */
/** @var Container */
protected $app;

/** @var Config */
Expand Down
4 changes: 2 additions & 2 deletions src/concerns/WithContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function getContainer()
/**
* 获取配置
* @param string $name
* @param null $default
* @param mixed $default
* @return mixed
*/
public function getConfig(string $name, $default = null)
Expand All @@ -42,7 +42,7 @@ public function getConfig(string $name, $default = null)
/**
* 触发事件
* @param string $event
* @param null $params
* @param mixed $params
*/
public function triggerEvent(string $event, $params = null): void
{
Expand Down
18 changes: 12 additions & 6 deletions src/response/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public function setFile($file, ?string $contentDisposition = null, bool $autoEta

public function setAutoContentType()
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if (extension_loaded('fileinfo')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);

$mimeType = finfo_file($finfo, $this->file->getPathname());
if ($mimeType) {
$this->header['Content-Type'] = $mimeType;
$mimeType = finfo_file($finfo, $this->file->getPathname());
if ($mimeType) {
$this->header['Content-Type'] = $mimeType;
}
}
}

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

public function setAutoLastModified()
{
$date = DateTime::createFromFormat('U', $this->file->getMTime());
return $this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
$mTime = $this->file->getMTime();
if ($mTime) {
$date = DateTime::createFromFormat('U', (string) $mTime);
$this->lastModified($date->format('D, d M Y H:i:s') . ' GMT');
}
return $this;
}

public function setAutoEtag()
Expand Down
6 changes: 3 additions & 3 deletions tests/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@
->toBe(200)
->and($response->getBody()->getContents())
->toBe('hot');

unlink(__DIR__ . '/stub/route/hot.php');
});
})->after(function () {
@unlink(__DIR__ . '/stub/route/hot.php');
})->skipOnWindows();

0 comments on commit 0cfbbf2

Please sign in to comment.