With PHPUnit Class 'mysqli' is not found
mysqli, php, phpunit
Solution
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
And
$ 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.
Problem
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. EDIT 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')) { $this->markTestSkipped( 'The MySQLi extension is not available.' ); } ``` /EDIT 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: ``` <?php class ConnectionTest extends \PHPUnit_Framework_TestCase { private $connection; protected function setUp() { $user = $this->getMockBuilder('mysqli\User') ->setMethods(array('getUser', 'getPwd')) ->getMock(); $user->expects($this->once()) ->method('getUser') ->will($this->returnValue('username')); $user->expects($this->once()) ->method('getPwd') ->will($this->returnValue('p@ssw0rd')); $this->connection = new \mysqli\Connection($user); } public function testInternalTypeGetMysqli() { $actual = $this->connection->getMysqli(); $expected = 'resource'; $this->assertInternalType($expected, $actual); } protected function tearDown() { unset($this->connection); } } ``` The tested Connection class looks like this: ``` <?php namespace mysqli; class Connection { protected $mysqli; protected $host = 'localhost'; protected $db = 'database'; public function __construct(\mysqli\User $user) { $this->mysqli = new \mysqli($this->host, $user->getUser(), $user->getPwd(), $this->db); if (mysqli_connect_errno()) { throw new \RuntimeException(mysqli_connect_error()); } } public function getMysqli() { return $this->mysqli; } } ``` SOLUTION 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 :-)