Skip to content
Advertisement

Global pdo connection not reachable in a PHPUnit Test

I am start using PHPUnit for my projects and I need to test that some data is been stored successfully in the database.

My problem is that the PDO connection, which is created in a separate file with the name $connpdo, is not reachable. I really don’t get why.

This is my code:

JavaScript

The $connpdo variable is created in the file database_inc.php

JavaScript

I have also tried without global but I don’t know what happens. Maybe the use of the namespace?:

JavaScript

I thought that maybe pdo was not enabled but I have checked with php -m and I see those modules:

JavaScript

Advertisement

Answer

Ok, after many months I understood that how PHPunit works. It won’t read that include_once for the database if it’s outside of the method. I moved it inside of the function and also I changed the phpUnit environment for a remote one, on the same server of the database, so the connection is allowed.

After those two changes my test is working: https://www.screencast.com/t/O2ccmcxt2

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement