我在跟踪https://make.wordpress.org/cli/handbook/plugin-unit-tests/ 为插件运行单元测试。
我创建了一个新插件。
$ wp scaffold plugin hello-plugin-1
Success: Created plugin files.
Success: Created test files.
运行
install-wp-tests.sh
.
$ cd wp-content/plugins/hello-plugin-1
$ ./bin/install-wp-tests.sh wordpress_test_1 root root localhost latest
+ install_wp
+ \'[\' -d /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/ \']\'
+ return
+ install_test_suite
++ uname -s
+ [[ Darwin == \\D\\a\\r\\w\\i\\n ]]
+ local ioption=-i.bak
+ \'[\' \'!\' -d /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib \']\'
+ \'[\' \'!\' -f wp-tests-config.php \']\'
+ download https://develop.svn.wordpress.org/tags/5.4/wp-tests-config-sample.php /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
++ which curl
+ \'[\' /usr/bin/curl \']\'
+ curl -s https://develop.svn.wordpress.org/tags/5.4/wp-tests-config-sample.php
++ echo /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/
++ sed \'s:/\\+$::\'
+ WP_CORE_DIR=/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/
+ sed -i.bak \'s:dirname( __FILE__ ) . \'\\\'\'/src/\'\\\'\':\'\\\'\'/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress//\'\\\'\':\' /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/youremptytestdbnamehere/wordpress_test_1/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/yourusernamehere/root/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/yourpasswordhere/root/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak \'s|localhost|localhost|\' /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ install_db
+ \'[\' false = true \']\'
+ PARTS=(${DB_HOST//\\:/ })
+ local PARTS
+ local DB_HOSTNAME=localhost
+ local DB_SOCK_OR_PORT=
+ local EXTRA=
+ \'[\' -z localhost \']\'
++ echo
++ grep -e \'^[0-9]\\{1,\\}$\'
+ \'[\' \']\'
+ \'[\' -z \']\'
+ \'[\' -z localhost \']\'
+ EXTRA=\' --host=localhost --protocol=tcp\'
+ mysqladmin create wordpress_test_1 --user=root --password=root --host=localhost --protocol=tcp
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
运行
phpunit
并出现此DB连接错误。
$ phpunit
PHP Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php on line 1626
PHP Stack trace:
PHP 1. {main}() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/includes/install.php:0
PHP 2. require_once() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/includes/install.php:29
PHP 3. require_wp_db() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-settings.php:126
PHP 4. wpdb->__construct() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/load.php:426
PHP 5. wpdb->db_connect() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php:631
PHP 6. mysqli_real_connect() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php:1626
wp_die called
Message : <p><code>No such file or directory</code></p>
<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</code>. This could mean your host’s database server is down.</p>
<ul>
<li>Are you sure you have the correct username and password?</li>
<li>Are you sure you have typed the correct hostname?</li>
<li>Are you sure the database server is running?</li>
</ul>
<p>If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="https://wordpress.org/support/forums/">WordPress Support Forums</a>.</p>
Title :
我们需要在中设置DB吗
wp-config.php
在里面
/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress
在我运行phpunit之前?我试过了,但没有成功。知道哪里出了问题吗?
我在跑步WP-CLI 2.4.0
和PHPUnit 7.5.9
.