php - Redirect database connection on PHPUnit test -
i trying test php web phpunit, , need test db connection (like insert, update , delete).
have 2 databases, 1 prod , 1 tests. right i'm replacing database url manually before , after every test.
is there way auto-replace urls when code run on test mode?
in phpunit.xml, set bootstrap
bootstrap.php
:
<phpunit bootstrap="bootstrap.php" >
then, in bootstrap.php
:
$globals['db'] = new c_database( 'testhost', // host 'testuser', // username 'testpass', // password 'testdb' // database );
you able access database connection $db
app. in app, can like:
if(!isset($db)){ $db = new c_database( 'livehost', // host 'liveuser', // username 'livepass', // password 'livedb' // database ); }
when running tests, sure specify location of phpunit.xml
. can makefile (assuming phpunit located @ /path/to/app/vendor/bin/phpunit
, phpunit.xml
located @ /path/to/app/tests/phpunit.xml
):
cwd:=$(pwd) test: vendor/bin/phpunit --configuration $(cwd)/tests/phpunit.xml .phony test
now, every time run tests (eg, make test
), correct credentials used.
Comments
Post a Comment