sebkay/touchstone

一个用于运行WordPress单元和集成测试的简单框架。

4.0.0 2023-10-04 10:10 UTC

README

PHP

这是一个围绕官方WordPress测试套件的现代包装器。它可以用于运行单元测试和集成测试。

安装

运行以下命令以在项目中安装Touchstone:

composer require sebkay/touchstone --dev

用法

1.) 设置

运行设置过程会下载并安装WordPress和官方WordPress测试文件到您的临时目录。

# Options
./vendor/bin/touchstone setup --db-host=[HOST] --db-socket=[SOCKET PATH] --db-name=[DATABASE NAME] --db-user=[DATABASE USERNAME] --db-pass=[DATABASE PASSWORD] skip-db-creation

常规连接

# Example
./vendor/bin/touchstone setup --db-host=127.0.0.1:8889 --db-name=touchstone_tests --db-user=root --db-pass=root

通过套接字

./vendor/bin/touchstone setup --db-host=127.0.0.1:8889 --db-socket="/path/to/mysqld.sock" --db-name=touchstone_tests --db-user=root --db-pass=root

2.) 创建测试

所有测试都需要从项目根目录以下结构开始

tests/
    Unit/
        ExampleUnitTest.php
    Integration/
        ExampleIntegrationTest.php

所有单元测试都需要扩展WPTS\Tests\UnitTest类,所有集成测试都需要扩展WPTS\Tests\IntegrationTest类。

以下是一个单元测试的示例

<?php

namespace WPTS\Tests\Unit;

class ExampleUnitTest extends UnitTest
{
    public function test_it_works()
    {
        $this->assertTrue(true);
    }
}

以下是一个集成测试的示例

<?php

namespace WPTS\Tests\Integration;

class ExampleIntegrationTest extends IntegrationTest
{
    public function test_post_title_was_added()
    {
        $post_id = $this->factory()->post->create([
            'post_title' => 'Example post title',
        ]);

        $post = \get_post($post_id);

        $this->assertSame('Example post title', $post->post_title);
    }
}

3.) 运行测试

您可以使用以下命令运行所有测试或单个测试套件

# Run all tests
./vendor/bin/touchstone test

# Run Unit tests
./vendor/bin/touchstone test --type=unit

# Run Integration tests
./vendor/bin/touchstone test --type=integration

4.) 配置

您可以通过在项目根目录创建一个config.touchstone.php文件来配置某些设置。

测试目录

以下是设置测试所在目录的方法

<?php
# config.touchstone.php

return [
    'directories' => [
        'all'         => 'tests',
        'unit'        => 'tests/Unit',
        'integration' => 'tests/Integration',
    ],
];

WordPress插件

以下是加载在每次测试之前加载的插件的方法

这意味着对于像ACF(高级自定义字段)这样的插件,您可以在代码中使用get_field()等函数,而不会破坏您的测试。

重要:您需要提供插件文件。我建议将它们全部放在主题/插件的bin/plugins/中,并将该路径添加到您的.gitignore中。

<?php
# config.touchstone.php

return [
    'plugins' => [
        [
            'name' => 'Advanced Custom Fields',
            'file' => dirname(__FILE__) . '/bin/plugins/advanced-custom-fields-pro/acf.php',
        ],
    ],
];
# Directories
bin/plugins

WordPress主题

以下是加载每个测试都启用的主题的方法

<?php
# config.touchstone.php

return [
    'theme' => [
        'root' => dirname(__FILE__) . '/../../themes/twentytwentyone',
    ],
];

Composer脚本

您可以创建Composer脚本,这样就不需要记住上述命令。

要这样做,请将以下内容添加到您的composer.json文件中:

...
    "scripts": {
        "touchstone:setup": "./vendor/bin/touchstone setup --db-host=[HOST] --db-name=[DATABASE NAME] --db-user=[DATABASE USER] --db-pass=[DATABASE PASSWORD] --skip-db-creation=true",
        "touchstone:test": "./vendor/bin/touchstone test",
        "touchstone:unit": "./vendor/bin/touchstone test --type=unit",
        "touchstone:integration": "./vendor/bin/touchstone test --type=integration"
    }
...

然后您可以从命令行运行以下命令:

# Run setup
composer touchstone:setup

# Run all tests
composer touchstone:test

# Run Unit tests
composer touchstone:unit

# Run Integration tests
composer touchstone:integration

故障排除

测试无法运行

如果您在运行测试时遇到任何问题,请运行setup命令。您很可能会在最后一次运行测试后重启了计算机,这会删除WordPress测试文件。重新运行设置过程通常会解决问题。

为什么会有这个存在呢?

运行WordPress测试套件的官方方式非常复杂,并且非常容易出错。

Touchstone通过简化创建和运行测试的过程来解决这个问题。