2010-02-13 39 views
2

我該如何在Zend_Db中測試我的映射器? 我的每一個模型都將有3類:Zend Framework:我應該如何在Zend_Db中測試我的Mapper?

  • 示範
  • 的映射
  • 的DBTABLE

這裏是我的單元測試:

<?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 

好像映射器沒有被讀取。任何人都可以告訴我如何做這種測試?我是UnitTesting的新手,我剛開始學習它。

這是我的藝人型號

<?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'; 
} 

回答

1

您應該測試模型(其中稱你的映射器)。這樣,你就會知道你的映射器是否有問題。

你有沒有在你的藝術家模型嘗試設置像

公共職能setMapper(Ly_Model_ArtistMapper $映射器) { ... }

+3

這不是使集成測試代替單元測試嗎? OP詢問的單元測試應該單獨測試每個類,而不是測試依賴關係鏈。我認爲@ mfacenet的答案是正確的想法。 – curtisdf

4

變化映射器的映射器的方法來使用控制模式的反轉(插入數據庫表對象到構造函數中(至少有選擇地),這樣你就可以斷開它並傳入一個模擬/存根對象。

0

單元測試是爲了測試少量的工作,即方法。該方法取決於on應該被嘲笑/ stubbed(成功/失敗),因爲這將在另一個單元測試中進行測試。

相關問題