我一直在努力让 PHPUnit 与 WordPress 很好地配合(在 , , , , , 和 ),我想我已经非常接近了。我在测试之前直接运行的代码看起来像这样(基本上逐字地从 本文):

( PHP_SAPI === 'cli' ) || die( 'Access Denied' );

define( 'PHPUNIT_DB_PREFIX', 'phpunit_' );

global $wp_rewrite, $wpdb;

define( 'WP_MEMORY_LIMIT', '100M' );

require_once( dirname( __FILE__ ) . '/../../../../wp-load.php' );
require_once( ABSPATH . 'wp-admin/includes/admin.php' );

wp_set_current_user( 1 );

似乎工作正常,除了函数根据它们在文件中的位置返回不同的东西。例如,在测试课之外, shortcode_exists 返回 true,而在测试用例中完全相同 shortcode_exists 语句返回 false。我究竟做错了什么?

以下是整个测试文件的内容:

[自我最初发布此文件以来,该文件已更改...]

编辑:好的,所以我已尽可能严格地遵循 J.D. 的教程。感觉 真的 关闭。我现在收到此错误:

PHPUnit_Framework_Error_Warning : copy(data/not-gettexted-0.php): failed to open stream: No such file or directory
#0 [internal function]: PHPUnit_Util_ErrorHandler::handleError(2, 'copy(data/not-g...', '/Applications/w...', 41, Array)
#1 /Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/wordpress-dev/trunk/tools/i18n/t/NotGettextedTest.php(41): copy('data/not-gettex...', 'data/not-gettex...')
#2 [internal function]: NotGettextedTest->test_replace()
#3 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/Framework/TestCase.php(988): ReflectionMethod->invokeArgs(Object(NotGettextedTest), Array)
#4 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/Framework/TestCase.php(838): PHPUnit_Framework_TestCase->runTest()
#5 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/Framework/TestResult.php(648): PHPUnit_Framework_TestCase->runBare()
#6 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/Framework/TestCase.php(783): PHPUnit_Framework_TestResult->run(Object(NotGettextedTest))
#7 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/Framework/TestSuite.php(779): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#8 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/Framework/TestSuite.php(749): PHPUnit_Framework_TestSuite->runTest(Object(NotGettextedTest), Object(PHPUnit_Framework_TestResult))
#9 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/Framework/TestSuite.php(709): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#10 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/TextUI/TestRunner.php(350): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#11 phar:///Applications/wordpress-3.8.3-0/apps/wordpress/htdocs/wp-content/plugins/hf-accountability/tests/phpunit-lts.phar/phpunit/TextUI/Command.php(176): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#12 /private/var/folders/r5/0w5bp7b13pndfymz_pgf_zz00000gn/T/ide-phpunit.php(268): PHPUnit_TextUI_Command->run(Array, true)
#13 /private/var/folders/r5/0w5bp7b13pndfymz_pgf_zz00000gn/T/ide-phpunit.php(506): IDE_Base_PHPUnit_TextUI_Command::main()
#14 {main}

知道我该如何解决这个问题吗?同时,我将继续检查我在几个配置文件中定义的所有文件路径。

编辑编辑:通过修改我正在使用的 PhpStorm 配置解决了最后一个错误。您必须将其设置为“使用备用配置文件”并将测试范围设置为“在配置文件中定义”。

有帮助吗?

解决方案

发生这种情况的原因实际上并不是因为您发布的代码本身,而是因为您配置 PHPUnit 的方式。短代码回调存储在全局 ($shortcode_tags)。当您加载 WP 时,短代码将被注册并添加到全局中。你打电话 shortcode_exists(), ,它检查 $shortcode_tags, ,并返回 true. 。然后 PHPUnit 开始运行您的测试。它清除了全局变量范围。所以,当你的测试运行时, $shortcode_tags 不再存在,所以 shortcode_exists() 报告您的短代码未注册。(然后 PHPUnit 会将全局作用域恢复到测试之前的状态,以便测试不会影响全局变量。跑步 shortcode_exists() 测试完成后会报告 true.)

PHPUnit 确实有 backupGlobals 设置,并将其设置为 false 将阻止 PHPUnit 触及全局范围。这应该可以解决你的问题。然而....

更优秀的方式

作为作者 本教程, ,我想建议您像我在那里展示的那样设置您的测试。原因是您不必编写所有这些引导程序即可让 PHPUnit 与 WordPress 一起使用。您应该只使用 WordPress 的 PHPUnit 引导程序,显然,它已经被设计为可以与 WordPress 很好地配合。:-)

许可以下: CC-BY-SA归因
scroll top