atoum / blackfire-extension
使用atoum编写Blackfire测试套件
2.1.0
2016-01-08 13:25 UTC
Requires
- atoum/atoum: >=2.5,<3.0
- blackfire/php-sdk: >=1.4.5,<2.0
This package is auto-updated.
Last update: 2024-08-29 03:52:51 UTC
README
blackfire-extension 允许您在 atoum 中使用 blackfire 断言。
Blackfire PHP-SDK 有一个内置的 PHPUnit 集成。此扩展执行相同的操作,但针对 atoum。
示例
让我们来看这个例子。
namespace Tests\Units; use Example as TestedClass; use atoum; class Example extends atoum { public function testExemple() { $this ->blackfire ->assert('main.wall_time < 2s', "Temps d'execution") ->profile(function() { sleep(4); //just to make the test fail //some code to profile // ... //you also can run atoum assertions inside this callable //but beware, atoum's logic will also be profiled. $this->boolean(true)->isTrue(); }) ; } }
当运行此测试时,回调将被自动仪表化并在 Blackfire 上执行配置中定义的断言。如果它们失败,将显示 atoum 错误。上面的示例将产生以下输出
安装它
使用 composer 安装扩展
composer require --dev atoum/blackfire-extension
使用 atoum 配置文件启用并配置扩展
<?php // .atoum.php require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; $runner ->getExtension(mageekguy\atoum\blackfire\extension::class) ->setClientConfiguration(new \Blackfire\ClientConfiguration($_ENV['BLACKFIRE_CLIENT_ID'], $_ENV['BLACKFIRE_CLIENT_TOKEN'])) ->addToRunner($runner) ;
其他示例
定义自定义指标
$this ->blackfire ->defineMetric(new \Blackfire\Profile\Metric("example_method_calls", "=Example::method")) ->assert("metrics.example_method_calls.count < 10") ->profile(function() { $testedClass = new TestedClass(); for ($i = 0; $i < 8; $i++) { $testedClass->method(); } }) ;
您可以在 Blackfire 文档的自定义指标部分了解更多。
传递自己的配置配置文件
$this ->given( $profileConfiguration = new \Blackfire\Profile\Configuration(), $profileConfiguration->setTitle('Profile configuration title'), $testedClass = new TestedClass() ) ->blackfire ->setConfiguration($profileConfiguration) ->assert("main.peak_memory < 10mb") ->profile(function() use ($testedClass) { $testedClass->method(); }) ;
您可以在 Blackfire 文档的配置基本配置部分了解更多。
传递自定义客户端
您可以在 .atoum.php
配置文件中传递 blackfire 客户端(在加载扩展时)。在这种情况下,客户端将在所有 blackfire 断言中使用。您还可以在 blackfire
断言中加载/覆盖它。例如
$this ->given( $config = new \Blackfire\ClientConfiguration($_ENV['BLACKFIRE_CLIENT_ID'], $_ENV['BLACKFIRE_CLIENT_TOKEN']); $client = new \Blackfire\Client($config); ) ->blackfire(client) ->assert('main.wall_time < 2s') ->profile(function() { //code to profile }) ;
测试过滤
为了避免在 blackfire 扩展未加载时运行测试,您可以使用 @extensions
注解。
/** * @extensions blackfire */ public function testExemple() { $this ->blackfire($this->getBlackfireClient()) ->defineMetric(new \Blackfire\Profile\Metric("example_method_calls", "=Example::method")) ->assert("metrics.example_method_calls.count < 10") ->profile(function() { $testedClass = new TestedClass(); for ($i = 0; $i < 8; $i++) { $testedClass->method(); } }) ; }
您可以在测试方法或测试类上添加此注解。
然后,在运行测试时,如果扩展不存在/未加载,则将跳过带有此注解的类/方法
Success (1 test, 0/1 method, 0 void method, 1 skipped method, 0 assertion)!
> There is 1 skipped method:
=> Tests\Units\Example::testExemple(): PHP extension 'blackfire' is not loaded
您还可以使用 atoum 的标签 和 ruler 扩展 仅运行 blackfire 测试。
链接
许可
blackfire-extension 在 MIT 许可下发布。有关详细信息,请参阅附带许可证文件。