ngyuki / phpower
在PHP中实现类似于Power Assert的断言。
v0.0.3
2022-03-31 14:13 UTC
Requires
- php: ^7.4|^8.0|^8.1
- ext-json: *
- microsoft/tolerant-php-parser: ^0.1
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.3.7|^6.0
This package is auto-updated.
Last update: 2024-08-29 05:53:30 UTC
README
在PHP中实现类似于 Power Assert 的断言。
安装
composer require --dev phpunit/phpunit ngyuki/phpower:dev-master
示例
使用 assert 编写测试代码。
<?php namespace Test; use PHPUnit\Framework\TestCase; class SomeTest extends TestCase { public function test_01() { $a = 1; $b = 2; $c = 3; assert(($a + ($b + 3)) === ($c * 3)); } public function test_02() { $s = "x\ny\nz"; assert("a\nb\nc" === strrev($s)); } public function test_03() { $a = [1,2,3]; $b = [7,8,9]; assert($a == array_reverse($b)); } public function test_ok() { $a = [1,2,3]; $b = [3,2,1]; assert($a == array_reverse($b)); } public function test_04() { $a = [ 'name' => 'alice', 'uid' => 1000, 'gid' => 1000, ]; assert($a == [ 'name' => 'bob', 'uid' => 2000, 'gid' => 2000, ]); } }
运行测试。
vendor/bin/phpunit tests/example/
显示以下结果。
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.
Runtime: PHP 7.4.2
Configuration: /path/to/phpower/phpunit.xml.dist
FFF.F 5 / 5 (100%)
Time: 261 ms, Memory: 4.00 MB
There were 4 failures:
1) Test\SomeTest::test_01
Assertion failed ($a + ($b + 3)) === ($c * 3) -> false
# $a -> 1
# $b -> 2
# $b + 3 -> 5
# $a + ($b + 3) -> 6
# $c -> 3
# $c * 3 -> 9
/path/to/phpower/tests/example/SomeTest.php:13
2) Test\SomeTest::test_02
Assertion failed "a\nb\nc" === strrev($s) -> false
# $s -> "x\ny\nz"
# strrev($s) -> "z\ny\nx"
/path/to/phpower/tests/example/SomeTest.php:19
3) Test\SomeTest::test_03
Assertion failed $a == array_reverse($b) -> false
# $a
# -> [
# 1,
# 2,
# 3,
# ]
#
# $b
# -> [
# 7,
# 8,
# 9,
# ]
#
# array_reverse($b)
# -> [
# 9,
# 8,
# 7,
# ]
#
/path/to/phpower/tests/example/SomeTest.php:26
4) Test\SomeTest::test_04
Assertion failed $a == ['name'=>'bob','uid'=>2000,'gid'=>2000,] -> false
# $a
# -> [
# "name" => "alice",
# "uid" => 1000,
# "gid" => 1000,
# ]
#
/path/to/phpower/tests/example/SomeTest.php:46
FAILURES!
Tests: 5, Assertions: 5, Failures: 4.
限制
仅适用于项目中本地安装的phpunit(不支持phpunit.phar)。