I just started with PHPUnit . Some simple tests that I wrote work. So in general, PHPUnit works. However, something is wrong with the MySQLi class.
It works fine in my code. Here's the line:
$this->mysqli = new \mysqli($this->host, $user->getUser(), $user->getPwd(), $this->db);
When this line is processed by phpunit, I get the following error message (pointing to this line):
PHP Fatal error: Class 'mysqli' not found in /opt/lampp/htdocs/...
Two possibilities (as I see):
1) I am missing some element / extension / configuration / something else related to the correct PHPUnit configuration working with the MySQLi extension.
EDIT
If I test extension extension_loaded('mysqli') , it returns true in my regular code. If I do the following in a test, it skips the test (i.e. returns false ):
if (!extension_loaded('mysqli')) { $this->markTestSkipped( 'The MySQLi extension is not available.' ); }
/ EDIT
2) There may be a problem with my code. I am trying to make fun of 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 verified connection class is as follows:
<?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; } }
Decision
All this problem is related to the installation. I am using the XAMPP installation, which provides my PHP. Installing PHPUnit independently makes it using a different installation! So everything was fine in my browser (powered by XAMPP), but on my command line, the MySQLi extension went missing together! Debian provides a package called php5-mysqlnd. After that, everything works fine! (except for the appearance of other errors :-)