詹姆斯·哈尔斯/phpspec-array-contains-matchers

为 PHPSpec 提供额外的包含匹配器

2.0.0 2019-03-04 21:58 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:58:27 UTC


README

Build Status

为 PHPSpec 数组提供额外的包含匹配器

要求

该扩展已为 PHPSpec 3.x 和 PHP 5.6 或更高版本构建。

安装

通过 composer 安装扩展

$ composer require jameshalsall/phpspec-array-contains-matchers

然后您可以将扩展添加到您的 phpspec.yml,如下所示

extensions:
  JamesHalsall\PhpSpecContainsMatchers\Extension: ~

用法

该扩展目前支持以下匹配器

  • containOnly
  • containAnyOf
  • containAllOf

containOnly 匹配器

当您想确保数组值只包含指定的参数时,使用 containOnly 匹配器。它被实现是为了节省您多次调用 shouldContain()

假设 ->getArray() 返回 [1, 2, 3],则您的 spec 中的以下代码会通过

$this->getArray()->shouldContainOnly(1, 2, 3);

如果数组中有任何其他内容,将会导致 spec 失败。

同样,这可以通过 shouldNotContainOnly 取反。保持上述示例,以下代码会失败,因为数组中没有额外的值

$this->getArray()->shouldNotContainOnly(1, 2, 3);

然而,如果 ->getArray() 返回 [1, 2, 3, 4],则上述 spec 会通过。

containAnyOf 匹配器

当您想确保数组包含至少一个指定的参数时,使用 containAnyOf 匹配器。

假设 ->getArray() 返回 [1, 2, 3],则以下断言为真

$this->getArray()->shouldContainAnyOf(3, 4, 5);

然而,以下示例不会

$this->getArray()->shouldContainAnyOf(5, 6);

同样,这可以通过 shouldNotContainAnyOf 取反。保持示例,以下代码会失败

$this->getArray()->shouldNotContainAnyOf(3, 7);

这是因为它包含值 3,这是我们说它不应该包含的值之一。

containAllOf 匹配器

当数组包含传递给匹配器的所有元素时,containAllOf 匹配器将评估为 true。

如果 ->getArray() 返回 [1, 2, 3],则以下断言为真

$this->getArray()->shouldContainAllOf(2, 3);

然而,以下示例不会

$this->getArray()->shouldContainAllOf(2, 3, 4);

同样,这可以通过 shouldNotContainAnyOf 取反。保持相同的示例,以下代码会失败

$this->getArray()->shouldNotContainAllOf(2, 3)