I have a global PHP variable $dblink that is initialized and kept alive for the duration the web application run. However, when I run PHPUnit I get this error
mysqli_query() expects parameter 1 to be mysqli, null given Looking into it I discovered that $dblink (database connection object) is NULL. Tuns out it loses value somewhere between first and second test inside the same class.
Here is my unit test class:
class LpTest extends Tests { public function __construct() { parent::__construct(); global $dblink; var_dump($dblink);//EXISTS and is not NULL } function firstTest() { global $dblink; var_dump($dblink);//EXISTS and is not NULL } function secondTest() { global $dblink; var_dump($dblink); //is NULL <------------ } } How I run it:
phpunit --bootstrap tests/bootstrap.php test/LpTest.php I believe this is a peculiarity of PHPUnit. If I remove firstTest above, the $dblink inside secondTest becomes not NULL and is a valid database object. Presumably because it is the first test to run and it is initialized differently.
What can this be? Why does PHPUnit lose global variable value between runs? How can I fix this to where it doesn't?
$dblinkis initialized inbootstrap.php