Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit Database Testing

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>
like image 854
Gcoop Avatar asked Nov 11 '10 10:11

Gcoop


People also ask

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.

How do I run a single PHPUnit test?

You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status. The output shows that we ran 1 test, and made 3 assertions in it.

What is DbUnit?

DbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that, among other things, puts your database into a known state between test runs.

What is refresh database in laravel?

Published 26 June, 2021. Laravel provides the Illuminate\Foundation\Testing\RefreshDatabase trait to reset the database after each test so that data from a previous test does not interfere with subsequent tests.


1 Answers

According to

  • http://www.phpunit.de/ticket/492

this is already built-in.

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

  • http://www.slideshare.net/mjlivelyjr/advanced-phpunit-testing

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

  • http://www.phpunit.de/manual/dbunit.txt

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
); 
like image 156
Gordon Avatar answered Oct 26 '22 15:10

Gordon