如何调试我的数据库连接以进行单元测试?

时间:2013-04-26 作者:user341493

我已经用MAMP在Mac上设置了wordpress环境,我正在本地运行3.5.1(即。http://localhost:8888/blog 工程)。我正在试着运行wordpress的单元测试套件。org并无法连接到数据库。

$svn co https://unit-tests.svn.wordpress.org/trunk wordpress-tests
$cd wordpress-tests
$cp wp-tests-config-sample.php wp-tests-config.php
编辑wp-test-config.php 对于我的设置

$ 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\', \'\' );
但是,当我尝试运行测试套件时phpunit 无法连接到数据库:

$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>
...
有人可以帮助调试此问题吗?

**编辑**

添加后wp-content/db-error.php 如下所述。我收到以下错误:

$ 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第1023行是print_error() 这里令人窒息:

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

好吧,我终于通过改变DB_HOSTlocalhost:/Applications/MAMP/tmp/mysql/mysql.sock.

测试正在运行:

$ 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

1 个回复
SO网友:fuxia

放置名为的文件db-error.php 进入wp-content 目录在这里,您可以询问全球$wpdb 对象以获取更详细的错误:

global $wpdb;
$wpdb->print_error();
如果数据库处理程序失败,这将打印出最后一条错误消息。

结束