如何测试对类包装器的父调用

时间:2017-03-03 14:19:42

标签: php unit-testing phpunit

我有以下课程,我想介绍一些测试:

namespace Nietonfir\RaygunBundle\Services;

use Raygun4php\RaygunClient;

class Client extends RaygunClient implements NietonfirRaygunClient
{
    private $defaultTags = null;

    protected function mergeTags($tags)
    {
        if (is_array($this->defaultTags)) {
            if (is_array($tags)) {
                $tags = array_merge($this->defaultTags, $tags);
            } else {
                $tags = $this->defaultTags;
            }
        }

        return $tags;
    }

    public function setDefaultTags(array $tags)
    {
        if (0 == count($tags)) {
            $this->defaultTags = null;
        }

        $this->defaultTags = $tags;
    }

    public function setVersion($version)
    {
        parent::SetVersion($version);
    }

    public function sendException($exception, $tags = null, $userCustomData = null, $timestamp = null)
    {
        $tags = $this->mergeTags($tags);
        parent::SendException($exception, $tags, $userCustomData, $timestamp);
    }

    public function sendError($errno, $errstr, $errfile, $errline, $tags = null, $userCustomData = null, $timestamp = null)
    {
        $tags = $this->mergeTags($tags);
        return parent::SendError($errno, $errstr, $errfile, $errline, $tags, $userCustomData, $timestamp);
    }
}

具体而言,我想使用正确的sendException参数测试parent::SendException次调用$tags。问题当然是以某种方式嘲弄父母,但我认为这几乎是不可能的。我正在考虑重构类,以便通过属性使Client指向RaygunClient的实例。这样,它可以测试,但我不确定这是一个很好的方式来做到这一点。毕竟,我想要的是用规范化的参数稍微改变原始的父类调用。

任何建议都非常受欢迎。

1 个答案:

答案 0 :(得分:0)

我将此作为评论,但它可以作为答案和可能的解决方案。

您在评论中澄清了您希望在测试用例中调用的方法将发送cURL请求。您希望避免实际在测试期间发送该请求,因为您测试的不是PHP的cURL库。如果您的cURL调用在该方法中,您应该将其提取到另一个类。

然后,您将能够在测试中模拟 类,将其注入您尝试测试的实际系统,并断言该方法调用发送cURL请求时,请求中的参数与测试用例中的参数相匹配。对您的测试 这些参数如何显示它们的方式并不重要,但我假设它们在某些可检测的('可断言的')中会有所不同。通过你的父母班级的方式'方法

然后会检测到破坏此功能的系统(通过向cURL请求提供错误的参数)的更改。