Installation:
composer require desarrolla2/test-bundle
Ensure your project uses Symfony 5.x/6.x (last release was 2023-02-07).
First Test Class:
Extend Desarrolla2\TestBundle\Functional\WebTestCase in your test class:
namespace App\Tests\Functional;
use Desarrolla2\TestBundle\Functional\WebTestCase;
class ExampleTest extends WebTestCase
{
public function testBasicRoute()
{
$client = $this->getClient();
$this->requestAndAssertOkAndHtml($client, 'GET', '/some-route');
}
}
Key Methods to Explore:
getClient(): Returns a Symfony Client instance.logIn($client, $email, $roles): Authenticates a user (requires getBackendRoles() helper).requestAndAssertOkAndHtml(): Asserts HTTP 200 + HTML response.requestGetAndPostAndAssertRedirect(): Tests form submissions with redirects.Where to Look First:
src/Functional/WebTestCase.php for all available helper methods.public function testProtectedRoute()
{
$client = $this->getClient();
$this->logIn($client, 'user@example.com', ['ROLE_ADMIN']);
$this->requestAndAssertOkAndHtml($client, 'GET', '_app.admin.dashboard');
}
getClient() and $user if testing multiple endpoints for the same user.public function testFormValidation()
{
$client = $this->getClient();
$this->logIn($client, 'user@example.com', []);
$this->requestGetAndPostAndAssertRedirect(
$client,
'_app.contact.submit',
['name' => 'Test User', 'email' => 'test@example.com']
);
}
requestGetAndPostAndAssertRedirect for forms that redirect on success.public function testApiEndpoint()
{
$client = $this->getClient();
$client->request('GET', '/api/endpoint', [
'headers' => ['Content-Type' => 'application/json']
]);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertJson($client->getResponse()->getContent());
}
Client methods for non-HTML/API responses.protected function setUp(): void
{
parent::setUp();
$this->loadFixtures([
'App\DataFixtures\UserFixture',
'App\DataFixtures\ProductFixture'
]);
}
DatabaseTestCase or DoctrineFixturesBundle for test data.protected function assertResponseContains($text)
{
$this->assertStringContainsString($text, $this->getClient()->getResponse()->getContent());
}
WebTestCase to add reusable assertions.Symfony Kernel:
getKernel() in your test class if using a custom kernel:
protected static function getKernelClass(): string
{
return CustomKernel::class;
}
Environment Configuration:
.env.test for test-specific settings (e.g., database, cache):
symfony console --env=test cache:clear
Parallel Testing:
ParallelTestCase if needed.Debugging:
$response = $this->getClient()->getResponse();
file_put_contents('debug.html', $response->getContent());
Deprecated Methods:
requestAndAssertOkAndHtml may not handle newer Symfony Client changes.Authentication Quirks:
logIn() relies on Symfony’s security system. Ensure your firewall is configured correctly.Invalid credentials may occur if the user provider isn’t set up for tests.Route Naming:
_app. route naming convention (e.g., _app.activity.index).router->generate() if routes are named differently:
$url = $this->getClient()->getContainer()->get('router')->generate('activity.index');
HTML Assertions:
requestAndAssertOkAndHtml checks for text/html content type. False negatives may occur with:
Fixture Loading:
setUp() may not persist across tests. Use transactions or DatabaseTestCase.Client State:
$client = static::createClient([], [
'PHP_AUTH_USER' => 'user',
'PHP_AUTH_PW' => 'pass',
]);
Response Inspection:
$response = $this->getClient()->getResponse();
dump([
'status' => $response->getStatusCode(),
'headers' => $response->headers->all(),
'content' => $response->getContent(),
]);
Slow Tests:
$client = static::createClient(['environment' => 'test', 'debug' => false]);
Custom Assertions:
namespace App\Tests\Functional;
use Desarrolla2\TestBundle\Functional\WebTestCase;
class CustomWebTestCase extends WebTestCase
{
protected function assertResponseHasElement($selector)
{
$this->assertSelectorTextContains('css', $selector, 'Expected element');
}
}
Override logIn:
protected function logIn($client, $email, $roles)
{
// Custom logic (e.g., API token auth)
$client->request('POST', '/api/login', [
'json' => ['email' => $email, 'password' => 'password']
]);
return $this->getUserFromResponse($client);
}
Mock Services:
container.get() to replace services in tests:
$this->getClient()->getContainer()->set('app.mailer', $this->createMock(Mailer::class));
Event Listeners:
$client = static::createClient();
$client->getContainer()->get('event_dispatcher')->addListener(
KernelEvents::REQUEST,
[$this, 'onKernelRequest']
);
How can I help you explore Laravel packages today?