karriere/phpspec-matchers

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

包含额外匹配器的phpspec扩展

安装次数: 50,490

依赖项: 4

建议者: 0

安全: 0

星标: 31

关注者: 13

分支: 5

开放问题: 1

类型:phpspec-extension

v3.0.0 2019-11-18 10:42 UTC

This package is auto-updated.

Last update: 2021-01-27 12:50:51 UTC


README

687474703a2f2f7777772e6b617272696572652e61742f696d616765732f6c61796f75742f6b61746c6f676f2e737667     Build Status codecov StyleCI

phpspec 匹配器集合

此包包含一组额外的 phpspec 匹配器。

安装

您可以通过 composer 安装此包

composer require karriere/phpspec-matchers

为了使用匹配器,您需要将以下定义添加到您的 phpspec.yml

extensions:
    Karriere\PhpSpecMatchers\Extension: ~

匹配器使用

此包中的所有自定义匹配器都实现了正例和反例。例如,您可以使用

$this->method()->shouldBeAnyOf(1, 2, 3);

以及

$this->method()->shouldNotBeAnyOf(1, 2, 3);

匹配器

通用匹配器

Json 匹配器

通用匹配器

beAnyOf

此匹配器允许检查返回值是否与一组值匹配。假设您有一种机制可以获取介于 2 和 4 之间的整数。您可以使用 shouldBeAnyOf 匹配器

$this->method()->shouldBeAnyOf(2, 3, 4);

beSomeOf

此匹配器允许检查返回的数组值是否包含在值集中。

// $this->method() may return [1, 2, 3]
$this->method()->shouldBeSomeOf(1, 2, 3, 4, 5);

rangeBetween

此匹配器允许检查给定的返回值是否在数值范围内。

$this->method()->shouldRangeBetween(2, 4);
$this->method()->shouldRangeBetween(0.1, 0.9);

beEmpty

此匹配器允许检查给定的返回值是否为空。实现使用了 empty 实现。

$this->method()->shouldBeEmpty();

beNull

此匹配器允许检查给定的返回值是否为null。实现使用is_null实现。

$this->method()->shouldBeNull();

beLessThan

此匹配器允许检查给定的返回值是否小于指定的值。

$this->method()->shouldBeLessThan(10);

beGreaterThan

此匹配器允许检查给定的返回值是否大于指定的值。

$this->method()->shouldBeGreaterThan(10);

Json 匹配器

beJson

此匹配器检查返回值是否为有效的JSON字符串

$this->method()->shouldBeJson();

haveJsonKey

此匹配器检查返回的JSON字符串是否包含JSON键。

$this->method()->shouldHaveJsonKey('key');

要匹配子键,可以使用点表示法。例如,假设以下JSON结构

{
  "key": {
    "subkey": "value"
  }
}

此检查的键为 'key.subkey'

$this->method()->shouldHaveJsonKey('key.subkey');

haveJsonKeyWithValue

此匹配器检查返回的JSON字符串是否包含指定的JSON键和值。子键的点表示法也可以应用。

$this->method()->shouldHaveJsonKeyWithValue('key.subkey', 'value');

许可

Apache License 2.0 请参阅LICENSE获取更多信息。