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.
To leverage wp-cli/wp-cli-tests in a Laravel-based WordPress plugin/theme project:
Install the package (add to composer.json under require-dev):
composer require --dev wp-cli/wp-cli-tests
Configure Composer scripts (add to composer.json):
"scripts": {
"test": [
"@phpunit",
"@behat"
],
"phpunit": "vendor/bin/phpunit",
"behat": "vendor/bin/behat --config vendor/wp-cli/wp-cli-tests/behat.yml"
}
Set up a Laravel-specific Behat config (behat.yml):
default:
suites:
default:
contexts:
- WP_CLI\Tests\Context\FeatureContext
- App\Tests\Behat\WordPressContext # Custom Laravel context
paths:
- tests/behat/features
Run tests:
composer test
Create a Behat feature file (tests/behat/features/cli_command.feature):
Feature: Test Laravel-WP CLI command
Scenario: Execute a custom WP-CLI command
Given I run the WP-CLI command "wp my-laravel-command"
Then the output should contain "Success"
graph TD
A[Laravel Test] --> B[WP-CLI Test Harness]
B --> C[Behat Feature Files]
C --> D[PHPUnit Unit Tests]
D --> E[Laravel Service Container]
E --> F[WordPress Core]
Key Integration Points:
Service Provider: Extend WP_CLI\Tests\Bootstrap in a Laravel service provider:
public function boot()
{
$this->app->singleton('wp-cli.tests', function () {
return new WP_CLI\Tests\Bootstrap(
base_path('vendor/wp-cli/wp-cli-tests')
);
});
}
Custom Contexts: Create Laravel-specific Behat contexts:
namespace App\Tests\Behat;
use Behat\Behat\Context\Context;
use WP_CLI\Tests\Context\FeatureContext;
class WordPressContext extends FeatureContext implements Context
{
public function __construct()
{
parent::__construct();
$this->useWordPressCore();
}
public function useWordPressCore()
{
// Laravel-specific WP core integration
}
}
// In a Behat test
public function setupDatabase()
{
$this->getDatabase()->createDatabase('wp_cli_test');
$this->getDatabase()->createUser('wp_cli_test', 'password1');
$this->getDatabase()->grantAllPrivileges('wp_cli_test', 'wp_cli_test');
// Laravel-specific DB setup
config(['database.connections.mysql.database' => 'wp_cli_test']);
}
// tests/Unit/Commands/MyCommandTest.php
public function testCommandExecution()
{
$command = new MyCommand();
$command->setLaravel($this->app);
$output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
->getMock();
$command->handle($output, ['option' => 'value']);
$this->assertStringContainsString('Expected output', $output->getContent());
}
Namespace Conflicts:
Artisan vs WP-CLI's wp command namingGiven I run the WP-CLI command "wp my-plugin:command"
Service Container Initialization:
$bootstrap->setLaravelApp($this->app);
Database Connection Issues:
.env may override test DB settingsWP_CLI_TEST_DBNAME=laravel_test DB_CONNECTION=mysql composer behat
Behat Verbose Mode:
composer behat -- -vvv
Laravel Log Integration:
// In Behat context
public function __construct()
{
$this->logger = $this->getLaravelApp()->make('log');
}
public function logMessage($message)
{
$this->logger->info($message);
}
CI-Specific Issues:
env:
MEMORY_LIMIT: -1
Custom Behat Steps:
// app/Tests/Behat/StepDefinition/LaravelSteps.php
use Behat\Behat\Tester\Exception\PendingException;
/**
* @Given /^I am authenticated as user "([^"]+)"$/
*/
public function iAmAuthenticatedAsUser($username)
{
$this->getLaravelApp()->make('auth')->loginUsingId(1);
}
PHPUnit Extensions:
// phpunit.xml
<extensions>
<extension class="WP_CLI\Tests\PHPUnit\WP_CLI_TestCase"/>
</extensions>
Custom Test Helpers:
// tests/TestHelper.php
if (!function_exists('createTestUser')) {
function createTestUser($role = 'administrator')
{
$user = wp_create_user('testuser', 'password123');
$role = get_role($role);
$role->add_user($user);
return $user;
}
}
Parallel Test Execution:
composer test -- --parallel
Database Caching:
# behat.yml
default:
extensions:
Behat\MinkExtension:
base_url: 'http://localhost'
goutte: ~
WP_CLI\Tests\Behat\Extension:
cache: true
Selective Test Running:
composer behat -- features/cli-command.feature
How can I help you explore Laravel packages today?