Support » Plugins » Hacks » WP_UnitTestCase::tearDownAfterClass() -> “mysqli_query(): Couldn’t fetch mysqli”

  • Hi there,

    I’ve been getting an error with the test tool (https://develop.svn.wordpress.org/tags/4.7/tests/phpunit/includes/) of WordPress 4.7. The error never occurred with the one bundled with v4.6 or below (https://develop.svn.wordpress.org/tags/4.6.1/tests/phpunit/includes/).

    The tearDownAfterClass() method of the WP_UnitTestCase class produces the following error.

    
      [PHPUnit_Framework_Exception]
      mysqli_query(): Couldn't fetch mysqli
    

    This is the function call trace which results in the error.

    
    mysqli_query() at wp-test\wp-includes\wp-db.php:1877
    wpdb->_do_query() at wp-test\wp-includes\wp-db.php:1765
    wpdb->query() at wp-test\wordpress-tests-lib\includes\functions.php:65
    _delete_all_data() at wp-test\wordpress-tests-lib\includes\testcase.php:76
    WP_UnitTestCase::tearDownAfterClass() at n/a:n/a
    

    This does not occur with the one that comes with v4.6 or below.

    The code of the method from v4.6.

    
    	public static function tearDownAfterClass() {
    		parent::tearDownAfterClass();
    
    		$c = self::get_called_class();
    		if ( ! method_exists( $c, 'wpTearDownAfterClass' ) ) {
    			return;
    		}
    
    		call_user_func( array( $c, 'wpTearDownAfterClass' ) );
    
    		self::commit_transaction();
    	}
    

    The code of the method from v4.7.

    
    	public static function tearDownAfterClass() {
    		parent::tearDownAfterClass();
    
    		_delete_all_data();
    		self::flush_cache();
    
    		$c = self::get_called_class();
    		if ( ! method_exists( $c, 'wpTearDownAfterClass' ) ) {
    			self::commit_transaction();
    			return;
    		}
    
    		call_user_func( array( $c, 'wpTearDownAfterClass' ) );
    
    		self::commit_transaction();
    	}
    

    Looks like

    
    		_delete_all_data();
    		self::flush_cache();
    

    is something to do with it. I wonder if others don’t get the error.

    Does anybody have a clue?

    • This topic was modified 11 months, 2 weeks ago by  umchal. Reason: Removed the wrong information
    • This topic was modified 11 months, 2 weeks ago by  umchal. Reason: `tearDown()` was not responsible
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Issues due to persistent cache data are becoming more of a problem in general. Adding a cache flush is a general improvement. I’m away from my test rig, so I cannot confirm if it works for me, but I would expect it would.

    I’m wondering why your installation has a problem with mysqli_query(). The mysqli class has been the standard way to interface with mySQL since mysql had been deprecated, and more so recently as mysql is removed in PHP7. Maybe an update beyond WP is order?

    Thanks for your insight.

    I was using Codeception to run tests and it seems that Codeception calls the tearDownAfterClass() method after all the test cases are done (https://github.com/Codeception/Codeception/issues/3198) and it is after the database connection is closed. That seems to be the cause of the error.

    As a workaround, I created a class extending WP_UnitTestCase by overriding the tearDownAfterClass() method to have the code of v4.6.x or below and made all tests use the extended class. This at least dodges the error.

    I’m seeing the same error, but I’m not using Codeception.

    Somehow, by the time the tearDownAfterClass() method is called, the connection to the database is closed or otherwise not available.

    I was able to remove the error overriding the tearDownAfterClass() method in the base class for all my tests:

    public static function tearDownAfterClass() {
        global $wpdb;
        @$wpdb->check_connection();
    
        parent::tearDownAfterClass();
    }

    This, of course, is not a great solution, but it works while we discover why is the connection not available at that point.

    Heads up: I filed this on Trac after finding the problem (PHPUnit’s backupGlobals option). The simple solution is to set protected $backupGlobals = false in your unit test.

    Will continue to investigate the problem there. 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_UnitTestCase::tearDownAfterClass() -> “mysqli_query(): Couldn’t fetch mysqli”’ is closed to new replies.