我一直在努力让PHPUnit与WordPress配合良好(在this, this, this, this, 和this), 我想我已经非常接近了。我在测试之前直接运行的代码如下所示(基本上从this article):
( 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配置解决的。您必须将其设置为“使用备用配置文件”,并将测试范围设置为“在配置文件中定义”