与PHPUnit类'mysqli'找不到 [英] With PHPUnit Class 'mysqli' is not found

查看:67
本文介绍了与PHPUnit类'mysqli'找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 PHPUnit .我编写的一些简单测试正在运行.因此,一般而言,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);

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

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)我缺少某些功能/扩展/配置步骤/与与MySQLi扩展一起使用的PHPUnit的正确设置有关的其他内容.

1) I'm missing some feature / extension / configuration step / something else related to a correct setup of PHPUnit working with the MySQLi extension.

编辑

如果我测试扩展名extension_loaded('mysqli'),它将在我的常规代码中返回true.如果我在测试"中执行以下操作,则会跳过测试(即返回false):

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)我的代码可能有问题.我正在尝试模拟User对象以建立测试连接.所以这里是:

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

经过测试的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;
    }
}

解决方案

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

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通常在CLI-命令行界面中运行.

PHPUnit normally runs within the CLI - command line interface.

您所拥有的PHP与网络服务器不同.不同的二进制文件,通常也有不同的配置.

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

还有

$ php -i | grep ini

确保您已经 mysqli类的扩展名已安装并启用.配置完成后,您应该可以完美地运行单元测试.

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'找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆