0

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?

5
  • 1
    stackoverflow.com/a/9073217/1675789 Commented Aug 10, 2016 at 21:43
  • 1
    have a look here : stackoverflow.com/questions/9073144/… Commented Aug 10, 2016 at 21:45
  • Where this $dblink variable is initialized? In bootstrap.php file? Commented Aug 10, 2016 at 21:47
  • $dblink is initialized in bootstrap.php Commented Aug 10, 2016 at 21:49
  • gontrollez and Suni: those links seem to have worked for me, thanks Commented Aug 10, 2016 at 21:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.