Search code examples
unit-testingphpunitfixturesdbunitdatabase-testing

Create XML dataset with the same table name as initial data set in DBUnit?


I'm trying to create an initial DB state in DB Unit like this...

public function getDataSet() {
        $primary = new \PHPUnit\DbUnit\DataSet\CompositeDataSet();
        $fixturePaths = [
            "test/Seeds/Upc/DB/UpcSelect.xml",
            "test/Seeds/Generic/DB/ProductUpcSelect.xml"
        ];
        foreach($fixturePaths as $fixturePath) {
            $dataSet = $this->createXmlDataSet($fixturePath);
            $primary->addDataSet($dataSet);
        }
        return $primary;
    }

Then after my query I'm attempting to call this user-defined function...

protected function compareDatabase(String $seedPath, String $table) {
    $expected = $this->createFlatXmlDataSet($seedPath)->getTable($table);
    $result = $this->getConnection()->createQueryTable($table, "SELECT * FROM $table");
    $this->assertTablesEqual($expected, $result);
  }

The idea here is that I have an initial DB state, run my query, then compare the actual table state with the XML data set representing what I expect the table to look like. This process is described in PHPUnit's documentation for DBUnit but I keep having an exception thrown...

PHPUnit\DbUnit\InvalidArgumentException: There is already a table named upc with different table definition

Test example...

public function testDeleteByUpc() {
    $mapper = new UpcMapper($this->getPdo());
    $mapper->deleteByUpc("someUpcCode1");
    $this->compareDatabase("test/Seeds/Upc/DB/UpcAfterDelete.xml", 'upc');
  }

I seem to be following the docs...how is this supposed to be done?


Solution

  • This was actually unrelated to creating a second XML Dataset. This exception was thrown because the two fixtures I loaded in my getDataSet() method both had table definitions for upc.