appeltaert / phpunit-assertion-message
增强PHPUnit断言消息 - 无需混乱即可调试断言。
v1.0.0
2018-02-23 22:12 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is not auto-updated.
Last update: 2024-09-21 15:33:20 UTC
README
PHPUnit断言消息

composer require --dev appeltaert/phpunit-assertion-message
用法
- 只需将你通常传递给断言的消息包装在
new PAM(string $message, [mixed $context,...])实例中。 - 使用
phpunit的 --debug 选项运行
$context 可以是任何东西,目前仅对 Symfony 的 Response 和 Request 对象有明确支持,但作为最后的手段,一个变量展平处理器将接管,以使任何内容在没有处理器可以处理你的上下文的情况下基本可读。
之前
这将把调试时的偶尔混乱变成这样。
$client->enableProfiler(); $response = $client->getResponse(); $this->assertTrue($response->isSuccessful(), "My message");
There was 1 failure:
1) Tests\AppBundle\Controller\DefaultControllerTest::testIndex
My message
// wtf is going on
// $this->assertTrue($client->getResponse()->isSuccessful())
// $collector = $profiler->getCollector()..
// var_dump($collector->...);
// echo $response->getContent();
// var_dump($response->headers->all();
// die.. die.. die..
之后
变成这样。
$client->enableProfiler(); $response = $client->getResponse(); $this->assertTrue($response->isSuccessful(), new PAM("My message", [$response, $profiler->getCollector('request')]));
There was 1 failure:
1) Tests\AppBundle\Controller\DefaultControllerTest::testIndex
My message
HTTP response: Code: 200
Content-type: text/html; charset=UTF-8
Cookies: 0: qwer=qewrqwer; path=/; httponly
Request profile: Format: html
StatusText: OK
Route: app_router_index
StatusCode: 200
ContentType: text/html; charset=UTF-8
PathInfo: /router/
工作原理
处理器
所有传递的上下文都由一系列 Processors 处理。首先接受上下文的处理器获胜。然后由 Printer 打印处理器。目前只有一个默认打印机 Plain,它会将所有内容直接打印成人类可读的块。
Symfony响应对象
$response = $client->getResponse(); $this->assertTrue($response->isSuccessful(), new PAM("My message", [$response]));
HTTP response: Code: 500
Headers: content-type: ["text\/html; charset=UTF-8"]
cache-control: ["no-cache, private"]
date: ["Fri, 23 Feb 2018 21:12:32 GMT"]
x-debug-token: ["771d20"]
Exception: qewr (500 Internal Server Error)
Symfony请求分析器
$this->assertSame( Response::HTTP_OK, $client->getResponse()->getStatusCode(), new PAM(sprintf('The %s public URL loads correctly.', $url), [$client->getProfile()->getCollector('request')]) );
Request profile: Format: html
Route: blog_index
StatusText: Internal Server Error
StatusCode: 500
ContentType: text/html; charset=UTF-8
PathInfo: /en/blog/
Method: GET
Locale: en
RouteParams: page: 1
_format: html
_locale: en
SessionAttributes: key: val
SessionMetaData: Created: Fri, 23 Feb 18 22:12:31 +0100
Last used: Fri, 23 Feb 18 22:12:31 +0100
Lifetime: 0
Action: AppBundle\Controller\BlogController::indexAction
数组
$someArray = []; $this->assertArrayHasKey("test", $array, new PAM("My message", [ 'qwerqwer' => 'qwerqewr', ['qwerqewr', 'qwerqwer'] ]));
Array: qwerqwer: qwerqewr
0: 0: qwerqewr
1: qwerqwer
配置
打印机
目前只有一个打印机,Plain。
静态设置所有选项
PAM::setDefaults($processors, $printer, $env);
覆盖环境
$env = new Env($debug = null, $verbose = null, $supportsColors = null);
PAM::setDefaults([], null, $env);
路线图
v1.1
处理器
- 更多Symfony
- 将它们移动到独立的建议存储库
- 使它们可插拔
打印机
- 根据环境进行独特转储,更多详细信息请使用 --verbose,不使用则不显示
- 检测交互式终端(例如,彩色输出),还检查ansi参数。
- posix_isatty,XTERM检查?