From 4e9199c68e563bc5c07a7245b82e96379f84435d Mon Sep 17 00:00:00 2001 From: sunxyw Date: Mon, 28 Mar 2022 17:27:36 +0800 Subject: [PATCH] refactor phpunit-swoole --- bin/phpunit-swoole | 85 +++++++++++++++++---------------------------- composer.json | 3 +- phpunit.xml.dist | 5 ++- tests/bootstrap.php | 23 ++++++------ 4 files changed, 48 insertions(+), 68 deletions(-) mode change 100644 => 100755 bin/phpunit-swoole diff --git a/bin/phpunit-swoole b/bin/phpunit-swoole old mode 100644 new mode 100755 index b480220e..acce0232 --- a/bin/phpunit-swoole +++ b/bin/phpunit-swoole @@ -1,69 +1,48 @@ #!/usr/bin/env php - * 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 ]); -if (!ini_get('date.timezone')) { - ini_set('date.timezone', 'Asia/Shanghai'); -} +/* + * This file is part of PHPUnit. + * + * (c) Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -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; - } -} - -if (!defined('PHPUNIT_COMPOSER_INSTALL')) { +if (PHP_VERSION_ID <= 70100) { 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 + 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); -} 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__ . '/../tests/bootstrap.php'; - PHPUnit\TextUI\Command::main(false); - } catch (Exception $e) { - echo $e->getMessage() . PHP_EOL; - } + +if (!ini_get('date.timezone')) { + ini_set('date.timezone', 'UTC'); +} + +require $root . '/vendor/autoload.php'; + +sleep(1); + +go(static function () { + PHPUnit\TextUI\Command::main(false); + Swoole\Process::wait(); }); -Swoole\Event::wait(); -echo "Took " . round(microtime(true) - $starttime, 4) . "s\n"; + +Swoole\Event::wait(); \ No newline at end of file diff --git a/composer.json b/composer.json index 5d86c85c..78f95c36 100644 --- a/composer.json +++ b/composer.json @@ -90,6 +90,7 @@ "[ $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" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 04a60377..49ebec24 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,6 @@ - diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7b0bb7a0..4368c0da 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,7 +1,4 @@