sebastianbergmann/dbunit — Issue #47: DbUnit 1.1.0 dataset assertions don't work with PHPUnit 3.6.0
Author: lynx-ripe
Created: 2011-11-03 17:37:05+00:00
State: open

After upgrading to the recent versions of software on pear channel pear.phpunit.de (PHPUnit 3.6.0 and DbUnit 1.1.0) I've noticed that PHPUnit_Extensions_Database_Constraint_DataSetIsEqual and PHPUnit_Extensions_Database_Constraint_TableIsEqual don't work anymore (they work with PHPUnit 3.5.15 and DbUnit 1.0.3). A quick debug session shows that PHPUnit_Extensions_Database_Constraint_DataSetIsEqual::evaluate() detects the difference but just returns FALSE. Looking at core PHPUnit constraints (e.g. PHPUnit_Framework_Constraint::evaluate()) reveals that it isn't enough for proper failure report in 3.6.

Below is the simple test that asserts 2 failures using PHPUnit 3.5.15 + DbUnit 1.0.3 but doesn't assert using PHPUnit 3.6.0 + DbUnit 1.1.0:

ds1 = new MyApp_DbUnit_ArrayDataSet(array('t1' => array(
array('c1' => '1', 'c2' => 'x')
)));
$this->ds2 = new MyApp_DbUnit_ArrayDataSet(array('t1' => array(
array('c1' => NULL, 'c2' => 'y')
)));
}

public function testDataSetIsEqual()
{
$this->assertThat($this->ds2,
new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($this->ds1));
}

public function testTableIsEqual()
{
$this->assertThat($this->ds2->getTable('t1'),
new PHPUnit_Extensions_Database_Constraint_TableIsEqual(
$this->ds1->getTable('t1')));
}

}

class MyApp_DbUnit_ArrayDataSet extends PHPUnit_Extensions_Database_DataSet_AbstractDataSet
{

/**
* @var array
*/
protected $tables = array();

/**
* @param array $data
*/
public function __construct(array $data)
{
foreach ($data AS $tableName => $rows) {
$columns = array();
if (isset($rows[0])) {
$columns = array_keys($rows[0]);
}

$metaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData($tableName, $columns);
$table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($metaData);

foreach ($rows AS $row) {
$table->addRow($row);
}
$this->tables[$tableName] = $table;
}
}

protected function createIterator($reverse = FALSE)
{
return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($this->tables, $reverse);
}

public function getTable($tableName)
{
if (!isset($this->tables[$tableName])) {
throw new InvalidArgumentException("$tableName is not a table in the current database.");
}

return $this->tables[$tableName];
}

}