jdgrimes/wp-plugin-uninstall-tester

此软件包已被弃用且不再维护。作者建议使用 jdgrimes/wpppb 软件包。

用于测试 WordPress 插件安装/卸载的 PHPUnit 工具

0.6.0 2016-06-30 19:28 UTC

This package is auto-updated.

Last update: 2022-02-01 12:39:07 UTC


README

用于测试插件安装和卸载的测试用例类,附带相关工具。

注意

该项目已不再维护。它已被合并到 WP Plugin PHPUnit Bootstrap 中。建议您使用该软件包。

背景

此测试用例的目的是让您将插件卸载测试尽可能做得现实。WordPress 在插件未激活时卸载插件,这些工具允许您模拟这一点。安装是远程执行的,因此在测试运行时插件不会被加载。

我在发现我的插件中的一个卸载脚本存在致命错误后创建了这些工具。我不是说我没有为卸载编写单元测试。我确实编写了。但是卸载测试是在插件已经加载的情况下运行的。因此,我从未意识到我在调用一个通常不可用的插件函数。那时我决定创建这些测试工具,这样我的卸载测试就会失败,如果我没有在我的插件卸载脚本中包含所有必需的依赖项。

除了提供现实主义的卸载测试环境外,它还提供了一些断言,可以帮助您确保您的插件彻底清理了数据库。

安装

Composer

composer require --dev jdgrimes/wp-plugin-uninstall-tester

设置

一旦安装了测试工具,您需要修改一下PHPUnit的引导文件。我们假设您有一个类似于这篇教程中的引导文件。

首先,您需要包含 /includes/functions.php 文件,这样您就可以使用 running_wp_plugin_uninstall_tests() 检查是否正在运行卸载测试。您需要确保只有在卸载测试不运行时才加载您的插件文件。

/*
 * This needs to go after you include WordPress's unit test functions, but before
 * loading WordPress's bootstrap.php file.
 */

// Include the uninstall test tools functions.
include_once dirname( __FILE__ ) . '/../../../vendor/jdgrimes/wp-plugin-uninstall-tester/includes/functions.php';

// Check if the tests are running. Only load the plugin if they aren't.
if ( ! running_wp_plugin_uninstall_tests() ) {
	tests_add_filter( 'muplugins_loaded', 'my_plugin_activate' );
}

其次,您需要包含 bootstrap.php 文件

/*
 * This needs to be included after loading WordPress's bootstrap.php, because the
 * uninstall testcase extends WordPress's WP_UnitTestCase class.
 */

include_once dirname( __FILE__ ) . '/../../../vendor/jdgrimes/wp-plugin-uninstall-tester/bootstrap.php';

第三,您需要从您的PHPUnit XML配置文件中的测试中排除 uninstall

	<!-- This needs to go inside of the <phpunit></phpunit> tags -->
	<groups>
		<exclude>
			<group>uninstall</group>
		</exclude>
	</groups>

这将默认排除卸载测试的运行。要运行它们,您需要执行 phpunit --group=uninstall

最后,您需要确保您已经将或创建了您的插件到测试站点的插件文件夹中(如果您还没有在那里开发它)

ln -s path/to/my-plugin path/to/trunk/src/wp-content/plugins/my-plugin

用法

现在,终于到了创建测试用例的时候了。为此,扩展 WP_Plugin_Uninstall_UnitTestCase 类。

<?php

/**
 * Test uninstallation.
 */

/**
 * Plugin uninstall test case.
 *
 * Be sure to add "@group uninstall", so that the test will run only as part of the
 * uninstall group.
 *
 * @group uninstall
 */
class My_Plugin_Uninstall_Test extends WP_Plugin_Uninstall_UnitTestCase {

	//
	// Protected properties.
	//

	/**
	 * The full path to the main plugin file.
	 *
	 * @type string $plugin_file
	 */
	protected $plugin_file;

	//
	// Public methods.
	//

	/**
	 * Set up for the tests.
	 */
	public function setUp() {

		// You must set the path to your plugin here.
		// This should be the path relative to the plugin directory on the test site.
		// You will need to copy or symlink your plugin's folder there if it isn't
		// already.
		$this->plugin_file = 'my-plugin/my-plugin.php';

		// Don't forget to call the parent's setUp(), or the plugin won't get installed.
		parent::setUp();
	}

	/**
	 * Test installation and uninstallation.
	 */
	public function test_uninstall() {

		global $wpdb;
		
		/*
		 * First test that the plugin installed itself properly.
		 */

		// Check that a database table was added.
		$this->assertTableExists( $wpdb->prefix . 'myplugin_table' );

		// Check that an option was added to the database.
		$this->assertEquals( 'default', get_option( 'myplugin_option' ) );

		/*
		 * Now, test that it uninstalls itself properly.
		 */

		// You must call this to perform uninstallation.
		$this->uninstall();

		// Check that the table was deleted.
		$this->assertTableNotExists( $wpdb->prefix . 'myplugin_table' );

		// Check that all options with a prefix was deleted.
		$this->assertNoOptionsWithPrefix( 'myplugin' );

		// Same for usermeta and comment meta.
		$this->assertNoUserMetaWithPrefix( 'myplugin' );
		$this->assertNoCommentMetaWithPrefix( 'myplugin' );
	}
}

保存您的测试用例,您就设置好了!

插件使用模拟

上述示例是测试您的插件是否完全卸载的绝佳第一步。然而,您可能做得更好。上述测试用例仅测试从您插件的新鲜、干净安装中卸载。但用户实际使用您的插件一段时间后呢?它可能已经在数据库的某个地方添加了一些更多的选项。为了进行更稳健和完整的卸载测试,需要模拟插件的使用。

测试用例为此提供了支持。要使用此功能,编写一个脚本以模拟您的插件使用。调用您添加数据的各种函数,例如。将您的代码保存到文件中。

现在,为了使测试用例运行模拟,您只需要在 $simulation_file 类属性中指定您刚刚创建的文件的路径(与上面我们用于主插件文件和 $plugin_file 属性的方式相同)。

现在,在卸载插件之前,将运行插件使用模拟脚本。如果需要,您也可以通过调用 $this->simulate_usage() 在此之前运行它。

许可证

此库在 MIT 许可证下提供。