mysqli_query(): Couldn't fetch mysqli while testing a PHP function in PHPUnit

…衆ロ難τιáo~ 提交于 2019-12-12 04:18:30

问题


I'm trying to write a PHPUnit test function on the school project I'm working on. The functions work fine with the normal web site. But when I try to test database related operations with PHPUnit, I keep getting mysqli_query(): Couldn't fetch mysqli.

Here is the a sample code snippet:

PHPUnit File: test.php

function testFindById() {
    $expected_object = "Artist";
    $result = Artist::find_by_id($id);
    $result_type = gettype($result);
    $this->assertEquals($expected_object, $result_type);
}

DatabaseHelper Class: DatabaseHelper.php

public static function find_by_sql($sql) {
    global $database;

    $result = $database->query($sql);
    $objects = array();
    if ($result) {
        while ($row = mysqli_fetch_array($result)) {
            $objects[] = static::new_instance($row);
        }
    }
    return $objects;
}    

public static function find_by_id($id) {
    global $database;

    $sql = "SELECT * FROM " . static::$table_name . " WHERE id = $id LIMIT 1";
    $result = static::find_by_sql($sql);
    if (!empty($result)) {
        return array_shift($result);
    } else {
        return false;
    }
}

Artist Class: Artist.php

class Artist extends DatabaseHelper {
    protected static table_name = "artists";
    // more irrelevant code follows
}

TEST OUTPUT

C:\Xampp\htdocs\musicstore-oop-beta\test>phpunit --verbose MusicStoreTest.php
PHPUnit 3.7.21 by Sebastian Bergmann.

SSE

Time: 0 seconds, Memory: 2.00Mb

There was 1 error:

1) MusicStoreTest::testFindById
mysqli_query(): Couldn't fetch mysqli

C:\Xampp\htdocs\musicstore-oop-beta\app\Database.php:38
C:\Xampp\htdocs\musicstore-oop-beta\app\DatabaseHelper.php:23
C:\Xampp\htdocs\musicstore-oop-beta\app\DatabaseHelper.php:37
C:\Xampp\htdocs\musicstore-oop-beta\test\MusicStoreTest.php:62

There were 2 skipped tests:

1) MusicStoreTest::testAddArtist
Artist Add Test Skipped

C:\Xampp\htdocs\musicstore-oop-beta\test\MusicStoreTest.php:24

2) MusicStoreTest::testAddAlbum
Album Add Test Skipped

C:\Xampp\htdocs\musicstore-oop-beta\test\MusicStoreTest.php:40

FAILURES!
Tests: 3, Assertions: 0, Errors: 1, Skipped: 2.

回答1:


This occurs when either the database variable (in your case $database) does not refer to a connected database. You should check that the database connection is valid:

$database = new mysqli('db_host','user','password','db_name'); 

if ($database->connect_errno) {
    error_log("Errno: " . $mysqli->connect_errno . ':' . mysqli->connect_error);
}

This can also occur if the variable used to store the database object is not in scope. Say if global $database; statement were missing (not applicable to your code which has the statement) then $database would only have local function scope - it would be a different variable to the global $database variable.



来源:https://stackoverflow.com/questions/45139719/mysqli-query-couldnt-fetch-mysqli-while-testing-a-php-function-in-phpunit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!