用 phpunit 模拟 PDO [英] Mocking PDO with phpunit

查看:47
本文介绍了用 phpunit 模拟 PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟 PDO 对象以在使用 phpunit 编写一些测试时使用,但我发现它非常复杂并且找不到太多关于它的文档.我创建了这个 xml 结构:

I am trying to mock PDO object to use when writing some tests with phpunit, but I find it pretty complicated and can't find too much documentation about it. I created this xml structure:

<dataset>
    <table name="providers">
            <column>id</column>
            <column>name</column>
            <column>description</column>
            <row>
                    <value>1</value>
                    <value>provdier_1</value>
                    <value>phpunit first provider</value>
            </row>
    </table>
</dataset>

现在我想查询 providers 表并取回数据,但我不知道该怎么做.

and now I want to query providers table and get the data back but I just cant figure out how to do that.

我开始模拟 PDO 对象,但我不明白我应该如何使用它以及如何在 getConnection() 方法中使用它.我的第一次尝试,我猜它与正确的方法相去甚远,因为我在这里很迷茫,看起来像这样:

I started with mocking the PDO object but I don't understand how should I work with it and how to use it inside the getConnection() method. my first attempt, which I'm guessing its quite far from the correct way because I am very lost here, looks something like this:

class AdProvidersTest extends PHPUnit_Extensions_Database_TestCase
{
    public function getConnection()
    {
      $dsn = 'mydb';
      $user = '';
      $password = '';

      $pdo = $this->getMockBuilder('PDOMock')
        ->getMock();

        return $this->createDefaultDBConnection($pdo, 'adserverTesting');
    }

    public function getDataSet()
    {
        return $this->createXMLDataSet('adserverTesting.xml');
    }

}

如何使连接与 'adserverTesting.xml' 文件交互以及如何使用以下行查询它:

how can I make the connection interact with the 'adserverTesting.xml' file and how can I query it using this lines:

$ds = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
$ds->addTable('adserverTesting', 'SELECT * FROM providers');

推荐答案

您不必模拟 PDO.这是它如何工作的示例:

You don't have to mock PDO. Here example how it works:

ConnectionTest.php:

ConnectionTest.php:

<?php

class ConnectionTest extends PHPUnit_Extensions_Database_TestCase
{
    public function getConnection()
    {
        $database = 'myguestbook';
        $user = 'root';
        $password = '';
        $pdo = new PDO('mysql:host=localhost;dbname=myguestbook', $user, $password);
        $pdo->exec('CREATE TABLE IF NOT EXISTS guestbook (id int, content text, user text, created text)');
        return $this->createDefaultDBConnection($pdo, $database);
    }

    public function getDataSet()
    {
        return $this->createFlatXMLDataSet(__DIR__.'/dataSets/myFlatXmlFixture.xml');
    }

    public function testGetRowCount()
    {
        $this->assertEquals(2, $this->getConnection()->getRowCount('guestbook'));
    }
}

myFlatXmlFixture.xml

myFlatXmlFixture.xml

<?xml version="1.0" ?>
<dataset>
    <guestbook id="1" content="Hello buddy!" user="joe" created="2010-04-24 17:15:23" />
    <guestbook id="2" content="I like it!" user="nancy" created="2010-04-26 12:14:20" />
</dataset>

结果:

PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

.

Time: 215 ms, Memory: 5.25Mb

OK (1 test, 1 assertion)

针对 db 的测试的要点是不要模拟 db,还要创建完全相同的 PDO 连接,而不是到生产 db 而是到 db 进行测试,它可以是 mysql、sqlite 等...

Main point in tests against db is don't mock db but also create absolutely same PDO connection not to production db but to db for test, it can be mysql, sqlite etc...

这篇关于用 phpunit 模拟 PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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