Home » Php » php – With PHPUnit Class 'mysqli' is not found

php – With PHPUnit Class 'mysqli' is not found

Posted by: admin July 12, 2020 Leave a comment


I just started with PHPUnit. Some simple tests I wrote are working. So in general PHPUnit is up and running. There’s something wrong with the MySQLi class though.

In my code it works fine. Here’s the line:

$this->mysqli = new \mysqli($this->host, $user->getUser(), $user->getPwd(), $this->db);

When this line is parsed running phpunit I get the following error message (pointing to that line):

PHP Fatal error:  Class 'mysqli' not found in /opt/lampp/htdocs/...

Two possibilities (as I see it):

1) I’m missing some feature / extension / configuration step / something else related to a correct setup of PHPUnit working with the MySQLi extension.


If I do test for the extension extension_loaded('mysqli') it returns true in my normal code. If I do the following within the Test it skips the test (i.e. it returns false):

if (!extension_loaded('mysqli')) {
        'The MySQLi extension is not available.'


2) There might be a problem with my code. I’m trying to mock a User object in order to make a test connection. So here it is:

class ConnectionTest extends \PHPUnit_Framework_TestCase
    private $connection;

    protected function setUp()
        $user = $this->getMockBuilder('mysqli\User')
                     ->setMethods(array('getUser', 'getPwd'))
             ->will($this->returnValue('[email protected]'));

        $this->connection = new \mysqli\Connection($user);

    public function testInternalTypeGetMysqli()
        $actual   = $this->connection->getMysqli();
        $expected = 'resource';

        $this->assertInternalType($expected, $actual);

    protected function tearDown()

The tested Connection class looks like this:

namespace mysqli;

class Connection
    protected $mysqli;
    protected $host = 'localhost';
    protected $db   = 'database';

    public function __construct(\mysqli\User $user)
        $this->mysqli = new \mysqli($this->host, 
        if (mysqli_connect_errno()) {
            throw new \RuntimeException(mysqli_connect_error());

    public function getMysqli()
        return $this->mysqli;


This whole thing is / was an installation problem. I’m using a XAMPP installation which provides my PHP. Installing PHPUnit independently makes it use a different setup! So in my browser (XAMPP powered) all was fine, but in my command line the MySQLi extension has been missing all together! Debian provides a package called php5-mysqlnd. Having this installed all works fine! (except other errors appearing 🙂

How to&Answers:

PHPUnit normally runs within the CLI – command line interface.

The PHP you’ve got there is different than with the webserver. Different binary and often a different configuration as well.

$ php -r "new mysqli();"

Should give you the same error. Verify where the binary and configuration is located:

$ which php


$ php -i | grep ini

Ensure you’ve got the extension for the mysqli class installed and enabled. Once configured you should be able to run your unit-tests flawlessly.