PHPUnit / DBUnit Timestamp Column

I am trying to use PHPUnit / DBUnit to test Zend Framework DbTable models. I worked with timestamped tables.

I am trying to use assertDataSetsEqual to compare the actual results of an insert with the expected results. Of course, when I insert a record into a table with a timestamp, the timestamp field is populated with the current date / time. The dataset representing the expected data is static, so the timestamp will not match.

How can I handle this situation? Can I make a statement ignore the timestamp column?

+4
source share
3 answers

I found a much better solution for this thanks to this blog post .

I created a function that makes it easy to claim that my two datasets are equal.

protected function assertDataSetEquals( $ExpectedXMLFileName, array $options = array()) { $model = (isset($options['model'])) ? $options['model'] : $this->_object; $excludeColumns = (isset($options['excludeColumns'])) ? $options['excludeColumns'] : null; $ds = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet(); $ds->addTable($model); $dsFiltered = (isset($excludeColumns)) ? $this->filterColumns($model, $ds, $excludeColumns) : $ds; $this->assertDataSetsEqual( $this->createFlatXmlDataSet( APPLICATION_PATH . '/../tests/fixtures/models/' . $ExpectedXMLFileName . '.xml' ), $dsFiltered ); } 

And a private function to return a filtered dataset.

 private function filterColumns( $model, $dataSet, $excludeColumns) { $dsFiltered = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet); $dsFiltered->setExcludeColumnsForTable( $model->getName(), $excludeColumns ); return $dsFiltered; } 

Now, to compare two datasets, excluding any columns, I just do it.

  $this->assertDataSetEquals( 'TableWithTimestampIWantToIgnoreUpdate', array('excludeColumns'=>array('timestamp','id')) ); 

This simplifies and simplifies the testing of models matching tables with a timestamp column.

+2
source

I did not find anything that would make me think that this is possible.

The way I ultimately decided was querying the table after the update, storing the results in an array, and then using assertEquals to ensure the fields are filled correctly.

0
source

Thanks to bconrad, I added this function to my test class:

 /** * @param Zend_Test_PHPUnit_Db_DataSet_QueryDataSet $dataSet * @param array $excludeTheseColumnsOfTheseTables Array where the keys are table names and the values are arrays of column names to exclude. * @return PHPUnit_Extensions_Database_DataSet_DataSetFilter * @see http://stackoverflow.com/a/11636295/470749 */ protected function filterColumns($dataSet, $excludeTheseColumnsOfTheseTables) { $filteredDataset = new \PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet); foreach ($excludeTheseColumnsOfTheseTables as $tableName => $excludedColumns) { $filteredDataset->setExcludeColumnsForTable($tableName, $excludedColumns); } return $filteredDataset; } 

Then filtering the dataset is just as simple:

 $queryDataSet = new \Zend_Test_PHPUnit_Db_DataSet_QueryDataSet($this->getConnection()); $queryDataSet->addTable('posts', 'SELECT * FROM posts'); $queryDataSet->addTable('users', 'SELECT * FROM users'); $excludeTheseColumnsOfTheseTables = array( 'users' => array('created', 'modified'), 'posts' => array('modified')); $filteredDataset = $this->filterColumns($queryDataSet, $excludeTheseColumnsOfTheseTables); $this->assertDataSetsEqual($this->getAssertionXmlDataSet('asrt.xml'), $filteredDataset); 
0
source

All Articles