With PHPUnit Class #39;mysqli#39; is not found(找不到 PHPUnit 类 mysqli)
问题描述
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('mysqliUser')
->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 mysqliConnection($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(mysqliUser $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 :-)
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.
这篇关于找不到 PHPUnit 类 'mysqli'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:找不到 PHPUnit 类 'mysqli'
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01