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

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

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