WP_Ajax_UnitTestCase不会抛出WPAjaxDieStopException

Jpl*_*us2 15 php ajax wordpress unit-testing wordpress-plugin

好的,我正在测试我的wordpress插件的ajax回调.所以我基本上按照这里的说明 https://codesymphony.co/wp-ajax-plugin-unit-testing/

这是我的ajax回调函数

public function my_plugin_get_site_pages( $args = null ) {

  //...... Processing $site_pages.....

  $response = array(
   'status'     => 'success',
   'site_pages' => $site_pages
  );

  @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
  echo wp_json_encode( $response );
  wp_die();

}
Run Code Online (Sandbox Code Playgroud)

这是我的测试

class My_Plugin_Ajax_Test extends WP_Ajax_UnitTestCase {

  private $_foo;

  public function setup() {

    //.....Initialize $_foo here...

  }

  public function test_foo() {

    try {

        $_POST[ 'args' ] = array( 'return_format' => 'raw' );

        add_action( 'wp_ajax_my_plugin_get_site_pages' , array( $this->_foo , 'my_plugin_get_site_pages' ) );

        //$this->setExpectedException( 'WPAjaxDieStopException' );
        $this->_handleAjax( 'my_plugin_get_site_pages' );

    } catch ( WPAjaxDieStopException $e ) {}

    //$response = json_decode( $this->_last_response );
    $response = $this->_last_response;
    var_dump( $response );

  }

}
Run Code Online (Sandbox Code Playgroud)

现在这里是问题

  1. 它不会像它的假设那样抛出WPAjaxDieStopException异常

当我执行此代码时,$this->setExpectedException( 'WPAjaxDieStopException' ); 它无法通过测试https://snag.gy/JSTqHV.jpg

  1. 它打印出wp_die()已被触发,所以这段代码

$response = $this->_last_response; var_dump( $response );

打印这个

https://snag.gy/pKqfUk.jpg

数字2是一个问题,因为你不能做json_decode输出的字符串,因为它是一个无效的json字符串,所以我不能继续我的测试.

我刚刚开始对wordpress插件进行自动化测试,我很感激任何帮助.

注意:我的ajax回调在我的实时插件上运行正常,即使我使用wp_die(),它只是在我的测试中打印出奇怪的'wp_die called ...'字符串.

我的php版本是5.6.21,我的phpunit版本是4.8.26


这是一些额外的信息

因此,不会抛出'WPAjaxDieStopException'和'WPAjaxDieContinueException',

然而,当我这样做时,有趣的是什么

$this->_setRole( 'administrator' );
Run Code Online (Sandbox Code Playgroud)

我在控制台上收到此错误

Trying to get property of non-object

/tmp/wordpress-tests-lib/includes/testcase-ajax.php:151
/vagrant/www/wordpress/wp-content/plugins/my-plugin/tests/test-file.php:30
Run Code Online (Sandbox Code Playgroud)

但显然我正在扩展WP_Ajax_UnitTestCase并且它具有_setRole方法 https://core.trac.wordpress.org/browser/trunk/tests/phpunit/includes/testcase-ajax.php#L168

此外,当我运行phpunit时,我在控制台上收到了一堆错误或警告

Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
WordPress database error Duplicate key name 'location_type_code' for query ALTER TABLE wptests_woocommerce_tax_rate_locations ADD KEY location_type_code (location_type(40),location_code(90)) 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('/vagrant/www/wordpress/wp-content/plugins/my-plugin/tests/bootstrap.php'), require('/tmp/wordpress-tests-lib/includes/bootstrap.php'), require_once('wp-settings.php'), do_action('init'), call_user_func_array, WC_Install::check_version, WC_Install::install, WC_Install::create_tables, dbDelta
Run Code Online (Sandbox Code Playgroud)

此外,我正在使用vagrant并使用http://vccw.cc/进行我的开发环境,并遵循本指南添加针对woocommerce扩展的测试 https://github.com/Automattic/wc-extensions-code-test-guide

希望所有这些额外的信息将有助于最终解决这个问题.

Jpl*_*us2 4

离开了一段时间,很忙,终于有时间来解决这个问题了。事实证明这是一个愚蠢的错误(捂脸)。

由于我们使用的是 WP_Ajax_UnitTestCase ,它扩展了 WP_UnitTestCase

然后当我们在 WP_Ajax_UnitTestCase 中使用函数设置时,我们需要调用它parent::setup();

public function setup() {
    parent::setup();
    // your init codes here
}
Run Code Online (Sandbox Code Playgroud)

我没有在现有代码上调用它,这就是测试表现得很奇怪的原因。添加它可以解决所有奇怪的问题并按预期运行测试并引发必要的异常。

如果 ajax 回调函数没有产生任何输出,则抛出WPAjaxDieStopException 。

如果 ajax 回调产生任何输出,则抛出WPAjaxDieContinueException 。

还要确保在您的 ajax 回调中使用wp_die()而不是使用die(),如果您使用后者,测试将停止。

我计划编写一份关于在 WordPress 插件中进行自动化测试的详细指南,我很快就会将其链接放在这里。

现在我希望这对任何人都有帮助。