symfony/browser-kit
Symfony BrowserKit simulates a web browser in PHP for functional tests: make requests, follow links, fill and submit forms, and inspect responses. Includes an HttpClient-based implementation to perform real HTTP requests programmatically.
HttpTests). It leverages Laravel’s HttpClient (via Symfony’s HttpClient adapter) or standalone Guzzle, making it a drop-in replacement for manual HTTP assertions or Dusk’s browser automation.HttpTests by adding DOM traversal (DomCrawler) and browser history tracking, enabling end-to-end validation without a real browser.assertResponse() with ->clickLink()->submitForm().TestCase with BrowserKit methods (e.g., public function simulateUserFlow()).BrowserKit and HttpClient as Laravel services (10-minute setup).Http facade (via HttpClient adapter).DomCrawler for HTML assertions (e.g., assertSelectorTextContains()).PHPUnit constraints (e.g., BrowserHistoryIsOnLastPage).| Risk Area | Mitigation Strategy |
|---|---|
| PHP Version Gaps | BrowserKit v8.x requires PHP 8.4+; v7.x supports 8.2+. Laravel 10+ aligns with 8.2+. |
| Symfony Dependency | Minimal footprint: Only symfony/browser-kit and symfony/http-client (~5MB). |
| Test Flakiness | Eliminates browser instability (e.g., timing issues, JS races). Tests run in ~50ms/page. |
| Learning Curve | Familiar API for Laravel devs (e.g., ->fillForm(), ->clickLink() mimics Dusk). |
| Missing Features | Gaps like WebSockets or JS execution are intentional—use Dusk/Playwright for those. |
| CI/CD Impact | No infrastructure changes needed (unlike Dusk’s ChromeDriver). |
HttpClient, DomCrawler)?TestCase via BrowserKitTestCase (custom wrapper).Http facade or standalone Guzzle (configurable).assertSelectorNotExists()).BrowserKitTestCase or custom extensions.HttpClient (already used in Laravel for HTTP requests).DomCrawler with Symfony’s Panther (headless browser testing).BrowserKitTestCase base class:
use Symfony\Component\BrowserKit\TestCase as SymfonyTestCase;
class BrowserKitTestCase extends SymfonyTestCase {
protected function createClient(array $options = []): BrowserKitClient {
return static::createClient($options, [
'browser' => new BrowserKitBrowser(),
]);
}
}
composer.json:
"require-dev": {
"symfony/browser-kit": "^8.0",
"symfony/http-client": "^6.4"
}
| Component | Compatibility Notes |
|---|---|
| Laravel 10/11 | Full support (PHP 8.2+). |
| Laravel 9 | Use BrowserKit v7.x (PHP 8.1+). |
| PHP 8.4+ | Required for BrowserKit v8.x (native HTML5 parser). |
| Guzzle 7.x | BrowserKit’s HttpClient adapter supports Guzzle. |
| PHPUnit 10+ | Works with PHPUnit’s latest constraints (e.g., BrowserHistoryIsOnLastPage). |
| PestPHP | Supports BrowserKit via custom extensions. |
| Dusk | Co-existence: Use Dusk for JS tests, BrowserKit for server-side tests. |
HttpTests assertions with BrowserKit’s ->request() + DomCrawler.$client = $this->createClient();
$crawler = $client->request('GET', '/checkout');
$crawler->submitForm('Proceed', ['payment' => 'credit_card']);
$this->assertSelectorTextContains('h1', 'Order Confirmation');
$client->clickLink('Login');
$client->submitForm('Sign In', ['email' => 'user@example.com']);
$this->assertBrowserHistoryIsOnLastPage();
$client->getCookieJar()->set(new Cookie('remember_me', '1', time() + 3600));
$client->request('POST', '/upload', ['file' => new UploadedFile(...)]);
How can I help you explore Laravel packages today?