【发布时间】:2018-06-06 13:58:29
【问题描述】:
我正在为插件编写集成测试,我使用wp-cli 和scaffolding 进行了所有测试。当我运行phpunit 时,它们运行良好。但我遇到的问题是我正在使用 composer 和 npm - 作曲家来获得一些额外的功能,并使用 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 只是我的 enqueue 方法在 setUp() 方法中的类的一个实例。
我得到一个错误,因为它说Failed asserting that false is true.
我正在测试的插件已构建并安装了 composer。当我在本地的 WordPress 实例上时,所有文件夹都存在并且一切正常。但是测试实例和我的本地实例(ofc)不一样。我在 enqueue 方法中 error_loged 以检查 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时运行一次吗?
【问题讨论】:
标签: php wordpress unit-testing phpunit integration-testing