2012-03-01 146 views
1

我想上手使用MySQL數據庫建立單元測試,我遇到了此異常:PHPUnit的數據庫測試異常

DBTest::test__getException() 

Argument 1 passed to PHPUnit_Extensions_Database_DataSet_DefaultTableIterator::__construct() must be an array, null given. 

我不知道我可能會錯過

我的單元測試代碼:

<?php 
class DBTest extends Generic_Tests_DatabaseTestCase { 
    //... 
    public function getDataSet() { 
     $dataSet = $this->createMySQLXMLDataSet(dirname(__FILE__)."/../db/t_enroll_fixtures.xml"); 
     return $dataSet; 
    } 

    public function setUp() { 
     $this->X = $this->getMock('\X\Engine\X'); 
     $this->model = new Model($this->X, 't_users'); 
     $this->className = get_class($this->model); 
     parent::setUp(); 
    } 

    public function tearDown() { 
     $this->X = NULL; 
     $this->model = NULL; 
     parent::tearDown(); 
    } 

    public function testMagicFields() { 
     $this->getConnection()->addTable('t_enroll'); 
     $this->assertEquals(10, $this->getConnection()->getRowCount('t_enroll')); 
    } 
} 
?> 

的Generic_Test_DatabaseTestCase類:

<?php 
require_once "PHPUnit/Extensions/Database/TestCase.php"; 
abstract class Generic_Tests_DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase { 

    // only instantiate pdo once for test clean-up/fixture load 
    static private $pdo = null; 
    // only instantiate PHPUnit_Extensions_Database_DB_IDatabaseConnection once per test 
    private $conn = null; 

    final public function getConnection() { 
     if($this->conn === null) { 
      if(self::$pdo == null) { 
       self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']); 
      } 
      $this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']); 
     } 

     return $this->conn; 
    } 

} 
?> 

我錯過了什麼?

+0

相關問題:http://stackoverflow.com/questions/4640802/php-testing-models-with-zend-test-phpunit-databasetestcase – 2012-03-01 15:09:42

+0

@cillosis這個問題是關於我收到的錯誤消息的專利,但OP的解決方案出現t o非常適合Zend。你能否更多地瞭解我可以如何使用這個問題來幫助我? – 2012-03-01 15:20:38

+0

@LeviHackwith我有同樣的問題,並沒有使用Zend。我已經在我的xml文檔中有數據庫名稱。你有沒有想出一個不同的解決方案,或爲你解決它?謝謝。 – GreeKatrina 2015-03-24 16:16:21

回答

2

我有同樣的問題。原因是,XML fixture是由MySQLDump生成的,有人刪除了<database name="xyz">節點。 事實證明這$this->tables PHPUnit中爲NULL,而不是陣列

0

這發生在我身上後,我增加了一個架構位置的mysqldump的像

<mysqldump xmlns="mysqldump" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="mysqldump mysqldump.xsd "> 

之後我刪除了命名空間,它的工作:

<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">