I’m trying to build a test case that uses the database extension of PHPUnit. I’ve followed the official documentation and this post of Mike Lively, but PHPUnit keeps complaining:
1) ClearanceProfileTest::testFetchPrivileges
PHPUnit_Extensions_Database_Operation_Exception: COMPOSITE[TRUNCATE] operation failed on query:
DELETE FROM "profile_privilege_mappings"
using args: Array
(
)
[SQLSTATE[HY000]: General error: 1 no such table: profile_privilege_mappings]
I believe that the error is caused because my database schema wasn’t set, but I can’t find a way to load it. I tried loading my .sql file contents, and executing it as PDO query, but it just did nothing.
Here is my testcase code:
class ClearanceProfileTest extends PHPUnit_Extensions_Database_TestCase
{
/**
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection() {
$pdo = new PDO('sqlite::memory:');
return $this->createDefaultDBConnection($pdo, ':memory:');
}
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
protected function getDataSet() {
$yaml = dirname(__FILE__) . "/ClearanceProfileTestDataset.yml";
$dataset = new PHPUnit_Extensions_Database_DataSet_YamlDataSet($yaml);
return $dataset;
}
public function testFetchPrivileges() {
$cp = ClearanceProfile::retrieve_one('id = ?', 1);
$privileges = $cp->fetchPrivileges();
$this->assertEquals(count($privileges), 1);
}
}
In a default implementation the database extension will perform a ‘clean insert’ operation on your database using the the dataset returned by getDataSet. The clean insert operation will truncate any tables in the data set (not all tables in the database) and then insert the rows in the dataset. The solution is to create the table before running the test.
Additionally you can modify the default functionality by overriding the
getSetUpOperation()andgetTearDownOperation()methods