sebastianknott/hamcrest-object-accessor

为Hamcrest添加了一个可以访问对象属性和方法的Matcher。

3.0.0 2023-01-18 14:02 UTC

This package is auto-updated.

Last update: 2024-09-18 17:32:41 UTC


README

Build Status

Hamcrest对象访问器

本包扩展了Hamcrest中的matcher集合。有关Hamcrest的一般信息,请访问他们的网站。非常值得一试^^。

hasProperty

此Matcher尝试通过名称访问对象的属性。它使用获取器、公共属性、存在检查器和不存在检查器。

示例类

class HasPropertyFixture
{
    public $bla = 'blub';
    private $getable = 'blub';
    private $issable = true;
    private $hassable = true;
    private $notGettable = 'nope';

    public function getGetable()
    {
        return $this->getable;
    }

    public function isIssable()
    {
        return $this->issable;
    }

    public function hasHassable()
    {
        return $this->hassable;
    }
}

Matcher

$object = new hasPropertyFixture();
MatcherAssert::assertThat(
    $object, 
    hasProperty(
        'bla', // property name
        stringValue() // matcher the property value has to match
    )
);

MatcherAssert::assertThat(
    $object, 
    hasProperty(
        'Getable', 
        stringValue()
    )
);

MatcherAssert::assertThat(
    $object, 
    hasProperty(
        'isIssable', 
        boolValue()
    )
);

安装

您可以使用composer包括此包。

composer require --dev sebastianknott/hamcrest-object-accessor

我建议仅在开发环境中使用此matcher!

设置

一旦安装了包,您可以通过静态方式调用matcher...

HasProperty::hasProperty('bla', stringValue()));

...或者通过要求此包提供的src/functions.php