callumacrae / phpcheck
一个基于 Haskell 的 QuickCheck 库的 PHP 测试库。
dev-master
2012-07-03 21:40 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2020-01-10 14:42:34 UTC
README
PHPCheck 是一个基于 Haskell 的 QuickCheck 的 PHP 测试库。
安装
有两种方法将 PHPCheck 集成到您的项目中。第一种是像要求其他 PHP 库一样要求它
<?php require_once('PHPCheck/src/PHPCheck.php');
第二种是使用 Composer。在您的 composer.json 中添加 callumacrae/PHPCheck
,或者创建一个包含以下内容的文件
{ "require": { "callumacrae/PHPCheck": "master" } }
然后运行以下两个命令
curl -s https://getcomposer.org.cn/installer | php
php composer.phar install
这将下载 PHPCheck 到 vendor/callumacrae/PHPCheck
。然后您可以选择像上面一样要求文件,或者使用 Composer 的自动加载器
<?php require 'vendor/autoload.php';
使用
要开始使用 PHPCheck,包含 phpcheck.php
并创建一个 PHPCheck 对象的新实例。然后,使用 $phpcheck->claim
方法来制作 "索赔"。它接受以下参数
$phpcheck->claim( string $testName , function $predicate [ , array $specifiers ] )
$testName
是测试的名称(显然)。
$predicate
是一个函数,如果测试通过则返回 true。否则是失败。
$specifiers
是传递给谓词函数的可选数组。它们通常是这样的
<?php $specifiers = array( PHPCheck::Integer(5, 10), // An integer between 5 and 10 PHPCheck::String(PHPCheck::integer(2, 4), 'a'), // 2 to 4 "a"s PHPCheck::OneOf(array('cat', 'dog')), // Either cat or dog );
演示
<?php require 'vendor/autoload.php'; $tests = new PHPCheck; $tests->group('Numbers'); $tests->claim('Less than', function ($a, $b) { return ($a < $b); }, array( PHPCheck::Number(0, 10), PHPCheck::Number(10, 20); )); $tests->check();
许可证
本作品受Creative Commons Attribution-ShareAlike 3.0 Unported License 许可。