2

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

$ 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', '[email protected]' );
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
8
  • shouldn't your db host be on the same as the blog itself? "localhost:8888"
    – NW Tech
    Commented Apr 26, 2013 at 22:49
  • Did you try connecting to mysql with the given credentials? I.e. executing mysql -h localhost -u root -P 8889 -p in a shell and then use show databases to look if there is db 'wordpress' which is accessable?
    – s1lv3r
    Commented Apr 26, 2013 at 23:02
  • @NWTech I don't think so. I configured the WordPress install this way in wp-config-sample.php and that is working fine. I tried changing DB_HOST to localhost:8888 and localhost but I get the same error each time.
    – user341493
    Commented Apr 26, 2013 at 23:04
  • apache on port 8888 and mysql on port 8889 is the default setting in mamp. If the defaults weren't changed, the configuration regarding ports should be just fine.
    – s1lv3r
    Commented Apr 26, 2013 at 23:06
  • @s1lv3r I can't connect that way $ mysql -h localhost -P 8889 -u root -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38) But I can connect through the socket $ mysql -u root -p --socket=/Applications/MAMP/tmp/mysql/mysql.sock and see the db 'wordpress' from there.
    – user341493
    Commented Apr 26, 2013 at 23:40

1 Answer 1

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.

4
  • Where is the error message printed? To syslog? To a log file?
    – user84207
    Commented Jun 8, 2014 at 6:46
  • When I do this, I just get global $wpdb;... as text in my browser.
    – user84207
    Commented Jun 8, 2014 at 6:48
  • I'm a beginner with php. The problem was that I wasn't adding <?php at the beginning of the file. Now I get a blank page instead though.
    – user84207
    Commented Jun 8, 2014 at 7:05
  • This did not work.
    – Meekohi
    Commented Mar 6, 2015 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.