Installation Add the bundle in development mode:
composer require antonio/aitestbundle --dev
Ensure your Symfony project is on PHP 8.2+ and Symfony 7.0+.
First Use Case
Generate and run tests for a controller (e.g., App\Controller\Api\UserController):
symfony console ai:test-controller App\Controller\Api\UserController
tests/Controller/ (or a configured path).Where to Look First
tests/Controller/ for the auto-generated test file (e.g., UserControllerTest.php).config/packages/aitestbundle.yaml (if needed).--verbose flag for debugging:
symfony console ai:test-controller --verbose App\Controller\Api\UserController
Incremental Testing
symfony console ai:test-controller App\Controller\ProductController
App\Controller\Api):
symfony console ai:test-controller App\Controller\Api
Note: Requires --batch flag (if supported in future versions).Integration with CI/CD
phpunit.xml.dist to run auto-generated tests alongside manual tests:
<testsuites>
<testsuite name="Controller Tests">
<directory>./tests/Controller</directory>
</testsuite>
</testsuite>
- run: symfony console ai:test-controller App\Controller
Custom Test Templates
templates/test.twig (if supported).// In a custom template (e.g., config/aitestbundle/test_template.php)
public function assertJwtTokenExists(Response $response) {
$this->assertStringContainsString('Bearer ', $response->headers->get('Authorization'));
}
Mocking Dependencies
// In UserControllerTest.php (manually edited)
protected function setUp(): void {
$this->mockService = $this->createMock(UserService::class);
$this->controller->setUserService($this->mockService);
}
assertDatabaseHas()).KernelEvents::REQUEST).False Positives in Assertions
# config/packages/aitestbundle.yaml
ai_test_bundle:
default_status: 201 # For POST routes
assertEquals(200, $response->getStatusCode())).Dynamic Routes
/users/{id}).->withParameter():
// In generated test
$this->client->request('GET', '/users/1');
Service Container Issues
setUp() (see Implementation Patterns).Environment-Specific Config
symfony console ai:test-controller --env=dev App\Controller\DashboardController
Verbose Mode: Reveal hidden errors:
symfony console ai:test-controller --verbose App\Controller\PaymentController
Dry Run: Generate tests without executing them:
symfony console ai:test-controller --generate-only App\Controller\ReportController
Output: Test file path printed to console.
Log Generation: Enable Symfony’s profiler to inspect test execution:
symfony console ai:test-controller --debug App\Controller\AuthController
Custom Assertions
// src/Test/AiTestBundle/Extension/CustomAssertions.php
namespace App\Test\AiTestBundle\Extension;
use PHPUnit\Framework\Assert;
class CustomAssertions {
public static function assertValidPagination(Response $response) {
$data = json_decode($response->getContent(), true);
Assert::assertArrayHasKey('data', $data);
Assert::assertArrayHasKey('meta', $data);
}
}
TestGenerator service to include your assertions.Pre/Post-Test Hooks
# config/services.yaml
services:
App\EventListener\AiTestListener:
tags:
- { name: kernel.event_listener, event: ai_test.pre_generate, method: onPreGenerate }
public function onPreGenerate(PreGenerateTestEvent $event) {
$this->container->get('cache_clearer')->clear();
}
Exclude Controllers
ai_test_bundle:
excluded_controllers:
- App\Controller\LegacyController
- App\Command\*
tests/Controller/. Change via:
ai_test_bundle:
test_namespace: tests\Functional
phpunit.xml.dist by default. Override with:
ai_test_bundle:
phpunit_config: phpunit.custom.xml
--parallel flag manually.How can I help you explore Laravel packages today?