I have a test for testing data model:
<?php
namespace Tests\Model\SQL;
/**
* @group Model_Users
* @group Model
*/
class UsersTest extends TestCase {
/** @var Users */
private $model;
public function setUp() {
parent::setUp();
$this->connection->execute( file_get_contents(
__DIR__ . '/fixtures/countries.sql' ) );
$this->model = new \Users(
$this->connection
);
}
public function testUserExists() {
$exists = $this->model->userExists( 'johndoe' );
$this->assertTrue( $exists );
}
}
The Tests\Model\SQL\TestCase uses SQLite as a database to keep tests as fast as possible.
Anyway, as you may expect, SQLite is not the same database as Postgres, therefore there could be some specialites, which will fail on SQLite and pass on PostgreSQL and vice versa.
My question is:
How to effectively (when keeping DRY in mind) design the architecture to be able to run tests only with SQLite and then only with Postgres.
When I am saying DRY, I mean ideally case, when I write only one testcase for one model and the architecture of my tests will take care of that, so I will be able to run tests like this:
php tests/run.php --group=MockedDbWithSqlite # fast way, prefered, default
php tests/run.php --group=RealDb # slower way, but with more precise results
So judging from the comment the issue is rather independent of any concrete storage issue and more about how one can pass a custom parameter to PHPUnit.
Because, using this parameter, you’d switch out DB drivers.
There are a couple of options to do this but no officially supported "custom parameters" objects or something. Even so it would be nice if someone would add this maybe 🙂
My suggestion would be to use one of the following to options:
Env variables
Make the test/bootstrap code rely on
$_ENVvariables.and using one of the ways below to
or
or using phpunits xml config to set the env or whatever you like.
By doing so you could provide two xml configs
phpunit-sqlite.xml&phpunit-pg.xmland run:It doesn’t have to be env. Any of the methods listed in the doc section
Setting PHP INI settings, Constants and Global Variableswill work."Custom" cli parameters
Calling phpunit like this:
and adding something like this in test cases or bootstrap code: