How can I debug my database connection for unit testing?

I’ve set up a wordpress environment on my Mac with MAMP and I’m running 3.5.1 locally (i.e. http://localhost:8888/blog works). I’m trying to run the unit test suit from wordpress.org and having trouble connecting to the database.

$svn co https://unit-tests.svn.wordpress.org/trunk wordpress-tests
$cd wordpress-tests
$cp wp-tests-config-sample.php wp-tests-config.php

edit wp-test-config.php for my setup

Read More
$ cat wp-tests-config.php
<?php

/* Path to the WordPress codebase you'd like to test. Add a backslash in the end. */
define( 'ABSPATH', '/Applications/MAMP/htdocs/blog/' );

// Test with multisite enabled: (previously -m)
// define( 'WP_TESTS_MULTISITE', true );

// Force known bugs: (previously -f)
// define( 'WP_TESTS_FORCE_KNOWN_BUGS', true );

// Test with WordPress debug mode on (previously -d)
// define( 'WP_DEBUG', true );

// ** MySQL settings ** //

// This configuration file will be used by the copy of WordPress being tested.
// wordpress/wp-config.php will be ignored.

// WARNING WARNING WARNING!
// These tests will DROP ALL TABLES in the database with the prefix named below.
// DO NOT use a production database or one that is shared with something else.

define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'root' );
define( 'DB_PASSWORD', 'root' );
define( 'DB_HOST', 'localhost:8889' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );

$table_prefix  = 'wp_';   // Only numbers, letters, and underscores please!

define( 'WP_TESTS_DOMAIN', 'example.org' );
define( 'WP_TESTS_EMAIL', 'admin@example.org' );
define( 'WP_TESTS_TITLE', 'Test Blog' );

define( 'WP_PHP_BINARY', 'php' );

define( 'WPLANG', '' );

But, when I try to run the test suite, I phpunit can’t connect to the database:

$phpunit
...
<h1>Error establishing a database connection</h1>
<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>localhost:8889</code>. This could mean your host's database server is down.</p>
...

Can anyone help debug this issue?

** EDIT **

After adding wp-content/db-error.php as described below. I’m getting the following error:

$ phpunit
PHP Warning:  mysql_error() expects parameter 1 to be resource, boolean given in /Users/spinlock/WordPress/wordpress-3.5.1/wp-includes/wp-db.php on line 1023
WordPress database error  for query  made by require_once('/Users/spinlock/WordPress/wordpress-3.5.1/wp-settings.php'), wp_set_wpdb_vars, dead_db, require_once('/Users/spinlock/WordPress/wordpress-3.5.1/wp-content/db-error.php')
Running as single site... To run multisite, use -c multisite.xml
PHP Warning:  mysql_error() expects parameter 1 to be resource, boolean given in /Users/spinlock/WordPress/wordpress-3.5.1/wp-includes/wp-db.php on line 1023
WordPress database error  for query  made by PHPUnit_TextUI_Command::main, PHPUnit_TextUI_Command->run, PHPUnit_TextUI_Command->handleArguments, PHPUnit_TextUI_Command->handleBootstrap, PHPUnit_Util_Fileloader::checkAndLoad, PHPUnit_Util_Fileloader::load, include_once('/Users/spinlock/WordPress/wordpress-tests/includes/bootstrap.php'), require_once('/Users/spinlock/WordPress/wordpress-3.5.1/wp-settings.php'), wp_set_wpdb_vars, dead_db, require_once('/Users/spinlock/WordPress/wordpress-3.5.1/wp-content/db-error.php')

wp-db.php line 1023 is the implementation of print_error() and it’s choking here:

1019   function print_error( $str = '' ) {
1020     global $EZSQL_ERROR;
1021
1022     if ( !$str )
1023       $str = mysql_error( $this->dbh );
1024     $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
1025

Solution

Ok, I finally got this working by changing DB_HOST to localhost:/Applications/MAMP/tmp/mysql/mysql.sock.

And the tests are running:

$ phpunit
Installing...
Running as single site... To run multisite, use -c multisite.xml
Not running ajax tests... To execute these, use --group ajax.
PHPUnit 3.7.19 by Sebastian Bergmann.

Configuration read from /Users/spinlock/WordPress/wordpress-tests/phpunit.xml.dist

.............FFF................PHP Fatal error:  Call to undefined function wp_unslash() in /Users/spinlock/WordPress/wordpress-tests/tests/attachment/slashes.php on line 45

Fatal error: Call to undefined function wp_unslash() in /Users/spinlock/WordPress/wordpress-tests/tests/attachment/slashes.php on line 45

Related posts

Leave a Reply

1 comment

  1. Put a file named db-error.php into the wp-content directory. Here you can ask the global $wpdb object for more detailed errors:

    global $wpdb;
    $wpdb->print_error();
    

    This will print out the last error message if the database handler.