xp-forge/assert

此包已被废弃,不再维护。未建议替代包。

XP 框架的断言

v4.1.2 2022-02-27 08:51 UTC

README

Build status on GitHub XP Framework Mdodule BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

在 XP 框架的 unittest 包之上提供灵活的断言。

示例

use unittest\assert\{Assert, All};

class ExampleTest {

  #[@test]
  public function succeeds() {
    Assert::that(['The Dude'])->hasSize(1)->contains('The Dude');
  }

  #[@test]
  public function fails() {
    All::of(function() { 
      Assert::that('localhost')->startsWith('www');
      Assert::that('example.com')->endsWith('.org');
    });
  }
}

运行此测试将产生一个成功和一个失败的测试。与常规 unittest 消息相比,其格式更加易于阅读。

此外,此库的断言也更加灵活:如上所示,我们使用 All::of() 调用包裹了链,这将产生所有失败的断言,而不仅仅是第一个。

$ xp test ExampleTest.class.php
[.F]

F unittest.TestAssertionFailed(test= ExampleTest::fails, time= 0.002 seconds) {
  unittest.AssertionFailedError{ The following 2 assertions have failures:
    1: unittest.AssertionFailedError{ Failed to verify that "localhost" starts with "www" }
    2: unittest.AssertionFailedError{ Failed to verify that "example.com" ends with ".org" }
   }
    at unittest.assert.All::of() [line 17 of ExampleTest.class.php]
    at ExampleTest::fails() [line 0 of StackTraceElement.class.php]
    at ReflectionMethod::invokeArgs() [line 90 of Method.class.php]
    at lang.reflect.Method::invoke() [line 334 of TestSuite.class.php]
    at unittest.TestSuite::runInternal() [line 565 of TestSuite.class.php]
    at unittest.TestSuite::run() [line 369 of Runner.class.php]
    at xp.unittest.Runner::run() [line 380 of Runner.class.php]
    at xp.unittest.Runner::main() [line 281 of class-main.php]

 }

✗: 2/2 run (0 skipped), 1 succeeded, 1 failed
Memory used: 1610.91 kB (1710.45 kB peak)
Time taken: 0.005 seconds

断言

通用断言

  • is(unittest.assert.Condition $condition) - 断言给定的条件匹配
  • isNot(unittest.assert.Condition $condition) - 断言给定的条件不匹配
  • isEqualTo(var $compare) - 断言值等于给定的比较
  • isNotEqualTo(var $compare) - 断言值不等于给定的比较
  • isNull() - 断言值为 null
  • isTrue() - 断言值为 true
  • isFalse() - 断言值为 false
  • isIn(var $enumerable) - 断言值在给定的可枚举中
  • isNotIn(var $enumerable) - 断言值不在给定的可枚举中
  • isInstanceOf(string|lang.Type $type) - 断言值是给定类型

具有特殊意义的类型

  • isEmpty() - 断言字符串、数组或映射为空
  • hasSize(int $size) - 断言字符串长度、数组或映射大小
  • startsWith(var $element) - 断言字符串或数组在开始处包含给定的元素
  • endsWith(var $element) - 断言字符串或数组在末尾包含给定的元素
  • contains(var $element) - 断言字符串、数组或映射包含给定的元素
  • doesNotContain(var $element) - 断言字符串、数组或映射不包含给定的元素

对于数字

  • isGreaterThan(int|double $comparison) - 断言 值 > 比较值
  • isLessThan(int|double $comparison) - 断言 值 < 比较值
  • isBetween(int|double $start, int|double $end) - 断言 值 >= 起始值 && 值 <= 结束值
  • isCloseTo(int|double $comparison, int|double $tolerance) - 断言值接近(根据容差定义)

转换

在调用断言之前可以对值进行转换。

提取直接在实例上工作(使用属性和以 get 前缀以及普通获取器)

$tim= new Person(6100, 'Tim Tailor');
Assert::that($tim)->extracting('name')->isEqualTo('Tim Tailor');

您还可以传递一个闭包来更精确地控制要提取的内容

$tim= new Person(6100, 'Tim Tailor');
Assert::that($tim)->extracting(function($p) { return $p->name(); })->isEqualTo('Tim Tailor');

对于映射,提取通过字符串键访问元素

$person= ['id' => 6100, 'name' => 'Test', 'age' => 42];
Assert::that($person)->extracting('age')->isEqualTo(42);

将数组传递给 extracting() 将提取多个元素,并按传递的顺序以数组形式返回

$person= ['id' => 6100, 'name' => 'Test', 'age' => 42];
Assert::that($person)->extracting(['name', 'age'])->isEqualTo(['Test', 42]);

将映射传递给 extracting() 将提取多个元素,并以映射形式返回

$person= ['id' => 6100, 'name' => 'Test', 'age' => 42];
Assert::that($person)->extracting(['identifier' => 'id'])->isEqualTo(['identifier' => 6100]);

对于数组,extracting() 方法对每个元素应用提取

$people= [new Person(1, 'Queen'), new Person(2, 'King')];
Assert::that($people)->extracting('name')->isEqualTo(['Queen', 'King']);