From 5e8f80b626db306238af176cb78365926a110aaf Mon Sep 17 00:00:00 2001 From: sunxyw <31698606+sunxyw@users.noreply.github.com> Date: Fri, 23 Dec 2022 17:16:08 +0800 Subject: [PATCH] enhance test run (#193) --- composer.json | 11 +++++-- phpstan.neon | 1 + tests/TestCase.php | 27 ++++++++++++++++ tests/Trait/HasLogger.php | 44 +++++++++++++++++++++++++++ tests/ZM/Config/ZMConfigTest.php | 17 +++++++---- tests/ZM/Middleware/PipelineTest.php | 13 +------- tests/ZM/Utils/ReflectionUtilTest.php | 2 +- tests/ZM/Utils/ZMUtilTest.php | 2 +- 8 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 tests/TestCase.php create mode 100644 tests/Trait/HasLogger.php diff --git a/composer.json b/composer.json index 90c3e028..2c4ef7bf 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "brainmaestro/composer-git-hooks": "^3.0", "friendsofphp/php-cs-fixer": "^3.2 != 3.7.0", "jetbrains/phpstorm-attributes": "^1.0", + "phpspec/prophecy": "1.x-dev", "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", @@ -40,6 +41,9 @@ "roave/security-advisories": "dev-latest", "swoole/ide-helper": "^4.5" }, + "replace": { + "symfony/polyfill-php80": "*" + }, "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", @@ -48,6 +52,7 @@ "league/climate": "Display columns and status in terminal" }, "minimum-stability": "dev", + "prefer-stable": true, "autoload": { "psr-4": { "ZM\\": "src/ZM" @@ -70,11 +75,11 @@ "bin/zhamao" ], "config": { - "optimize-autoloader": true, - "sort-packages": true, "allow-plugins": { "phpstan/extension-installer": true - } + }, + "optimize-autoloader": true, + "sort-packages": true }, "extra": { "hooks": { diff --git a/phpstan.neon b/phpstan.neon index 626496d2..bc5de909 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,6 +3,7 @@ parameters: level: 4 paths: - ./src/ + - ./tests/ ignoreErrors: - '#Constant .* not found#' - '#PHPDoc tag @throws with type Psr\\Container\\ContainerExceptionInterface is not subtype of Throwable#' diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 00000000..7185cc2c --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,27 @@ +prophet = new Prophet(); + } + + protected function tearDown(): void + { + parent::tearDown(); + $this->prophet->checkPredictions(); + } +} diff --git a/tests/Trait/HasLogger.php b/tests/Trait/HasLogger.php new file mode 100644 index 00000000..246086d0 --- /dev/null +++ b/tests/Trait/HasLogger.php @@ -0,0 +1,44 @@ +logs[] = compact('level', 'message', 'context'); + } + + private function logged($level, $message, array $context = []): bool + { + return in_array(compact('level', 'message', 'context'), $this->logs, true); + } + + private function assertLogged($level, $message = null, array $context = []): void + { + $this->assertTrue( + $this->logged($level, $message, $context), + "Failed asserting that the log contains [{$level}] {$message}" + ); + } + + private function startMockLogger(): void + { + $logger = $this->prophet->prophesize(AbstractLogger::class); + $logger->log()->will(function ($args) { + $this->mockLog(...$args); + }); + ob_logger_register($logger->reveal()); + } +} diff --git a/tests/ZM/Config/ZMConfigTest.php b/tests/ZM/Config/ZMConfigTest.php index 35b0e494..59a5284c 100644 --- a/tests/ZM/Config/ZMConfigTest.php +++ b/tests/ZM/Config/ZMConfigTest.php @@ -4,8 +4,9 @@ declare(strict_types=1); namespace Tests\ZM\Config; -use PHPUnit\Framework\TestCase; +use Tests\TestCase; use ZM\Config\ZMConfig; +use ZM\Exception\ConfigException; use ZM\Utils\ReflectionUtil; /** @@ -64,9 +65,13 @@ class ZMConfigTest extends TestCase ' "yes", "another array" => ["far", "baz"]];' ); - $config = new ZMConfig([ - __DIR__ . '/config_mock', - ], 'development'); + try { + $config = new ZMConfig([ + __DIR__ . '/config_mock', + ], 'development'); + } catch (ConfigException $e) { + self::fail($e->getMessage()); + } self::$config = $config; } @@ -83,8 +88,8 @@ class ZMConfigTest extends TestCase public function testGetValueWhenKeyContainsDot(): void { $this->markTestSkipped('should it be supported?'); - $this->assertEquals('c', self::$config->get('test.a.b')); - $this->assertEquals('d', self::$config->get('test.a.b.c')); +// $this->assertEquals('c', self::$config->get('test.a.b')); +// $this->assertEquals('d', self::$config->get('test.a.b.c')); } public function testGetBooleanValue(): void diff --git a/tests/ZM/Middleware/PipelineTest.php b/tests/ZM/Middleware/PipelineTest.php index babe2cd7..ff079f41 100644 --- a/tests/ZM/Middleware/PipelineTest.php +++ b/tests/ZM/Middleware/PipelineTest.php @@ -4,8 +4,7 @@ declare(strict_types=1); namespace Tests\ZM\Middleware; -use PHPUnit\Framework\TestCase; -use ZM\Logger\ConsoleLogger; +use Tests\TestCase; use ZM\Middleware\Pipeline; use ZM\Middleware\TimerMiddleware; @@ -14,16 +13,6 @@ use ZM\Middleware\TimerMiddleware; */ class PipelineTest extends TestCase { - public function setUp(): void - { - ob_logger_register(new ConsoleLogger('debug')); - } - - public function tearDown(): void - { - ob_logger_register(new ConsoleLogger('error')); - } - public function testPipeline() { $pipe = new Pipeline(); diff --git a/tests/ZM/Utils/ReflectionUtilTest.php b/tests/ZM/Utils/ReflectionUtilTest.php index 9533212f..c3ee23d4 100644 --- a/tests/ZM/Utils/ReflectionUtilTest.php +++ b/tests/ZM/Utils/ReflectionUtilTest.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Tests\ZM\Utils; -use PHPUnit\Framework\TestCase; +use Tests\TestCase; use ZM\Utils\ReflectionUtil; /** diff --git a/tests/ZM/Utils/ZMUtilTest.php b/tests/ZM/Utils/ZMUtilTest.php index 0c79db43..f45bd935 100644 --- a/tests/ZM/Utils/ZMUtilTest.php +++ b/tests/ZM/Utils/ZMUtilTest.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Tests\ZM\Utils; -use PHPUnit\Framework\TestCase; +use Tests\TestCase; use ZM\Utils\ZMUtil; /**