sebkay/touchstone
一个用于运行WordPress单元和集成测试的简单框架。
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.3
- league/flysystem: ^2.3
- php-stubs/wordpress-stubs: ^5.8
- phpunit/phpunit: ^10.2
- sebkay/wp-unit-test-stubs: ^1.0
- symfony/console: ^5.3
- symfony/process: ^5.3
- vlucas/phpdotenv: ^5.5
- wp-cli/wp-config-transformer: ^1.2
- yoast/phpunit-polyfills: ^2.0
Requires (Dev)
- spatie/ray: ^1.26
- squizlabs/php_codesniffer: ^3.6
- vimeo/psalm: ^5.14
README
这是一个围绕官方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通过简化创建和运行测试的过程来解决这个问题。