Testing
Testing your WP-CLI commands with WPZylos WP-CLI.
Unit Testing
Testing Command Logic
use PHPUnit\Framework\TestCase;
class GreetCommandTest extends TestCase
{
public function testGreetsWithProvidedName(): void
{
$output = new TestOutput();
$command = new GreetCommand($output);
$command(['John'], []);
$this->assertStringContainsString('Hello, John', $output->getMessages());
}
public function testGreetsWorldByDefault(): void
{
$output = new TestOutput();
$command = new GreetCommand($output);
$command([], []);
$this->assertStringContainsString('Hello, World', $output->getMessages());
}
}
Mock Output Class
class TestOutput
{
private array $messages = [];
public function success(string $message): void
{
$this->messages[] = ['type' => 'success', 'message' => $message];
}
public function error(string $message): void
{
$this->messages[] = ['type' => 'error', 'message' => $message];
}
public function getMessages(): string
{
return implode("\n", array_column($this->messages, 'message'));
}
}
Integration Testing
Test with actual WP-CLI:
# Test migration command
wp myplugin migrate --dry-run
# Test with specific arguments
wp myplugin export users --output=/tmp/users.csv
Running Tests
composer run test