【问题标题】:Custom PhpSpec matcher and/or extension not working自定义 PhpSpec 匹配器和/或扩展不起作用
【发布时间】:2019-03-21 14:16:58
【问题描述】:

我正在尝试测试课程是否是最终课程。由于我没有找到默认匹配器(或任何其他干净的测试方法),我决定创建一个自定义扩展,添加一个新的匹配器来做到这一点,但我无法让它工作。

我已经尝试过使用内联匹配器,如下所示:

public function getMatchers(): array
{
    return [
        'beFinal' => function ($subject) {
            $reflection = new \ReflectionClass($subject);

            if (!$reflection->isFinal()) {
                throw new FailureException('Expected subject to be final, but it is not.');
            }
            return true;
        },
    ];
}

当我打电话给$this->shouldBeFinal(); 时,这很好用。问题是当我调用$this->shouldNotBeFinal(); 时,它会输出一条通用消息:[obj:Class\To\Test] not expected to beFinal(), but it did.,而不是我想显示的消息。

另一个问题是我不希望只在一个课程上使用这个。这就是为什么我决定对其进行扩展。

这是我得到的:

phpspec.yml

extensions:
    PhpSpecMatchers\Extension: ~

PhpSpecMatchers/Extension.php

<?php

declare(strict_types=1);

namespace PhpSpecMatchers;

use PhpSpec\ServiceContainer;
use PhpSpecMatchers\Matchers\BeFinalMatcher;

class Extension implements \PhpSpec\Extension
{
    public function load(ServiceContainer $container, array $params): void
    {
        $container->define(
            'php_spec_matchers.matchers.be_final',
            function ($c) {
                return new BeFinalMatcher();
            },
            ['matchers']
        );
    }
}

PhpSpecMatchers/Matchers/BeFinalMatcher.php

<?php

declare(strict_types=1);

namespace PhpSpecMatchers\Matchers;

use PhpSpec\Exception\Example\FailureException;
use PhpSpec\Matcher\BasicMatcher;

class BeFinalMatcher extends BasicMatcher
{
    public function supports(string $name, $subject, array $arguments): bool
    {
        return $name === 'beFinal';
    }

    public function getPriority(): int
    {
        return 0;
    }

    protected function matches($subject, array $arguments): bool
    {
        $reflection = new \ReflectionClass($subject);

        return $reflection->isFinal();
    }

    protected function getFailureException(string $name, $subject, array $arguments): FailureException
    {
        return new FailureException('Expected subject to not be final, but it is.');
    }

    protected function getNegativeFailureException(string $name, $subject, array $arguments): FailureException
    {
        return new FailureException('Expected subject to be final, but it is not.');
    }
}

每当我尝试使用此配置调用 $this-&gt;beFinal(); 时,规范被破坏并显示以下消息:method [array:2] not found.。例如,如果我将 isFinal() 方法添加到我正在测试的类并返回 true,它会通过 $this-&gt;shouldBeFinal(); 而对于 $this-&gt;shouldNotBeFinal(); 则失败,但我不想添加它方法。我应该在没有它的情况下工作,据我所知,它应该能够像那样工作,对吧?

我也尝试将自定义 suites 添加到我的 phpspec.yml 中,如下所示:

suites:
    matchers:
        namespace: PhpSpecMatchers\Matchers
        psr4_prefix: PhpSpecMatchers\Matchers
        src_path: src/PhpSpecMatchers/Matchers
        spec_prefix: spec/PhpSpecMathcers/Matchers

但这不会改变任何事情。我还尝试将以下配置添加到 phpspec.yml

extensions:
    PhpSpecMatchers\Extension:
        php_spec_matchers:
        src_path: src
        spec_path: spec

这也不会改变任何事情。

我尝试过的另一件事是放弃扩展方法,只在 phpspec.yml 中声明我的数学,如下所示:

matchers:
    - PhpSpecMatchers\Matchers\BeFinalMatcher

如您所料:结果相同。

确实调用了 PhpSpecMatchers\Extension 中的加载(通过简单的var_dump(…); 进行测试),但它似乎在 PhpSpecMatchers\Matchers\BeFinalMatcher,因为我没有从任何var_dump(…);得到任何输出

我遵循了来自 symfonycasts、phpspec 文档本身和我发现的其他一些 github 项目的教程和示例,它们几乎都与我的代码相同(除了命名空间、目录结构和类似的东西),所以我很友善在这里不知所措。

如何才能成功调用$this-&gt;shouldBeFinal();$this-&gt;shouldNotBeFinal();

非常感谢可以在这里帮助我的人。

P.S.:我还在 phpspec 的 github 上发布了这个 issue

【问题讨论】:

    标签: php phpspec


    【解决方案1】:

    所以显然优先级太低(请参阅this comment我的 phpspec 的 github 问题)。 PhpSpec\Matcher\IdentityMatchershouldBe 的来源)扩展自 PhpSpec\Matcher\BasicMatcher,其中优先级设置为 100。 由于我的设置为 0,因此它首先得到了我的(我认为),因此没有正确执行。我已将我的优先级设置为 101,并且它完美无缺(我发现除了我切换了正面和负面信息之外)。

    【讨论】:

    • 你应该在 Packagist 上发布你的扩展!
    • @DamianDziaduch 好主意。我会尝试设置它(之前没有创建包的经验)并在完成后在此处发布链接。
    • @DamianDziaduch,正如我所承诺的,我已经在Packagist 上发布了一个初始工作版本(我很快就会更新自述文件和版本号)。
    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多