Merge pull request #83 from zhamao-robot/add-phpunit-framework

添加 PHPUnit 单元测试框架
This commit is contained in:
Jerry Ma
2022-03-28 20:28:39 +08:00
committed by GitHub
17 changed files with 193 additions and 128 deletions

15
.editorconfig Normal file
View File

@@ -0,0 +1,15 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_size = 2

67
.gitignore vendored
View File

@@ -1,21 +1,23 @@
.idea/
/src/test/
/src/webconsole/config/
/vendor/
### Zhamao Framework ###
/zm_data/
composer.lock
/resources/server.phar
/distribute/
/bin/.phpunit.result.cache
/resources/zhamao.service
.phpunit.result.cache
.daemon_pid
/runtime/
/tmp/
/temp/
/site/
# go-cqhttp快速安装启动相关可能被废弃
# 进程锁文件
.daemon_pid
.zm_worker_*.pid
### Composer ###
composer.phar
/vendor/
composer.lock
### Go CQHTTP ###
/ext/go-cqhttp/data/
/ext/go-cqhttp/logs/
/ext/go-cqhttp/config.hjson
@@ -23,12 +25,53 @@ composer.lock
/ext/go-cqhttp/go-cqhttp
/ext/go-cqhttp/session.token
.zm_worker_*.pid
### Git ###
# 备份文件
*.orig
# Git Hook 的相关锁文件
# 解决 Merge 冲突时可能会自动生成的文件
*.BACKUP.*
*.BASE.*
*.LOCAL.*
*.REMOTE.*
*_BACKUP_*.txt
*_BASE_*.txt
*_LOCAL_*.txt
*_REMOTE_*.txt
# Git Hooks
cghooks.lock
# Vuepress 相关文件
### VuePress ###
/node_modules/
/docs/.vuepress/dist/
package-lock.json
### PHPUnit ###
# 生成的文件
/bin/.phpunit.result.cache
.phpunit.result.cache
.phpunit.cache
# 本地配置
/phpunit.xml
# 构建目录
/build/
### PhpStorm ###
# 兼容 PHPStorm 及其他 Jetbrains IDE
# 除了代码格式配置和运行配置之外的所有文件
.idea/*
!.idea/codeStyles
!.idea/runConfigurations
### Phive ###
/.phive/
/tools/
### ASDF ###
/.tool-version

85
bin/phpunit-swoole Normal file → Executable file
View File

@@ -1,69 +1,48 @@
#!/usr/bin/env php
<?php
/**
* Copyright: Swlib
* Author: Twosee <twose@qq.com>
* Date: 2018/4/14 下午10:58
*/
/** For Swoole coroutine tests */
use Swoole\Coroutine;
$root = dirname(__DIR__);
Coroutine::set([
co::set([
'log_level' => SWOOLE_LOG_INFO,
'trace_flags' => 0
]);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
if (PHP_VERSION_ID <= 70100) {
fwrite(
STDERR,
sprintf(
'This version of PHPUnit is supported on PHP 7.1 and above.' . PHP_EOL .
'You are using PHP %s (%s).' . PHP_EOL,
PHP_VERSION,
PHP_BINARY
)
);
die(1);
}
if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'Asia/Shanghai');
}
foreach ([
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php'
] as $file
) {
if (file_exists($file)) {
define('PHPUNIT_COMPOSER_INSTALL', $file);
break;
}
}
require $root . '/vendor/autoload.php';
if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
fwrite(
STDERR,
'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL .
' composer install' . PHP_EOL . PHP_EOL .
'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL
);
sleep(1);
die(1);
} else {
if (array_reverse(explode('/', __DIR__))[0] ?? '' === 'test') {
$vendor_dir = dirname(PHPUNIT_COMPOSER_INSTALL);
$bin_unit = "{$vendor_dir}/bin/phpunit";
$unit_uint = "{$vendor_dir}/phpunit/phpunit/phpunit";
if (file_exists($bin_unit)) {
@unlink($bin_unit);
@symlink(__FILE__, $bin_unit);
}
if (file_exists($unit_uint)) {
@unlink($unit_uint);
@symlink(__FILE__, $unit_uint);
}
}
}
/** @noinspection PhpIncludeInspection */
require PHPUNIT_COMPOSER_INSTALL;
$starttime = microtime(true);
go(function () {
try {
require_once __DIR__.'/../test/bootstrap.php';
PHPUnit\TextUI\Command::main(false);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
go(static function () {
PHPUnit\TextUI\Command::main(false);
Swoole\Process::wait();
});
Swoole\Event::wait();
echo "Took " . round(microtime(true) - $starttime, 4) . "s\n";

View File

@@ -1,38 +1,13 @@
{
"name": "zhamao/framework",
"description": "High performance chat robot and web server development framework",
"minimum-stability": "stable",
"license": "Apache-2.0",
"extra": {
"zm": {
"exclude-annotation-path": [
"src/ZM"
]
},
"hooks": {
"post-merge": "composer install",
"pre-commit": [
"echo committing as $(git config user.name)",
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src"
],
"pre-push": [
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src",
"composer analyse"
]
}
},
"authors": [
{
"name": "jerry",
"email": "admin@zhamao.me"
}
],
"prefer-stable": true,
"bin": [
"bin/start",
"bin/phpunit-swoole",
"bin/gendoc"
],
"require": {
"php": "^7.2 || ^7.3 || ^7.4 || ^8.0 || ^8.1",
"ext-json": "*",
@@ -52,6 +27,14 @@
"zhamao/console": "^1.0",
"zhamao/request": "^1.1"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.8",
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
"phpstan/phpstan": "^1.1",
"phpunit/phpunit": "^8.5 || ^9.0",
"roave/security-advisories": "dev-latest",
"swoole/ide-helper": "^4.5"
},
"suggest": {
"ext-ctype": "Use C/C++ extension instead of polyfill will be more efficient",
"ext-mbstring": "Use C/C++ extension instead of polyfill will be more efficient",
@@ -59,6 +42,8 @@
"ext-redis": "If you use Redis in framework, you will need this extension",
"league/climate": "Display columns and status in terminal"
},
"minimum-stability": "stable",
"prefer-stable": true,
"autoload": {
"psr-4": {
"ZM\\": "src/ZM"
@@ -73,22 +58,39 @@
"Custom\\": "src/Custom"
}
},
"bin": [
"bin/gendoc",
"bin/phpunit-swoole",
"bin/start"
],
"config": {
"optimize-autoloader": true,
"sort-packages": true
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.8",
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
"phpstan/phpstan": "^1.1",
"phpunit/phpunit": "^8.5 || ^9.0",
"swoole/ide-helper": "^4.5"
"extra": {
"hooks": {
"post-merge": "composer install",
"pre-commit": [
"echo committing as $(git config user.name)",
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src"
],
"pre-push": [
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src",
"composer analyse"
]
},
"zm": {
"exclude-annotation-path": [
"src/ZM"
]
}
},
"scripts": {
"post-install-cmd": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/cghooks add"
],
"analyse": "phpstan analyse --memory-limit 300M -l 0 ./src",
"cs-fix": "php-cs-fixer fix $1"
"cs-fix": "php-cs-fixer fix $1",
"test": "bin/phpunit-swoole"
}
}

29
phpunit.xml.dist Normal file
View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
testdox="true"
verbose="true"
>
<testsuites>
<testsuite name="Zhamao Framework Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src/ZM</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
</php>
</phpunit>

View File

@@ -1,31 +0,0 @@
<?php
/**
* @since 2.5
*/
use ZM\Config\ZMConfig;
use ZM\Console\Console;
use ZM\Store\LightCacheInside;
use ZM\Store\ZMAtomic;
use ZM\Utils\DataProvider;
use ZM\Utils\Terminal;
set_coroutine_params([]);
// 模拟define
chdir(__DIR__ . '/../');
define("WORKING_DIR", getcwd());
define("SOURCE_ROOT_DIR", WORKING_DIR);
define("ZM_DATA", WORKING_DIR . "/zm_data/");
define("LOAD_MODE", 0);
define("FRAMEWORK_ROOT_DIR", realpath(__DIR__ . "/../"));
ZMConfig::setDirectory(WORKING_DIR."/config/");
ZMConfig::setEnv("");
if (ZMConfig::get("global") === false) {
die (zm_internal_errcode("E00007") . "Global config load failed: " . ZMConfig::$last_error . "\nError path: " . DataProvider::getSourceRootDir() . "\nPlease init first!\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/37\n");
}
LightCacheInside::init();
ZMAtomic::init();
Terminal::init();
Console::setLevel(4);

28
tests/bootstrap.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
use ZM\Config\ZMConfig;
use ZM\Console\Console;
use ZM\Store\LightCacheInside;
use ZM\Store\ZMAtomic;
use ZM\Utils\DataProvider;
use ZM\Utils\Terminal;
require_once __DIR__ . '/../vendor/autoload.php';
// 模拟define
chdir(__DIR__ . '/../');
define('WORKING_DIR', getcwd());
const SOURCE_ROOT_DIR = WORKING_DIR;
const ZM_DATA = WORKING_DIR . '/zm_data/';
const LOAD_MODE = 0;
define('FRAMEWORK_ROOT_DIR', dirname(__DIR__) . '/');
ZMConfig::setDirectory(WORKING_DIR . '/config/');
ZMConfig::setEnv();
if (ZMConfig::get('global') === false) {
die (zm_internal_errcode('E00007') . 'Global config load failed: ' . ZMConfig::$last_error . "\nError path: " . DataProvider::getSourceRootDir() . "\nPlease init first!\nSee: https://github.com/zhamao-robot/zhamao-framework/issues/37\n");
}
LightCacheInside::init();
ZMAtomic::init();
Terminal::init();
Console::setLevel(4);