将表添加到pdo模拟对象

时间:2015-08-12 12:58:56

标签: php unit-testing pdo

我一直在关注创建pdo模拟对象的教程。我使用var_dump来理解这个pdo模拟包含什么,我发现结构非常混乱。我想要做的是查询一个名为'providers'的表,它应该包含dataProvider方法中的数据。 这是pdo创作:

class AdProvidersTest extends PHPUnit_Framework_TestCase
{
    public function dataProvider()
    {
        return array (
            array (1, '1st', 'desc_1', 11),
            array (2, '2nd', 'desc_2', 22),
            array (3, '3rd', 'desc_3', 33),
        );
    }
    /**
     * @dataProvider dataProvider
     */
    public function testAdProviders($id, $name, $desc, $account_id)
    {
        $data = array (
            array (
                'id' => $id,
                'name' => $name,
                'description' => $desc,
                'account_id' => $account_id
            )
        );

        $stmt = $this->getMock('PDOStatement', array ('fetchAll'));
        $stmt->expects($this->any())
             ->method('fetchAll')
             ->will($this->returnValue($data));

        $pdo = $this->getMock('PDO', array('prepare'),
            array('sqlite:dbname=:memory'),'PDOMock_' . uniqid(),true);
        $pdo->expects($this->any())
            ->method('prepare')
            ->will($this->returnValue($stmt));


        var_dump($pdo);
    }

}

如何在模拟对象中创建一个名为“providers”的表,所以当我像这样查询SELECT * FROM providers时,我会从dataProvider获取这三行?

我真正想要测试的功能:

  function getProviders() {
    if ($this->providers == null){
      $DBH = $this->getDbh();
      $query = "select * from providers";
      $STH = $DBH->query($query);
      $STH->setFetchMode(PDO::FETCH_ASSOC);
      while($provider = $STH->fetch()) {
        $this->providers[$provider['id']] = $provider;
      }
    }
    return $this->providers;
  }

0 个答案:

没有答案