way / phpunit-wrappers
该软件包已被废弃,不再维护。没有建议的替代软件包。
PHPUnit 的 Should 和 Assert 包装器
dev-master
2013-03-21 19:46 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-05-30 12:43:15 UTC
README
本项目提供两个包装器(您可以添加更多)围绕 PHPUnit 的断言库。例如,而不是键入
$this->assertEquals(4, 2 + 2);
您可以改为做
Assert::equals(4, 2 + 2); # or Should::equal(4, 2 + 2);
为了提高可读性,当使用 Should 时,您可以预先添加 be
或 have
,如下所示
Should::beTrue(true); Should::haveCount(2, ['a', 'b']);
别名
此外,您还可以注册自己的别名。
Should::getInstance()->registerAliases([ 'beCakesAndPies' => 'assertTrue' ]); # or Assert::getInstance()->registerAliases([ 'eq' => 'assertEquals' ]);
现在,您可以在测试中使用 Should::beCakesAndPies
和 Assert::eq
,它们分别映射到 assertTrue
和 assertEquals
。
安装
通过 Composer 安装这些辅助工具。将以下内容添加到您的 composer.json
文件中:
{ "require-dev": { "way/phpunit-wrappers": "dev-master" } }
然后,像往常一样,运行 composer install
或 composer update
进行开发。
composer install --dev
该命令将那些类拉入 Way\Tests
命名空间下。
然后,在您的测试文件中,使用您想要的断言包装器(或两者都使用)。
<?php use Way\Tests\Should; class DemoTest extends PHPUnit_Framework_TestCase { public function testItWorks() { Should::beTrue(true); } }
如果您愿意,您可以选择性地将 Should 别名为更短的名称,例如 S
。
use Way\Tests\Should as S;
这将允许使用 S::beTrue()
。这是一个选项,但我更喜欢使用更具可读性的 Should
。
就是这样!这里有一些示例
<?php use Way\Tests\Should; use Way\Tests\Assert; class DemoTest extends PHPUnit_Framework_TestCase { public function testItWorks() { Should::beTrue(true); Assert::true(true); Should::equal(4, 2 + 2); Assert::equals(4, 2 + 2); Should::beGreaterThan(20, 21); Assert::greaterThan(20, 21); Should::contain('Joe', ['John', 'Joe']); Should::have('Joe', ['John', 'Joe']); Assert::has('Joe', ['John', 'Joe']); Assert::has('Joe', 'Joey'); Should::haveCount(2, ['1', '2']); Assert::count(2, ['1', '2']); } }
别忘了将 Composer 的自动加载器包含到您的项目中某处:
require_once 'vendor/autoload.php'
。或者,您可以在运行 PHPUnit 时指定引导选项:phpunit --bootstrap vendor/autoload.php --colors SomeTest.php
记住:这些是围绕 PHPUnit 断言的简单包装器。有关完整列表,请参阅侧边栏 此处。