tests-always-included / health-check
一个通用的用于与Symfony2及类似项目一起使用的HealthChecker组件。
v0.0.2
2014-08-17 18:09 UTC
Requires
- symfony/event-dispatcher: ~2.5
Requires (Dev)
- phpunit/phpunit: ~3.7
Suggests
- symfony/console: ~2.5
This package is not auto-updated.
Last update: 2024-09-24 01:41:06 UTC
README
一个通用的用于与Symfony2及类似项目一起使用的HealthChecker组件。
功能
- 在TestSuites和TestGroups中组织测试。
- 可自定义的报告。
- 可自定义确定最终成功/健康状态的方法。
- 拦截错误的能力(对于质量较差的SOAP客户端很有用)。
- 用于执行测试的服务(无状态)。
安装
作为独立仓库
此方法通常用于开发。
git clone https://github.com/tests-always-included/HealthCheckComponent.git HealthCheckComponent
/path/to/your/composer.phar install
作为依赖项
此方法会将组件作为项目的一部分。 composer.json
{
...
dependencies: {
...,
"tests-always-included/health-check": "@dev-master"
}
}
告诉composer更新您的项目供应商。 /path/to/your/composer.phar install
测试
测试是用 PHPUnit框架 编写的。
cd /path/to/your/clone/of-this-repository
./vendor/bin/phpunit -c .
用法
创建一些测试。
每个测试都必须扩展此库中的抽象定义。不提供默认实现。
示例场景:
- 您的项目连接到远程服务。
- 此远程服务可能需要身份验证。
- 您站点的健康状况依赖于该身份验证。
namespace My\Project\HealthCheck\Tests;
use TestsAlwaysIncluded\HealthCheck\Test\Test;
class MyTest extends Test
{
public function execute()
{
// Example: Authentication check.
$someService->authenticate();
if ($someService->isAuthenticated()) {
$this->pass('My service is authenticating as expected.');
return;
}
if ($someService->timedOut()) {
$this->fail('My service appears to be down.');
return;
}
if ($someService->isNotConfigured()) {
$this->skip('We might be running this from a lower environment.');
return;
}
if ($someService->returnedGarbage()) {
$this->error('My service is doing something I didnt expect.');
return;
}
}
}
建立您的TestSuite(s)
$test = new MyTest;
$testGroup = new TestGroup('RemoteServicesAreEverywhere');
$testGroup->addTest($test);
$testSuite = new TestSuite('ThisIsProbablyABundleInMyProject');
$testSuite->addTestGroup($testGroup);
将TestSuite(s)附加到您的HealthCheck
$healthCheck = new HealthCheck;
$healthCheck->addTestSuite($testSuite);
定义您想要的报告(如果有)。
// Example: A basic console dot-F reporter is included with the repo.
$reporter = new ConsoleOutputReporter();
// A second, optional argument allows you to alias the reporter.
// Otherwise, the Reporter's class name is used.
$healthCheck->registerReporter($reporter, 'consoleReporter');
执行您创建的场景。
$runner = new HealthCheckRunner;
// You will most likely obtain an EventDispatcher from your DependencyInjection Container.
$runner->setEventDispatcher($eventDispatcher);
$runner->run($healthCheck);
确定您的系统是否健康。
// You can define your own strategies, as well.
// A simple PassFailSuccessStrategy is included with the repo.
$successStrategy = new PassFailSuccessStrategy();
echo $healthCheck->passed($successStrategy) ? 'Everything is AOK!' : 'Something might be wrong.';
未来考虑
- 如果依赖测试失败/通过等,允许跳过测试。
- 提供执行测试的默认命令实现。
- 允许附加多个实例的Reporter(可能在以相同格式输出简单和详细报告时很有用)。
- 提供从数组配置中组装HealthCheck实例的默认加载器。