加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用PHPUnit类’mysqli’找不到

发布时间:2020-12-13 17:52:51 所属栏目:PHP教程 来源:网络整理
导读:我刚刚开始使用 PHPUnit.我写的一些简单测试正在运行.所以一般来说PHPUnit已启动并正在运行.但MySQLi类有问题. 在我的代码中它工作正常.这是一行: $this-mysqli = new mysqli($this-host,$user-getUser(),$user-getPwd(),$this-db); 当运行phpunit解析此行
我刚刚开始使用 PHPUnit.我写的一些简单测试正在运行.所以一般来说PHPUnit已启动并正在运行.但MySQLi类有问题.

在我的代码中它工作正常.这是一行:

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

当运行phpunit解析此行时,我收到以下错误消息(指向该行):

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

两种可能性(我认为):

1)我缺少一些功能/扩展/配置步骤/与使用MySQLi扩展的PHPUnit正确设置相关的其他内容.

编辑

如果我测试扩展extension_loaded(‘mysqli’),它会在我的普通代码中返回true.如果我在测试中执行以下操作,则会跳过测试(即返回false):

if (!extension_loaded('mysqli')) {
    $this->markTestSkipped(
        'The MySQLi extension is not available.'
    );
}

/编辑

2)我的代码可能有问题.我正在尝试模拟User对象以进行测试连接.所以这里是:

<?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);
    }
}

经过测试的Connection类看起来像这样:

<?php
namespace mysqli;

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

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

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

整件事是安装问题.我正在使用XAMPP安装,它提供了我的PHP.独立安装PHPUnit使其使用不同的设置!所以在我的浏览器(XAMPP驱动)一切都很好,但在我的命令行中,MySQLi扩展已经一共丢失了! Debian提供了一个名为php5-mysqlnd的软件包.安装好后一切正常! (除了出现的其他错误:-)

PHPUnit通常在CLI – 命令行界面中运行.

你所拥有的PHP与网络服务器不同.不同的二进制文件也经常是不同的配置.

$php -r "new mysqli();"

应该给你同样的错误.验证二进制文件和配置的位置:

$which php

$php -i | grep ini

确保您已安装并启用了the extension for the mysqli class.配置完成后,您应该能够完美地运行单元测试.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读