【发布时间】:2016-10-05 00:42:58
【问题描述】:
好的,我正在为我的 wordpress 插件测试我的 ajax 回调。 所以我基本上按照说明here 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();
}
这是我的测试
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 );
}
}
问题来了
- 它不会像假设的那样抛出 WPAjaxDieStopException 异常
当我执行此代码时$this->setExpectedException( 'WPAjaxDieStopException' );
它没有通过测试https://snag.gy/JSTqHV.jpg
- 打印出 wp_die() 已被触发,所以这段代码
$response = $this->_last_response;
var_dump( $response );
打印这个
数字 2 是个问题,因为您不能执行 json_decode 输出的字符串,因为它是无效的 json 字符串,所以我无法继续我的测试。
我刚刚开始对 wordpress 插件进行自动化测试,感谢任何帮助。
注意: 即使我使用 wp_die(),我的 ajax 回调在我的实时插件上也能正常工作,它只是在我的测试中打印出那个奇怪的 'wp_die called ...' 字符串。
我的php版本是5.6.21,我的phpunit版本是4.8.26
这里有一些附加信息
所以 'WPAjaxDieStopException' 和 'WPAjaxDieContinueException' 都不会被抛出,
但有趣的是当我这样做时
$this->_setRole( 'administrator' );
我在控制台上收到此错误
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
但显然我正在扩展 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
另外,我正在使用 vagrant 并在我的开发环境中使用 http://vccw.cc/,并遵循本指南添加针对 woocommerce 扩展的测试 https://github.com/Automattic/wc-extensions-code-test-guide
希望所有这些额外信息将有助于最终解决此问题。
【问题讨论】:
-
你试过添加 $this->assertEquals(..);或 $this->assertTrue( isset( $e ) );在 try-catch 之后立即?因为你可能会收到手册上说的一些输出。
-
我不知道这件事 :(
-
@Oooogi 不走运,$this->assertTrue(isset($e));测试失败,没有任何异常被抛出。
-
你能试试把: add_action( 'wp_ajax_my_plugin_get_site_pages' , array( $this , 'my_plugin_get_site_pages' ) );而不是你写的和检查的?
-
@Oooogi 是的,已经这样做了,没有运气。
标签: php ajax unit-testing wordpress