Testing
This guide covers testing the CLI Devtool commands.
Running Package Tests
# Run all tests
composer test
# Run specific test
./vendor/bin/phpunit tests/Commands/MakeControllerCommandTest.php
# Run with coverage
./vendor/bin/phpunit --coverage-html coverage/
Testing Command Output
<?php
namespace WPZylos\Framework\Cli\DevTool\Tests\Commands;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use WPZylos\Framework\Cli\DevTool\Commands\MakeControllerCommand;
class MakeControllerCommandTest extends TestCase
{
private string $tempDir;
protected function setUp(): void
{
$this->tempDir = sys_get_temp_dir() . '/wpzylos_test_' . uniqid();
mkdir($this->tempDir . '/app/Http/Controllers', 0755, true);
}
public function testExecuteCreatesController(): void
{
$application = new Application();
$application->add(new MakeControllerCommand());
$command = $application->find('make:controller');
$tester = new CommandTester($command);
$tester->execute([
'name' => 'TestController',
'--path' => $this->tempDir,
]);
$this->assertFileExists(
$this->tempDir . '/app/Http/Controllers/TestController.php'
);
}
public function testExecuteWithResourceOption(): void
{
$application = new Application();
$application->add(new MakeControllerCommand());
$command = $application->find('make:controller');
$tester = new CommandTester($command);
$tester->execute([
'name' => 'ResourceController',
'--resource' => true,
'--path' => $this->tempDir,
]);
$content = file_get_contents(
$this->tempDir . '/app/Http/Controllers/ResourceController.php'
);
$this->assertStringContainsString('public function index', $content);
$this->assertStringContainsString('public function store', $content);
$this->assertStringContainsString('public function destroy', $content);
}
}
Testing File Generation
<?php
public function testGeneratedFileHasCorrectNamespace(): void
{
// ... setup ...
$tester->execute(['name' => 'ProductController']);
$content = file_get_contents($outputPath);
$this->assertStringContainsString(
'namespace MyPlugin\\Http\\Controllers;',
$content
);
}
public function testGeneratedFileHasCorrectClassName(): void
{
// ... setup ...
$tester->execute(['name' => 'ProductController']);
$content = file_get_contents($outputPath);
$this->assertStringContainsString(
'class ProductController',
$content
);
}
Integration Testing
Test the full CLI workflow:
<?php
public function testFullWorkflow(): void
{
$application = new Application();
// Add all commands
$application->add(new MakePluginCommand());
$application->add(new MakeControllerCommand());
// Create plugin
$tester = new CommandTester($application->find('make:plugin'));
$tester->execute(['name' => 'test-plugin']);
// Generate controller in plugin
$tester = new CommandTester($application->find('make:controller'));
$tester->execute([
'name' => 'ProductController',
'--path' => $this->tempDir . '/test-plugin',
]);
$this->assertFileExists(
$this->tempDir . '/test-plugin/app/Http/Controllers/ProductController.php'
);
}
Related
- API Reference — Command reference
- wpzylos.com — Full documentation