wpsyntex/wp-phpunit

WordPress 的 PHPUnit 库

dev-master 2024-03-13 15:14 UTC

This package is auto-updated.

Last update: 2024-09-21 18:56:26 UTC


README

为 WP Syntex 项目提供的代码库,包含

  • 安装 WordPress 测试套件的脚本,
  • 安装所需插件和主题的脚本,
  • 构建和分发项目的脚本,
  • 引导单元和集成测试的脚本,
  • 测试辅助工具。

如何使用

安装测试套件

测试套件安装在你的项目 tmp 文件夹中。

为了告诉安装脚本如何连接到你的数据库,你可以在项目根目录创建一个名为 DB-CONFIG 的文件,格式如下(当然,该文件不与 git 版本控制同步)。
每行都是可选的,默认值如下

db_host: localhost
db_name: wordpress_tests
db_user: root
db_pass: '' (an empty string)

安装依赖项

插件和主题安装在你的项目 tmp 文件夹中。

创建一个 install-plugins.sh 文件来启动所有下载。例如

#!/usr/bin/env bash

. "$PWD/vendor/wpsyntex/wp-phpunit/bin/wp-download-tools.sh"

mkdir -p $WP_PLUGINS_DIR
mkdir -p $WP_THEMES_DIR

# Install WP All Import Pro.
downloadPluginFromEdd wp-all-import-pro 'WP All Import' https://www.wpallimport.com

# Install Polylang Pro.
downloadPolylangPro

# Install Polylang for WooCommerce.
downloadPolylangForWoocommerce

# Install WooCommerce.
downloadWoocommerce

# Install TwentyFourteen.
downloadThemeFromRepository twentyfourteen

注意:两个变量可供您使用:$WP_PLUGINS_DIR$WP_THEMES_DIR。它们分别包含插件和主题的安装路径,即 tmp/pluginstmp/themes。默认情况下,这些文件夹不会创建,所以不要忘记像前面的例子一样使用 mkdir -p $WP_PLUGINS_DIR 和/或 mkdir -p $WP_THEMES_DIR,然后再使用下载函数。

从 EDD 安装的付费插件需要更多的关注,因为它们需要一个许可证密钥。您可以通过在项目根目录创建一个名为 LICENSE-CODES 的文件并提供格式如下(当然,该文件不与 git 版本控制同步)来提供它。

license {PLUGIN-SLUG}:{YOUR-LICENSE}
site {PLUGIN-SLUG}:{YOUR-SITE}

根据 EDD 配置,可能不需要 site 行。
此外,如果你的 EDD 插件不需要许可证密钥,请按照以下步骤操作

license {PLUGIN-SLUG}:none

Composer 脚本

然后你可以创建像以下这样的 composer 脚本

{
    "scripts": {
        "install-suite": "vendor/wpsyntex/wp-phpunit/bin/install-wp-suite.sh",
        "install-suite-with-db": "vendor/wpsyntex/wp-phpunit/bin/install-wp-suite.sh latest true",
        "install-plugins": "Tests/bin/install-plugins.sh",
        "install-tests": [
            "@install-suite",
            "@install-plugins"
        ],
        "install-tests-with-db": [
            "@install-suite-with-db",
            "@install-plugins"
        ],
        "build": "vendor/wpsyntex/wp-phpunit/bin/build.sh",
        "build-update": "vendor/wpsyntex/wp-phpunit/bin/build.sh -- -u",
        "dist": "vendor/wpsyntex/wp-phpunit/bin/distribute.sh -- polylang-foobar"
    },
    "scripts-descriptions": {
        "install-suite": "Installs the WordPress tests suite (without installing the database).",
        "install-suite-with-db": "Installs the WordPress tests suite (with database creation).",
        "install-plugins": "Installs dependencies needed for integration tests.",
        "install-tests": "Installs both the WordPress tests suite (without installing the database) and the dependencies needed for integration tests.",
        "install-tests-with-db": "Installs both the WordPress tests suite (with database creation) and the dependencies needed for integration tests, without creating the database.",
        "build": "Builds the project.",
        "build-update": "Builds the project (runs `composer update` instead of `composer install`).",
        "dist": "Makes the zip file to distibute the project release."
    }
}

集成测试

集成测试引导

你的 bootstrap.php 文件示例

<?php
/**
 * Bootstraps the Polylang Foobar integration tests
 * php version 7.0
 *
 * @package WP_Syntex\Polylang_Foobar\Tests\Integration
 */

namespace WP_Syntex\Polylang_Foobar\Tests\Integration;

use function WP_Syntex\Polylang_Phpunit\Integration\bootstrapSuite;

require dirname( dirname( __DIR__ ) ) . '/vendor/wpsyntex/wp-phpunit/UnitTests/Integration/bootstrap.php';

bootstrapSuite(
    dirname( __DIR__ ), // Path to the directory containing all tests.
    '7.0.0', // The PHP version required to run this test suite.
    [
        'plugins' => [
            'polylang-pro/polylang.php'                            => true,
            'woocommerce/woocommerce.php'                          => [
                'group' => 'WithWoo',
                'init'  => '\WP_Syntex\Polylang_Phpunit\Integration\WooCommerce\Bootstrap::initWoocommerce',
            ],
            'polylang-wc/polylang-wc.php'                          => [
                'group' => 'WithWoo',
            ],
            dirname( dirname( __DIR__ ) ) . '/polylang-foobar.php' => true,
        ], // A list of plugins to include and activate.
    ]
);

上面的代码将

  • 需要 polylang.phppolylang-foobar.php
  • 如果使用 --group=WithWoo 时调用 phpunit,则还需要 woocommerce.phppolylang-wc.php
  • woocommerce.php 被需要后调用 \WP_Syntex\Polylang_Phpunit\Integration\WooCommerce\Bootstrap::initWoocommerce()

在你的集成测试中使用 trait

你可以在你的集成测试中使用 WP_Syntex\Polylang_Phpunit\Integration\TestCaseTrait trait。

提示:如果你需要创建自己的方法 set_up()wpSetUpBeforeClass 等,你不能像使用父/子类一样简单地使用 parent:: 来调用 trait 中的方法。在这种情况下,你可以这样做

<?php
/**
 * Test Case for all of the integration tests.
 * php version 7.0
 *
 * @package WP_Syntex\Polylang_Foobar\Tests\Integration
 */

namespace WP_Syntex\Polylang_Foobar\Tests\Integration;

use WP_Syntex\Polylang_Phpunit\Integration\TestCaseTrait;
use WP_UnitTest_Factory;
use WP_UnitTestCase;

abstract class AbstractTestCase extends WP_UnitTestCase {
    use TestCaseTrait {
        wpSetUpBeforeClass as private traitWpSetUpBeforeClass;
        set_up as private traitSetUp;
    }

    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
        self::traitWpSetUpBeforeClass( $factory );
        // Do things.
    }

    public function set_up() {
        $this->traitSetUp();
        // Do more things.
    }
}

如果你需要在你的集成测试中列出一些插件作为“活动插件”(get_option( 'active_plugins' ))之一,你可以创建一个类似下面的类在你的测试中

<?php
/**
 * Test Case for all of the integration tests.
 * php version 7.0
 *
 * @package WP_Syntex\Polylang_Foobar\Tests\Integration
 */

namespace WP_Syntex\Polylang_Foobar\Tests\Integration;

use WP_Syntex\Polylang_Phpunit\Integration\TestCaseTrait;
use WP_UnitTestCase;

/**
 * Test Case for all of the integration tests.
 */
abstract class AbstractTestCase extends WP_UnitTestCase {
    use TestCaseTrait;

    /**
     * List of active plugins.
     *
     * @var array<string>
     */
    protected $activePlugins = [
        'wp-all-import-pro/wp-all-import-pro.php',
    ];
}

你的测试中有一些辅助工具可用,如与反射交互或获取测试数据。你可以在 TestCaseTrait trait 中找到它们。

单元测试

单元测试引导

你的 bootstrap.php 文件示例

<?php
/**
 * Bootstraps the Polylang Foobar Unit Tests.
 * php version 7.0
 *
 * @package WP_Syntex\Polylang_Foobar\Tests\Unit
 */

namespace WP_Syntex\Polylang_Foobar\Tests\Unit;

use function WP_Syntex\Polylang_Phpunit\Unit\bootstrapSuite;

require dirname( dirname( __DIR__ ) ) . '/vendor/wpsyntex/wp-phpunit/UnitTests/Unit/bootstrap.php';

bootstrapSuite( dirname( __DIR__ ), '7.0.0' );

在你的单元测试中扩展抽象类

你可以在你的单元测试中扩展 WP_Syntex\Polylang_Phpunit\Unit\AbstractTestCase 抽象类。

Brain\Monkey 在你的单元测试中可用,因为它包含在 Yoast\WPTestUtils 中。

在您的测试中,您可以通过设置两个自定义属性来模拟WordPress中最常见的函数。并且,与集成测试一样,在您的测试中,有一些辅助工具可用,来自TestCaseTrait特性。

<?php
/**
 * Tests for `WP_Syntex\Polylang_Foobar\barbaz()`.
 * php version 7.0
 *
 * @package WP_Syntex\Polylang_Foobar\Tests\Unit
 */

namespace WP_Syntex\Polylang_Foobar\Tests\Unit;

use WP_Syntex\Polylang_Phpunit\Unit\AbstractTestCase;

/**
 * Tests for `WP_Syntex\Polylang_Foobar\barbaz()`.
 */
class Barbaz extends AbstractTestCase {

    /**
     * Stubs the WP native translation functions in the set_up().
     *
     * @var bool
     */
    protected static $stubTranslationFunctions = true;

    /**
     * Stubs the WP native escaping functions in the set_up().
     *
     * @var bool
     */
    protected static $stubEscapeFunctions = true;
}