在PHPUnit类“mysqli的”未找到(With PHPUnit Class 'mysq

2019-06-23 17:37发布

我刚开始用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)我失去了一些功能/扩展/配置步/别的东西有关的PHPUnit与MySQLi扩展工作正确的设置。

编辑

如果我为扩展做测试extension_loaded('mysqli')它返回true在我的正常的代码。 如果我做的测试中它后面跳过测试(即它返回false ):

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

/编辑

2)有可能是我的代码有问题。 我试图嘲弄用户对象,以使测试连接。 所以在这里,它是:

<?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, 
                                    $user->getUser(),
                                    $user->getPwd(),
                                    $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包。 有了这个安装的一切工作正常! (除其他错误出现:-)

Answer 1:

命令行界面 - PHPUnit的通常的CLI中运行。

你到了那里的PHP比与Web服务器不同。 不同的二进制和通常是一个不同的配置,以及。

$ php -r "new mysqli();"

应该给你同样的错误。 验证,其中二进制文件和配置位于:

$ which php

$ php -i | grep ini

确保你有扩展的mysqli类安装并启用。 一旦配置完成,你应该能够完美运行单元测试。



文章来源: With PHPUnit Class 'mysqli' is not found