mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-25 01:25:34 +08:00
enhance test run (#193)
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
"brainmaestro/composer-git-hooks": "^3.0",
|
"brainmaestro/composer-git-hooks": "^3.0",
|
||||||
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
|
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
|
||||||
"jetbrains/phpstorm-attributes": "^1.0",
|
"jetbrains/phpstorm-attributes": "^1.0",
|
||||||
|
"phpspec/prophecy": "1.x-dev",
|
||||||
"phpstan/extension-installer": "^1.1",
|
"phpstan/extension-installer": "^1.1",
|
||||||
"phpstan/phpstan": "^1.1",
|
"phpstan/phpstan": "^1.1",
|
||||||
"phpstan/phpstan-deprecation-rules": "^1.0",
|
"phpstan/phpstan-deprecation-rules": "^1.0",
|
||||||
@@ -40,6 +41,9 @@
|
|||||||
"roave/security-advisories": "dev-latest",
|
"roave/security-advisories": "dev-latest",
|
||||||
"swoole/ide-helper": "^4.5"
|
"swoole/ide-helper": "^4.5"
|
||||||
},
|
},
|
||||||
|
"replace": {
|
||||||
|
"symfony/polyfill-php80": "*"
|
||||||
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"ext-ctype": "Use C/C++ extension instead of polyfill will be more efficient",
|
"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",
|
"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"
|
"league/climate": "Display columns and status in terminal"
|
||||||
},
|
},
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
|
"prefer-stable": true,
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"ZM\\": "src/ZM"
|
"ZM\\": "src/ZM"
|
||||||
@@ -70,11 +75,11 @@
|
|||||||
"bin/zhamao"
|
"bin/zhamao"
|
||||||
],
|
],
|
||||||
"config": {
|
"config": {
|
||||||
"optimize-autoloader": true,
|
|
||||||
"sort-packages": true,
|
|
||||||
"allow-plugins": {
|
"allow-plugins": {
|
||||||
"phpstan/extension-installer": true
|
"phpstan/extension-installer": true
|
||||||
}
|
},
|
||||||
|
"optimize-autoloader": true,
|
||||||
|
"sort-packages": true
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"hooks": {
|
"hooks": {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ parameters:
|
|||||||
level: 4
|
level: 4
|
||||||
paths:
|
paths:
|
||||||
- ./src/
|
- ./src/
|
||||||
|
- ./tests/
|
||||||
ignoreErrors:
|
ignoreErrors:
|
||||||
- '#Constant .* not found#'
|
- '#Constant .* not found#'
|
||||||
- '#PHPDoc tag @throws with type Psr\\Container\\ContainerExceptionInterface is not subtype of Throwable#'
|
- '#PHPDoc tag @throws with type Psr\\Container\\ContainerExceptionInterface is not subtype of Throwable#'
|
||||||
|
|||||||
27
tests/TestCase.php
Normal file
27
tests/TestCase.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests;
|
||||||
|
|
||||||
|
use Prophecy\Prophet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class TestCase extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
protected Prophet $prophet;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->prophet = new Prophet();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
parent::tearDown();
|
||||||
|
$this->prophet->checkPredictions();
|
||||||
|
}
|
||||||
|
}
|
||||||
44
tests/Trait/HasLogger.php
Normal file
44
tests/Trait/HasLogger.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Trait;
|
||||||
|
|
||||||
|
use Prophecy\Prophet;
|
||||||
|
use Psr\Log\AbstractLogger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模拟 Logger 行为
|
||||||
|
* @property Prophet $prophet
|
||||||
|
*/
|
||||||
|
trait HasLogger
|
||||||
|
{
|
||||||
|
private array $logs = [];
|
||||||
|
|
||||||
|
private function mockLog($level, $message, array $context = []): void
|
||||||
|
{
|
||||||
|
$this->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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tests\ZM\Config;
|
namespace Tests\ZM\Config;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use Tests\TestCase;
|
||||||
use ZM\Config\ZMConfig;
|
use ZM\Config\ZMConfig;
|
||||||
|
use ZM\Exception\ConfigException;
|
||||||
use ZM\Utils\ReflectionUtil;
|
use ZM\Utils\ReflectionUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,9 +65,13 @@ class ZMConfigTest extends TestCase
|
|||||||
'<?php return ["patch" => "yes", "another array" => ["far", "baz"]];'
|
'<?php return ["patch" => "yes", "another array" => ["far", "baz"]];'
|
||||||
);
|
);
|
||||||
|
|
||||||
$config = new ZMConfig([
|
try {
|
||||||
__DIR__ . '/config_mock',
|
$config = new ZMConfig([
|
||||||
], 'development');
|
__DIR__ . '/config_mock',
|
||||||
|
], 'development');
|
||||||
|
} catch (ConfigException $e) {
|
||||||
|
self::fail($e->getMessage());
|
||||||
|
}
|
||||||
self::$config = $config;
|
self::$config = $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,8 +88,8 @@ class ZMConfigTest extends TestCase
|
|||||||
public function testGetValueWhenKeyContainsDot(): void
|
public function testGetValueWhenKeyContainsDot(): void
|
||||||
{
|
{
|
||||||
$this->markTestSkipped('should it be supported?');
|
$this->markTestSkipped('should it be supported?');
|
||||||
$this->assertEquals('c', self::$config->get('test.a.b'));
|
// $this->assertEquals('c', self::$config->get('test.a.b'));
|
||||||
$this->assertEquals('d', self::$config->get('test.a.b.c'));
|
// $this->assertEquals('d', self::$config->get('test.a.b.c'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetBooleanValue(): void
|
public function testGetBooleanValue(): void
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tests\ZM\Middleware;
|
namespace Tests\ZM\Middleware;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use Tests\TestCase;
|
||||||
use ZM\Logger\ConsoleLogger;
|
|
||||||
use ZM\Middleware\Pipeline;
|
use ZM\Middleware\Pipeline;
|
||||||
use ZM\Middleware\TimerMiddleware;
|
use ZM\Middleware\TimerMiddleware;
|
||||||
|
|
||||||
@@ -14,16 +13,6 @@ use ZM\Middleware\TimerMiddleware;
|
|||||||
*/
|
*/
|
||||||
class PipelineTest extends TestCase
|
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()
|
public function testPipeline()
|
||||||
{
|
{
|
||||||
$pipe = new Pipeline();
|
$pipe = new Pipeline();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tests\ZM\Utils;
|
namespace Tests\ZM\Utils;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use Tests\TestCase;
|
||||||
use ZM\Utils\ReflectionUtil;
|
use ZM\Utils\ReflectionUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tests\ZM\Utils;
|
namespace Tests\ZM\Utils;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use Tests\TestCase;
|
||||||
use ZM\Utils\ZMUtil;
|
use ZM\Utils\ZMUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user