【问题标题】:Integration testing of a WordPress plugin that needs composer and npm to be executed需要执行 composer 和 npm 的 WordPress 插件的集成测试
【发布时间】:2018-06-06 13:58:29
【问题描述】:

我正在为插件编写集成测试,我使用wp-cliscaffolding 进行了所有测试。当我运行phpunit 时,它们运行良好。但我遇到的问题是我正在使用 composernpm - 作曲家来获得一些额外的功能,并使用 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


    【解决方案1】:

    一个非常基本的解决方案是检查 npm 在引导程序中使用 file_exists() 创建的文件。

    如果不存在,请使用 shell_exec() 运行构建

    http://php.net/manual/en/function.file-exists.php

    http://php.net/manual/en/function.shell-exec.php

    【讨论】:

    • 在测试中还是在引导程序中?
    • 在助推器中!
    • 原来在boostrap.php中,目录指向本地构建插件,但在我的enqueue方法中,plugin_dir_url( __DIR__)http://example.org/wp-content/plugins/Users/dingo_d/vagrant-local/www/projects/project/public_html/wp-content/plugins/my-plugin/。我不知道这个http://example.org/wp-content/plugins/ 来自哪里......
    • plugin_dir_url() 听起来像一个采用__DIR__ 的方法,并用url 替换站点根目录。你不需要URL,你想要realpath()
    • 我的意思是我在插件中使用它,它工作正常,只是在单元测试中表现得很奇怪。我会看看它可能是什么并报告。
    【解决方案2】:

    您需要在执行断言之前运行do_action('wp_enqueue_scripts')This article 有更多细节和示例。因此,您的测试应如下所示:

    public function test_enqueued_scripts() {
        $this->admin->enqueue_styles();
        do_action('wp_enqueue_scripts');
        $this->assertTrue( wp_script_is( 'plugin-scripts' ) );
    }
    

    我已经测试过这个解决方案,它可以工作。

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 2013-07-02
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      相关资源
      最近更新 更多