PHPUnit also gives some help on this, see Database Testing .
Essentially, you can write test classes to extend PHPUnit_Extensions_Database_TestCase , and then use the getConnection() and getDataSet() functions to load data for the test.
require_once 'PHPUnit/Extensions/Database/TestCase.php'; class PersonTest extends PHPUnit_Extensions_Database_TestCase { protected function getConnection() { $pdo = new PDO('mysql:host=localhost;dbname=application_test', 'root', ''); return $this->createDefaultDBConnection($pdo, 'application_test'); } protected function getDataSet() { return $this->createMySQLXMLDataSet('person.xml'); }
Then you can pinpoint what you want to test in the database in XML.
You can also claim that the resulting DataSet from your tests is equal to what you expect:
public function testCreate() { // Execute some code with your ORM to create a person. $actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection()); $actual->addTable('person'); $expected = $this->createMySQLXMLDataSet('person_create_expected.xml'); $this->assertDataSetsEqual($expected, $actual); }
In this example, we only compare the resulting person table ... Thus, person_create_expected.xml should contain only the person table.
To create XML, you can use mysqldump .
mysqldump --xml -t -u root -p application_test > person.xml
Jacob
source share