我正在为插件编写集成测试,我使用 wp-cli
完成了所有操作和 scaffolding测试。当我运行 phpunit
时,它们运行良好。但我遇到的问题是我正在使用 composer
和npm
- 一些额外功能的 Composer ,以及 npm
用于捆绑我的脚本。
脚本部分很重要,因为我正在将 public
中的脚本排入队列。文件夹(构建文件夹)
$main_script = 'public/scripts/application.js';
wp_register_script( 'plugin-scripts', plugin_dir_url( __DIR__ ) . $main_script, array() );
wp_enqueue_script( 'plugin-scripts' );
我需要测试我的脚本和样式是否已排队,因此我添加了一个测试
public function test_enqueued_scripts() {
$this->admin->enqueue_styles();
$this->assertTrue( wp_script_is( 'plugin-scripts' ) );
}
$this->admin
只是我的入队方法位于 setUp()
中的类的一个实例方法。
我收到一个错误,因为它说 Failed asserting that false is true.
我正在测试的插件已构建并安装了 Composer 。当我在本地 WordPress 实例上时,所有文件夹都存在并且一切正常。但测试实例与我的本地实例(ofc)不一样。我error_log
在 enqueue 方法中编辑以检查 file_exist
是否我得到false
.
我需要使用 phpunit 对此进行测试(客户要求具有完整的测试覆盖率)。
我的bootstrap.php
看起来像这样
<?php
/**
* PHPUnit bootstrap file
*
* @package Plugin
*/
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL;
exit( 1 );
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
// Update array with plugins to include ...
$plugins_to_active = array(
'advanced-custom-fields-pro/acf.php',
'my-plugin/my-plugin.php',
);
update_option( 'active_plugins', $plugins_to_active );
require dirname( dirname( dirname( __FILE__ ) ) ) . '/advanced-custom-fields-pro/acf.php';
require dirname( dirname( __FILE__ ) ) . '/my-plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
如何启动构建过程 ( npm run build
),以便我的脚本在单元测试之前存在?另外,是否可以让此构建步骤仅运行一次,而不是每次运行 phpunit
时运行一次? ?
最佳答案
一个非常基本的解决方案是检查 npm 在 Bootstrap 中使用 file_exists()
创建的文件。
如果不存在,请使用 shell_exec()
运行构建
关于php - 需要执行composer和npm的WordPress插件的集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722315/