unit-testing - Zend 框架 : How should I unit test my Mapper in Zend_Db?

标签 unit-testing zend-framework zend-db

如何在 Zend_Db 中测试我的映射器? 我的每个模型都有 3 个类:

  • 模型
  • 映射器
  • 数据库表

这是我的单元测试:

<?php
// Call Model_BugTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "Model_ArtistTest::main");
}

require_once dirname(__FILE__) . '/../../TestHelper.php';

/** Model_Artist */
require_once 'Artist.php';


/**
 * Test class for Model_Artist.
 *
 * @group Models
 */
class Model_ArtistTest extends PHPUnit_Framework_TestCase 
{
    /**
     * Runs the test methods of this class.
     *
     * @return void
     */
    public static function main()
    {
        $suite  = new PHPUnit_Framework_TestSuite("Model_ArtistTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @return void
     */
    public function setUp()
    {
        $this->model = new Ly_Model_Artist();
    }

    /**
     * Tears down the fixture, for example, close a network connection.
     * This method is called after a test is executed.
     *
     * @return void
     */
    public function tearDown()
    {
    }

    public function testCanDoTest()
    {
        $this->assertTrue(true);
    }

    public function testCanFindArtist()
    {
        $artist = "Rage Against the Machine";
        $result = $this->model->findbyalpha($artist);
        var_dump($result);
    }
}

我正在使用 Matthew 的 TestHelper:http://github.com/weierophinney/bugapp/blob/master/tests/TestHelper.php

我得到的错误是这样的:

c:\xampp\htdocs\ly\tests>phpunit --configuration phpunit.xml
PHPUnit 3.4.10 by Sebastian Bergmann.

.
Fatal error: Class 'Ly_Model_ArtistMapper' not found in C:\xampp\htdocs\ly\appli
cation\models\Artist.php on line 72

似乎没有读取映射器。谁能告诉我如何进行此类测试?我是单元测试的新手,我才刚刚开始学习它。

这是我的艺术家模型

<?php
/**
 * Artist Model
 */
class Ly_Model_Artist
{
    protected $_id; //a field
    protected $_name; //a field

protected $_mapper;

    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value)
    {
        $pieces = explode('_', $name);
        foreach($pieces AS $key => $row) {
            $pieces[$key] = ucfirst($row);
        }

        $name = implode('',$pieces);
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid group property');
        }

        $this->$method($value);
    }

    public function __get($name)
    {
        $pieces = explode('_', $name);
        foreach($pieces AS $key => $row) {
            $pieces[$key] = ucfirst($row);
        }
        $name = implode('',$pieces);
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid group property');
        }

        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }


    public function setMapper($mapper)
    {
        $this->_mapper = $mapper;
        return $this;
    }

    public function getMapper()
    {
        if (null === $this->_mapper) {
            $this->setMapper(new Ly_Model_ArtistMapper());
        }
        return $this->_mapper;
    }

    public function setId($id)
    {
        $this->_id = (int) $id;
        return $this;
    }

    public function getId()
    {
        return $this->_id;
    }

    public function setName($text)
    {
        $this->_name = (string) $text;
        return $this;
    }

    public function getName()
    {
        return $this->_name;
    }

    public function find($id)
    {
        $this->getMapper()->find($id, $this);
        return $this;
    }

    public function findbyalpha($keyword)
    {
        return $this->getMapper()->findbyalpha($keyword);
    }
}

这是艺术家映射器:

<?php
/**
 * Artist Model Mapper
 */
class Ly_Model_ArtistMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Ly_Model_DbTable_Artist');
        }
        return $this->_dbTable->getAdapter();
    }


    public function find($id, Ly_Model_Artist $artist)
    {
        if(!isset($id) OR empty($id)) {
            throw new Exception ('Could not find id.');
        }

        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $artist->setId($row->id)
               ->setName($row->name);
    }

    public function findbyalpha($keyword)
    {
        if(!isset($keyword) OR empty($keyword)) {
            throw new Exception ('Could not find keyword.');
        }

        $keyword = $this->getDbTable()->quote($keyword.'%');

        //$sql = $this->getDbTable()->select()->where('twitter_id = ?',$twitter_id)->order('weight');
        $sql = "SELECT
                DISTINCT
                a.name
                FROM artist a
                WHERE a.name LIKE ".$keyword."
                ORDER BY a.name ASC
                ";

        //Zend_Debug::dump($sql);
        $result =  $this->getDbTable()->fetchAll($sql);
        return $result;
    }
}

Db_Table 看起来像这样:

<?php
class Ly_Model_DbTable_Artist extends Zend_Db_Table_Abstract
{
    protected $_name = 'artist';
}

最佳答案

更改映射器以使用控制反转模式(将数据库表对象插入构造函数(至少可选),这样您就可以断开连接并传入模拟/ stub 对象。

关于unit-testing - Zend 框架 : How should I unit test my Mapper in Zend_Db?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2257225/

相关文章:

php - 如何将 javascript 添加到 Zend Framework 中的表单?

database - 使用 Zend 和 doctrine 在数据库中查询现有用户名

php - Zend 数据库适配器 - 复杂的 MySQL 查询

php - 使用zend的Mysql删除查询

java - 单元测试有序 SQL 查询

java - 单元测试应该使用日志记录吗?

c++ - 使用 gmock 返回模拟方法参数

ios - 使用 OCUnit 对 keyWindow 进行单元测试会抛出错误

php - 从 Zend_Form_Element_File 移除格式

php - 在 zf2 mvc 之外使用 Zend_Db zf2 模块