I have integration tests set up. The install-wp-tests.sh
is taken from the wpcli scaffold plugin tests. I'm on VVV and I have wordpress-develop
set up.
In my plugin I have phpunit.integration.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap-integration.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
syntaxCheck="false"
verbose="true"
>
<testsuites>
<testsuite name="integration">
<directory prefix="test-" suffix=".php">./tests/integration/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./node_modules</directory>
<directory>./vendor</directory>
<directory>./tests</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="tests/_reports/coverage-integration" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="80" />
<log type="coverage-clover" target="tests/_reports/logs/clover-integration.xml"/>
<log type="coverage-php" target="tests/_reports/logs/coverage-integration.cov"/>
<log type="junit" target="tests/_reports/logs/logfile-integration.xml"/>
</logging>
</phpunit>
In my composer.json
I have (currently not using some of the packages like wp_mock and brainMonkey)
{
"require-dev": {
"10up/wp_mock": "^0.3.0",
"brain/monkey": "^2.2",
"codedungeon/phpunit-result-printer": "^0.20.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5",
"infinum/coding-standards-wp": "*",
"php-mock/php-mock-mockery": "^1.3",
"phpunit/phpunit": "^6.5",
"roave/security-advisories": "dev-master"
},
"autoload": {
"classmap": [
"src/",
"views/",
"tests/"
]
},
"config": {
"sort-packages": true,
"optimize-autoloader": true,
"process-timeout": 2000
},
"scripts" : {
"check-cs": "@php ./vendor/bin/phpcs --extensions=php --ignore=/vendor/",
"fix-cs": "@php ./vendor/bin/phpcbf --extensions=php --ignore=/vendor/",
"punit": "@php ./vendor/bin/phpunit -c phpunit.xml.dist --no-coverage --colors=always",
"punit-cov": "@php ./vendor/bin/phpunit -c phpunit.xml.dist --colors=always",
"pint": "@php ./vendor/bin/phpunit -c phpunit.integration.xml.dist --no-coverage --colors=always",
"pint-cov": "@php ./vendor/bin/phpunit -c phpunit.integration.xml.dist --colors=always"
}
}
My boostrap-integration.php
file looks like this:
<?php
/**
* PHPUnit bootstrap file
*
* Integration tests bootstrap file.
*
* @package Developer_Portal
*/
$_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; // WPCS: XSS ok.
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() {
require dirname( __FILE__, 3 ) . '/advanced-custom-fields-pro/acf.php';
require dirname( __FILE__, 2 ) . '/my-plugin.php';
$plugins_to_activate = [
'advanced-custom-fields-pro/acf.php',
'my-plugin/my-plugin.php',
];
update_option( 'active_plugins', $plugins_to_activate );
}
tests_add_filter( 'plugins_loaded', '_manually_load_plugin' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
Here I've replaced the default (which I've also tested with)
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __FILE__, 3 ) . '/advanced-custom-fields-pro/acf.php';
require dirname( __FILE__, 2 ) . '/my-plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
My test looks like this
<?php
/**
* Class Admin Menu tests
*
* @package My_Plugin\Tests\Integration\Src\Admin_Menus
*/
use My_Plugin\Admin_Menus\Credentials;
use My_Plugin\Core\Service;
/**
* Class that tests the admin menu.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class Credentials_Menu_Integration_Test extends WP_UnitTestCase {
/**
* Test suite setUp method
*/
public function setUp() {
parent::setUp();
// This is needed to create a user. Internal implementation.
$_REQUEST['_wpnonce_create-user'] = wp_create_nonce( 'create-user' );
wp_set_current_user(
self::factory()->user->create(
[
'role' => 'administrator',
]
)
);
}
/**
* Test default template name
*/
public function test_apigee_menu_render() {
$cred_menu = new Credentials();
$cred_menu->register();
$cred_menu->process_admin_menu( [] );
$cred_menu->render();
$this->assertInstanceOf( 'My_Plugin\Core\Service', $cred_menu );
$this->assertNotEmpty( menu_page_url( Credentials::MENU_SLUG ) );
}
}
The code works when I look at the WordPress page. I can see the credentials menu and all, but this
$this->assertNotEmpty( menu_page_url( Credentials::MENU_SLUG ) );
fails.
It looks like in the tests the plugins are not active at all. The instance of test passes, the coverage shows that the public methods are passed during testing.
I just don't know what to think of this. The plugin files are required in the bootstrap, but it's like when I do integration tests the plugins are not actually loaded/activated in the test WordPress.
EDIT
The entire test suite is working. I just created tests that extended WP_Ajax_UnitTestCase
and I successfully tested my AJAX callback methods...
I'm not sure I understand why the above happens still.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745319691a4622385.html
评论列表(0条)