guitarneck / taphp
PHP 的 TAP 生成库。
Requires
- php: >=5.3.0
- composer-runtime-api: ^2.0.0
Suggests
- guitarneck/taphp-color: TAP consumer in CLI colors and/or HTML.
This package is auto-updated.
Last update: 2024-09-07 00:52:54 UTC
README
taphp
PHP 的 TAP 生成库,基于 tape for node。
此库不遵循 psr-0/4 规范。实际上,它包含组织运行测试的功能,这使得它与 composer 的自动加载方式不兼容。
测试过的 PHP 版本:5.6.9,7.4.13,8.0.0
示例
使用 composer
require dirname(__DIR__).'/vendor/guitarneck/taphp/taphp.lib.php';
不使用 composer
require_once 'taphp.lib.php';
test('timing test', function ($t) { $t->plan(2); $t->equal(get_class($t), 'TAPHP'); $start = time(); sleep(1); $t->equal(time() - $start, 0); });
$ php .\test.php
TAP version 13
# timing test
ok 1 it should strictly be equal
not ok 2 it should strictly be equal
---
actual : 1
expected: 0
...
1..2
# tests 2 (1.0868s)
# pass 1
# fail 1
TAPHP
此类是一个单例。
instance()
返回此类的实例。如果您确实需要它。
discardExit ()
此方法防止 TAPHP 使用退出代码终止。在某些情况下可能很有用,例如还有任务要执行。
函数
函数和方法几乎与 tape 中的相同,但由于 PHP 语言的一些差异,也有一些不同。
所谓的 deep
方法是为了与 tape
兼容而存在的。PHP 本身就支持它。
only ( [$name], [$options], $callback )
声明一个将运行的唯一测试。
skip ( [$name], [$options], $callback )
声明一个将被跳过的测试。它将不会运行。
test ( [$name], [$options], $callback )
声明一个新的测试。 $name
和 $options
是可选的。 $callback
将在先前的测试之后触发,并通过参数提供对 TAPHP
对象方法的访问。
todo ( [$name], [$options], $callback )
声明一个还需要做的测试。允许失败。
选项
可用的 options
有
- 'todo' => true|false。请参阅函数 todo 和方法 todo。
- 'skip' => true|false。请参阅函数 skip 和方法 skip。
- 'only' => true|false。请参阅函数 only。
- 'timeout' => null|integer。为测试设置超时,超时后测试将失败。请参阅方法 timeout。
方法
assert ( $condition, [$text] )
断言 condition
是真,可选地带有断言描述 $text
。
别名:ok
,true
bailout ( [$text] )
生成一个立即退出并停止所有测试的代码,可选地带有原因 $text
。
comment ( $text )
生成一个带有消息 $text
的注释。
deepEqual ( $any, $val, [$text] )
断言 $any === $val
,可选地带有断言描述 $text
。
别名:deepEquals
,isEquivalent
,same
,deep_equal
deepLooseEqual ( $any, $val, [$text] )
断言 $any == $val
,可选地带有断言描述 $text
。
别名:deep_loose_equal
end ( [$error] )
声明测试的结束,可选地生成 $error
。
error ( $error, [$text] )
为给定的 $error
生成一个失败,作为一个异常对象类型,可选地带有 $test
描述。
别名: ifError
、ifErr
、iferror
、if_error
exception ( $exception, [$text] )
为给定的 $exception
生成一个失败,作为一个 Exception 类型的对象,可选的 $test
描述。
别名: ifException
、ifExcept
、ifExpt
、ifExp
、ifexception
、if_exception
fail ( [$text], [$options] )
生成一个带有消息 $text
的失败断言。
looseEqual ( $any, $val, [$text] )
断言 $any == $val
,可选地带有断言描述 $text
。
别名: looseEquals
、loose_equal
no ( $condition, [$text] )
断言 condition
是假的,带有可选的断言描述 $text
。
别名: notOK
、false
、notok
、not_ok
notDeepEqual ( $any, $val, [$text] )
断言 $any !== $val
,带有可选的断言描述 $text
。
别名: notDeepEquals
、notEquivalent
、notDeeply
、notSame
、isNotDeepEqual
、isNotDeeply
、isNotEquivalent
、isInequivalent
、not_deep_equal
、not_same
notDeepLooseEqual ( $any, $val, [$text] )
断言 $any != $val
,带有可选的断言描述 $text
。
别名: not_deep_loose_equal
notLooseEqual ( $any, $val, [$text] )
断言 $any != $val
,带有可选的断言描述 $text
。
别名: notLooseEquals
、not_loose_equal
notStrictEqual ( $any, $val, [$text] )
断言 $any !== $val
,带有可选的断言描述 $text
。
别名: notEqual
、notEquals
、isNotEqual
、doesNotEqual
、isInequal
、notStrictEquals
、isNot
、not
、not_strict_equal
、not_equal
、is_not_equal
、is_not
pass ( [$text], [$options] )
生成一个带有消息 $text
的通过断言。
plan ( $n )
声明即将运行的断言数量。之后将自动调用 end()
,如果断言数量不匹配,将发生错误。
restoreTimeLimit ()
恢复 PHP 的 max_execution_time
。
setTimeLimit ( $seconds )
更改 PHP 的 max_execution_time
。
skip ( [$text], [$options] )
生成一个带有可选的 $text
的 skip
,并跳过正在运行的测试。
strictEqual ( $any, $val, [$text] )
断言 $any === $val
,可选地带有断言描述 $text
。
别名: equal
、equals
、isEqual
、strictEquals
、is
、strict_equal
、is_equal
test ( [$name], [$options], $callback )
在运行时生成一个新的测试。
throws ( $thrower, [$throwed], [$text] )
断言函数调用 $thrower
抛出异常。 $throwed
可以是一个字符串、一个正则表达式、一个异常、一个描述一些异常属性的数组,或 null。
timeout ( $callback, $ms, [$text] )
生成一个断言,应在 $ms
微秒之前运行。或者,如果可选的 $text
存在,则失败。
todo ( [$text], [$options] )
在测试中生成一个带有可选的 $text
的 todo
,将下一个断言切换到 todo
模式。