m00t/phpspec-phpunit-matchers

将 IdentityMatcher 的比较符从 '===' 更改为 PHPUnit 的 assertEquals() 方法

dev-master 2015-01-23 17:14 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:44:23 UTC


README

此扩展将 PhpSpec 的 IdentityMatcher 从 "===" 更改为 PHPUnit 的 assertEquals() 方法。此外,它还将 PhpSpec 的差异更改为 PHPUnit 的差异。

动机

1. PHPUnit 拥有经过时间考验的比较库,它不仅提供 "==" 或 "===",所以我们可以直接使用它,不必担心使用 "shouldBeLike()" 而不是 "shouldReturn()"。

  1. 差异。

例如,我们有一些类

class Value
{
    private $value;

    public function __construct($value)
    {
        $this->value = $value;
    }
}

class TestValueObject
{
    public function getValue($value)
    {
        return new Value($value + 1);
    }
}

class TestValueObjectSpec extends ObjectBehavior
{
    function it_can_get_value()
    {
        $this->getValue(42)->shouldReturn(new \Value(42));
    }
}

此扩展未应用时的 PhpSpec 输出("shouldReturn" 已更改为 "shouldBeLike")

$ bin/phpspec run spec/TestValueObjectSpec.php -v
TestValueObject
  10  ✘ it can get value
      expected [obj:Value], but got [obj:Value].

        10     function it_can_get_value()
        11     {
        12         $this->getValue(42)->shouldBeLike(new \Value(42));
        13     }
        14 }
        15


       0 vendor/phpspec/phpspec/src/PhpSpec/Matcher/ComparisonMatcher.php:74
         throw new PhpSpec\Exception\Example\NotEqualException("Expected [obj:Valu"...)
       1 [internal]
         spec\TestValueObjectSpec->it_can_get_value()


                       100%                        1
1 specs
1 example (1 failed)
30ms

应用此扩展后的 PhpSpec 输出

$ bin/phpspec run spec/TestValueObjectSpec.php -v
TestValueObject
  10  ✘ it can get value
      expected [obj:Value], but got [obj:Value].

      --- Expected
      +++ Actual
      @@ @@
       Value Object (
      -    'value' => 42
      +    'value' => 43
       )


        10     function it_can_get_value()
        11     {
        12         $this->getValue(42)->shouldReturn(new \Value(42));
        13     }
        14 }
        15


       0 vendor/phpspec/phpspec/src/PhpSpec/Matcher/IdentityMatcher.php:83
         throw new PhpSpec\Exception\Example\NotEqualException("Expected [obj:Valu"...)
       1 [internal]
         spec\TestValueObjectSpec->it_can_get_value()


                       100%                        1
1 specs
1 example (1 failed)
48ms

安装

  1. 在您的 composer.json 中定义依赖关系
{
    "require-dev": {
        ...

        "m00t/phpspec-phpunit-matchers": "dev-master",
    }
}
  1. 安装/更新您的供应商
$ composer update m00t/phpspec-phpunit-matchers
  1. 通过在您的 phpspec.yml 中指定其类来激活扩展
# phpspec.yml
extensions:
    - M00t\PhpSpec\PHPUnitMatchers\Extension\PHPUnitMatchersExtension