8

I am using PHPUnit to test insertion of objects via my storage object. Each domain object has a added and lastmodified timestamp, that is handled by the storage object automatically. I can using PHPUnits DB extensions method assertDataSetsEqual and passing as XML data set as below shows. The problem is added and lastmodified cannot be hardcoded into the XML dataset as this will change all the time automatically, can I tell PHPUnit to ignore these cols? or compare the tables output another way (not XML) where I can ignore these columns?

Test

$user = new Social_User();
$user->setFk_mswuserId(10);
$user->setFirstName('Gavin');

$store = new Storage();
$store->save($user);

$xml_dataset = $this->createFlatXMLDataSet('after-new.xml');
$this->assertDataSetsEqual($xml_dataset, $this->getConnection()->createDataSet());

XML Dataset

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
            <user id="1" password="NULL" ip="0" added="0" authenticated="0" lat="0" lon="0" avatar="0" fk_mswuserId="1" timezoneoffset="0" firstName="Ben" lastName="Freeston" deleted="0" lastModified="0" />
            <user id="2" password="NULL" ip="0" added="0" authenticated="0" lat="0" lon="0" avatar="0" fk_mswuserId="10" timezoneoffset="0" firstName="Gavin" lastName="Cooper" deleted="0" lastModified="0"/>
</dataset>

1 Answer 1

9

According to

this is already built-in.

Also see these slides by M.Lively (the main DBUnit author)

and B. Eberlei's Ultimate Guide to DB Testing with PHPUnit

So it should work along the lines of

$database_dataset = new PHPUnit_Extensions_Database_DataSet_DataSetFilter ( 
    $this->getConnection()->createDataSet(array('bank_account')), 
    array('bank_account' => array ('date_created')) // excluded
); 
Sign up to request clarification or add additional context in comments.

2 Comments

@Gcoop that was much more a case of knowing-where-to-look than anything else. I had no clue the filter exists until you asked. So thanks for making me search. I learned something new, too ;)
Same thing exists for table filtering: new PHPUnit_Extensions_Database_DataSet_TableFilter($table, $excludeColumns).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.