ZF2/PHPUnit:模拟Zend/Db/Adapter/Adapter以便进一步使用 [英] ZF2 / PHPUnit: Mock a Zend/Db/Adapter/Adapter for further usage

查看:86
本文介绍了ZF2/PHPUnit:模拟Zend/Db/Adapter/Adapter以便进一步使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:在Zend Framework 2中对InputFilter进行单元测试.

The goal: Unit test an InputFilter in Zend Framework 2.

问题:需要模拟的DbAdapter.

The problem: A mocked DbAdapter is needed.

由于我对单元测试还比较陌生,所以我刚开始使用模拟类.经过大量研究后,我仍然无法找到解决问题的合适方法,因此我们在这里使用我的过滤器以开始工作:

As I am relatively new to Unit Testing I just got started with mocking classes. After doing a lot of research I'm still unable to find a proper solution for my problem so here we go with my filter to start things off:

class ExampleFilter extends Inputfilter
{
    protected $dbAdapter;

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

    public function init()
    {
        $this->add(
            [
                'name' => 'example_field',
                'required' => true,
                'filters' => [
                    ['name' => 'StringTrim'],
                    ['name' => 'StripTags'],
                ],
                'validators' => [
                    [
                        'name'    => 'Db\NoRecordExists',
                        'options' => [
                            'adapter' => $this->dbAdapter,
                            'table' => 'example_table',
                            'field' => 'example_field',
                        ],
                    ],
                ],
            ]
        );
    }
}

不需要适配器,测试此过滤器将非常容易.我的问题是在TestClass中创建过滤器,如下所示:

Without the need of an adapter, testing this filter would be rather easy. My problem is to create the Filter in my TestClass as seen here:

class ExampleFilterTest extends \PHPUnit_Framework_TestCase
{
    protected $exampleFilter;
    protected $mockDbAdapter;

    public function setUp()
    {
        $this->mockDbAdapter = $this->getMockBuilder('Zend\Db\Adapter')
            ->disableOriginalConstructor()
            ->getMock();
        $this->exampleFilter = new ExampleFilter($this->mockDbAdapter);
    }

}

当像这样创建过滤器时,ExampleFilter类最终会说我确实为其构造函数提供了错误的类.当期望类型为Zend \ Db \ Adapter \ Adapter时,它会收到一个模拟对象.

When creating the filter like this the ExampleFilter class will end up saying that I did provide a wrong class to its constructor. It's receiving a mock object when expecting one of type Zend\Db\Adapter\Adapter.

我当然可以创建一个真正的适配器,但是我想避免对数据库执行实际查询,因为它是一个单元测试,并且这将远远超出我要测试的单元的范围.

I could create a real adapter of course but I want to avoid performing actual queries to the database as it is a unit test and this would go far beyond the border of my unit to test.

有人可以告诉我如何通过模拟的DbAdapter达到测试过滤器的目标吗?

Can anyone tell me how I can achieve my goal of testing the filter with a mocked DbAdapter?

推荐答案

好吧...当我在gontrollez上发表评论时,我已经闻到了我的错误.我必须创建一个'Zend/Db/Adapter/AdapterInterface'的模拟,而不仅仅是'/Zend/Db/Adapter'.

Well... As I was commenting on gontrollez hint I already smelled my mistake. I had to create a mock of 'Zend/Db/Adapter/AdapterInterface' instead of just '/Zend/Db/Adapter'.

无论如何都要感谢我让正确的道路gontrollez:)

Thanks for bringing me on the right path anyway gontrollez :)

这篇关于ZF2/PHPUnit:模拟Zend/Db/Adapter/Adapter以便进一步使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆