1

I'm trying to get started with setting up unit tests using a MySQL database and I'm running into this exception:

DBTest::test__getException()

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

I don't know what I could be missing

My Unit Test Code:

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

The Generic_Test_DatabaseTestCase Class:

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

}
?>

What am I missing?

Levi Hackwith
  • 9,232
  • 18
  • 64
  • 115
  • Related Question: http://stackoverflow.com/questions/4640802/php-testing-models-with-zend-test-phpunit-databasetestcase – Jeremy Harris Mar 01 '12 at 15:09
  • @cillosis This question is spot-on regarding the error message I'm receiving, but the solution for the OP appears to be very Zend-specific. Could you perhaps shed some more light on how I could use this question to help me? – Levi Hackwith Mar 01 '12 at 15:20
  • @LeviHackwith I am having the same issue, and not using Zend. I already have the database name in my xml document. Did you come up with a different solution, or did that fix it for you? Thanks. – Katrina Mar 24 '15 at 16:16

2 Answers2

2

I had the same problem. The reason was, that the XML fixture was generated by MySQLDump and someone removed the <database name="xyz"> node. This turned $this->tables in PHPUnit into NULL instead Array

nico gawenda
  • 3,648
  • 3
  • 25
  • 38
0

This happened to me after I added a schema location for the mysqldump like

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

After I removed the namespace, it worked:

<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Arany
  • 1,238
  • 11
  • 11