wp-cli/wp-cli-tests
WP-CLI testing framework for WP-CLI packages. Adds Composer scripts and tooling to run PHPUnit, Behat, PHPCS, and linting with optional cross-platform Behat config and custom PHPCS rulesets for consistent CI-ready testing.
While wp-cli/wp-cli-tests is primarily designed for WordPress CLI plugins/themes, Laravel developers can leverage it for testing WordPress integrations (e.g., custom WP-CLI commands, Laravel-WP bridges, or headless WordPress APIs). Here’s how to start:
Install the Package Add as a dev dependency in a Laravel project with WordPress integration:
composer require --dev wp-cli/wp-cli-tests
Configure composer.json
Add test scripts (adjust based on needs):
"scripts": {
"test:wp": [
"@test:wp:unit",
"@test:wp:behat"
],
"test:wp:unit": "vendor/bin/phpunit --testdox-html tests/Unit",
"test:wp:behat": "vendor/bin/behat --config tests/Behat/behat.yml"
}
First Use Case: Testing a Custom WP-CLI Command
tests/Behat/Feature/Example.feature file:
Feature: WP-CLI Command Test
Scenario: Run a custom command
Given I run the WP-CLI command "my:command --option=value"
Then the output should contain "Success"
composer test:wp
phpunit for pure PHP logic (e.g., service classes interacting with WP APIs).wp-cli-tests for WP-CLI command behavior (e.g., behat for CLI interactions).Artisan::call()).Example Workflow:
// Laravel Controller
public function triggerWpCliCommand() {
Artisan::call('wp my:command --option=value');
return response()->json(['status' => 'success']);
}
Test with Behat:
Scenario: Laravel route triggers WP-CLI command
Given I call the Laravel endpoint "/wp-cli-trigger"
Then the WP-CLI command "my:command" was executed
WP_VERSION=5.8 composer test:wp).WP_CLI_TEST_DBTYPE=sqlite composer test:wp:behat
# .github/workflows/test.yml
jobs:
test-wp:
runs-on: ubuntu-latest
steps:
- run: composer test:wp:unit
- run: WP_VERSION=trunk composer test:wp:behat
Given I mock the HTTP request to "https://api.example.com"
with filename "tests/Behat/Data/api_response.json"
Database Setup
composer prepare-tests must run once per environment (e.g., CI job). Skipping it causes "database not found" errors.composer prepare-tests && composer test:wp
Windows Path Issues
FeatureContext:
// tests/Behat/Context/FeatureContext.php
public function __construct() {
$this->tempDir = str_replace('\\', '/', sys_get_temp_dir());
}
Object Cache Conflicts
@skip-object-cache tag may fail if the cache is enabled globally.behat.yml:
default:
extensions:
WP_CLI\Tests\Behat\ServiceContainer\WPCLIExtension:
object_cache: false
PHPUnit vs. Behat Conflicts
TestCommandTest.php for PHPUnit and test_command.feature for Behat).--verbose to see step-by-step execution:
composer test:wp:behat -- --verbose
--rerun to retry only failed tests:
composer test:wp:behat -- --rerun
wp binary is used (e.g., via WP_CLI_BIN_DIR):
WP_CLI_BIN_DIR=/path/to/custom/wp composer test:wp
Custom Behat Contexts
Extend FeatureContext for Laravel-specific steps:
// tests/Behat/Context/LaravelContext.php
use Behat\Behat\Context\Context;
use Illuminate\Support\Facades\Artisan;
class LaravelContext implements Context {
/**
* @Given I call the Artisan command :command
*/
public function iCallArtisanCommand($command) {
Artisan::call($command);
}
}
Register in behat.yml:
default:
contexts:
- WP_CLI\Tests\Context\FeatureContext
- tests\Behat\Context\LaravelContext
Custom PHPCS Rules
Extend the WP_CLI_CS ruleset in phpcs.xml.dist to enforce Laravel-specific standards (e.g., Facade usage):
<rule ref="WP_CLI_CS">
<exclude name="Generic.Files.LineEndings"/>
<exclude name="Generic.Files.EndFileNewline"/>
</rule>
Parallel Test Execution Speed up Behat tests by running suites in parallel (requires Behat 3.6+):
composer test:wp:behat -- --parallel
RefreshDatabase trait with wp-cli-tests’ database setup:
use Illuminate\Foundation\Testing\RefreshDatabase;
use WP_CLI\Tests\TestCase;
class MyTest extends TestCase {
use RefreshDatabase;
}
@slow and filter them out in CI:
composer test:wp:behat -- --tags="~@slow"
docker-compose up -d wordpress
WP_CLI_TEST_DBHOST=mysql composer prepare-tests
---
```markdown
## Laravel-Specific Notes
- **Service Provider Testing**: Mock Laravel service providers in Behat using `INVOKE_WP_CLI_WITH_PHP_ARGS`:
```gherkin
Given I run the WP-CLI command "wp my:command"
with PHP arguments:
"""
$_ENV['APP_ENV'] = 'testing';
$_SERVER['KERNEL'] = new \App\Providers\AppServiceProvider();
"""
Given I am logged in as a user with role "administrator"
How can I help you explore Laravel packages today?