定制 sebkay/touchstone 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sebkay/touchstone

Composer 安装命令:

composer require sebkay/touchstone

包简介

A simple framework for running WordPress unit and integration tests.

README 文档

README

PHP

A modern wrapper around the official WordPress testsuite. It can be used to run both Unit and Integration tests.

Installation

Run the following command to install Touchstone in your project:

composer require sebkay/touchstone --dev

Usage

1.) Setup

Running the setup process downloads and installs both WordPress and the official WordPress test files in your temp directory.

# 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

Regular Connection

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

via a Socket

./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.) Creating Tests

All your tests will need to be in the following structure from the root of your project:

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

All your Unit tests will need to extend the WPTS\Tests\UnitTest class and all your integrationt tests will need to extend the WPTS\Tests\IntegrationTest class.

Here's an example Unit test:

<?php

namespace WPTS\Tests\Unit;

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

Here's an example Integration test:

<?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.) Running Tests

You can run either all of your tests or a single testsuite with the following commands:

# 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.) Configuration

You can configure certain things by creating a config.touchstone.php file in the root of your project.

Directories For Tests

Here's how to set the directories for where your tests are located:

<?php
# config.touchstone.php

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

WordPress Plugins

Here's how to load plugins which are loaded before each test.

This means for a plugin like ACF (Advanced Custom Fields) you can use functions like get_field() in your code and it won't break your tests.

Important: You will need to provide the plugin files. I recommend putting them all in bin/plugins/ in your theme/plugin and the adding that path to your .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 Theme

Here's how to load a theme which is active for each test.

<?php
# config.touchstone.php

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

Composer Scripts

You can create Composer scripts so you don't need to memorise the above commands.

To do so add the following to your composer.json file:

...
    "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"
    }
...

Then you can run the following from the command line:

# Run setup
composer touchstone:setup

# Run all tests
composer touchstone:test

# Run Unit tests
composer touchstone:unit

# Run Integration tests
composer touchstone:integration

Troubleshooting

Tests Won't Run

If you ever have problems running your tests, run the setup command. It's more than likely you've restarted your machine since the last time you ran the tests which deletes the WordPress test files. Re-running the setup process will usually fix the problem.

Why Does This Exist?

The official way of running the WordPress testsuite is horribly complicated and incredibly prone to user error.

Touchstone fixes both of those issues by making the process of creating and running tests easy.

sebkay/touchstone 适用场景与选型建议

sebkay/touchstone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 566 次下载、GitHub Stars 达 19, 最近一次更新时间为 2021 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sebkay/touchstone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sebkay/touchstone 我们能提供哪些服务?
定制开发 / 二次开发

基于 sebkay/touchstone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 566
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-25