atoum/bdd-extension
Composer 安装命令:
composer require atoum/bdd-extension
包简介
atoum Spec BDD extension
README 文档
README
This extensions helps you to write your tests (specs) in a Behavior Driven development fashion.
Example
public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format underscore separated method name') ; }
and the output will look like this :
Install it
Install extension using composer:
composer require --dev atoum/bdd-extension
Enable the extension using atoum configuration file:
<?php // .atoum.php require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; use atoum\atoum\bdd; $extension = new bdd\extension($script); $runner->addExtension($extension);
Use it
Your first spec
The extension requires you to place your specs in a specs namespace and make them extend atoum\spec. Let's write a
spec for the jubianchi\example\formatter class:
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format underscore separated method name') ; } public function shouldFormatCamelCaseMethodName() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format camel case method name') ; } public function should_formatMixed__MethodName() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format mixed method name') ; } }
Running your first spec
$ vendor/bin/atoum -d specs > jubianchi\example\formatter... ✘ should format camel case method name 256: Tested class 'jubianchi\example\formatter' does not exist for test class 'jubianchi\example\specs\formatter' File: /atoum-bdd-extension/formatter.php ✘ should format underscore separated method name 256: Tested class 'jubianchi\example\formatter' does not exist for test class 'jubianchi\example\specs\formatter' File: /atoum-bdd-extension/formatter.php ✘ should format mixed method name 256: Tested class 'jubianchi\example\formatter' does not exist for test class 'jubianchi\example\specs\formatter' File: /atoum-bdd-extension/formatter.php Failure (1 spec, 3/3 examples, 0 void example, 0 skipped example, 0 uncompleted example, 0 failure, 3 errors, 0 exception)!
And run it again until you get green:
$ vendor/bin/atoum -d specs > jubianchi\example\formatter... ✔ should format underscore separated method name ✔ should format camel case method name ✔ should format mixed method name Success (1 spec, 3/3 examples, 0 void example, 0 skipped example, 6 assertions)!
You can use the --loop flag to work incrementally: vendor/bin/atoum -d specs --loop
Reading the report
The spec report uses a set of icon to identify examples' statuses:
✘to mark failed examples (error, exception or assertion failure)✔to mark passed examples↣to mark skipped examples∅to mark void examples (not implemented specs)
Spec syntax
Invoking methods
Invoking a method is the process of calling a method on a tested object in a way that will allow the tester to assert on method behavior:
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_invoke_method() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) // ... ; } }
This example will invoke the format method of the $formatter object with one argument, __FUNCTION__.
There are several way of invoking method, depending on which syntax you prefer:
$this->invoking->format(__FUNCTION__)->on($formatter); $this->invoking('format', __FUNCTION__)->on($formatter); $this->invokingFormat(__FUNCTION__)->on($formatter);
Asserting on returned values
The spec extension provides some shortcut to assert on method return value.
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format underscore separated method name') ; } }
As you can see, shouldReturn lets you assert on invoked method return value. Again, there are several way of doing the
same:
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldReturn('...'); $this->invoking->format(__FUNCTION__)->on($formatter)->returns('...'); $this->invoking->format(__FUNCTION__)->on($formatter)->should->return('...');
Calling shouldReturn with a parameter will fallback on the atoum\asserters\variable::isEqual assertion which is the
less strict one in atoum. You can of course use strict assertions to do some type checking:
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldReturn->string->isEuqlaTo('...')
Asserting on thrown exceptions
Asserting on exception is quite simple and similar to asserting on returned values:
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(uniqid())->on($formatter) ->shouldThrow('invalidArgumentException') ; } }
As for shouldReturn, shouldThrow provides some alternative synatxes:
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldThrow('exception'); $this->invoking->format(__FUNCTION__)->on($formatter)->throws('exception'); $this->invoking->format(__FUNCTION__)->on($formatter)->should->throw('exception');
Calling shouldThrow this way will fallback on atoum\asserters\exception::isInstanceOf, if you don't want to check
the exception type, you can simple omit the argument:
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldThrow; $this->invoking->format(__FUNCTION__)->on($formatter)->throws; $this->invoking->format(__FUNCTION__)->on($formatter)->should->throw;
Of course, you can also check the exception message using regular assertions:
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldThrow->hasMessage('...'); $this->invoking->format(__FUNCTION__)->on($formatter)->throws('invalidArgumentException')->hasMessage('...');
Links
Licence
bdd-extension is released under the BSD-3-Clause. See the bundled LICENSE file for detail.
atoum/bdd-extension 适用场景与选型建议
atoum/bdd-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.02k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「BDD」 「TDD」 「atoum」 「test」 「unit testing」 「spec」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 atoum/bdd-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 atoum/bdd-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 atoum/bdd-extension 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Helper that decorates any SUS with a phpspec lazy object wrapper
Behat Context for testing Symfony Api
atoum telemetry reports extension
Provides access to magento2 object manager from behat and allows to change magento config settings temporarly
Automatically run atoum's loop mode on file change
Cobertura report for atoum
统计信息
- 总下载量: 35.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 21
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-01
