使用PHPUnit类’mysqli’找不到

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用PHPUnit类’mysqli’找不到脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始使用 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('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);
    }
}

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

<?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,$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.配置完成后,您应该能够完美地运行单元测试.

脚本宝典总结

以上是脚本宝典为你收集整理的使用PHPUnit类’mysqli’找不到全部内容,希望文章能够帮你解决使用PHPUnit类’mysqli’找不到所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: