lchrusciel/api-test-case
PHPUnit TestCase for Symfony API development. Extends WebTestCase with easy JSON/XML response matching (via php-matcher) and optional Alice/Doctrine fixtures loading. Supports a clear TDD workflow using expected response files and helpful diffs.
WebTestCase extension) and leverages Symfony’s kernel, routing, and HTTP client. This aligns well with Laravel’s Lumen or Symfony-like microservices but introduces mismatches for vanilla Laravel due to:
Kernel class (Laravel uses Illuminate\Foundation\Application).Router (Laravel uses Illuminate\Routing\Router).Client (Laravel’s HttpClient or HttpTesting traits differ).assertResponse() is a strong fit for API-driven development, especially for teams using contract testing or API-first TDD.DatabaseMigrations or Laravel Fixtures packages).spatie/laravel-symfony).Client to Laravel’s Http).Kernel replacement needed).nelmio/alice + fidry/alice-data-fixtures (compatible with Laravel’s Doctrine or Eloquent).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Symfony Kernel Lock-in | High | Abstract Symfony Kernel via adapter pattern. |
| Routing Incompatibility | Medium | Use Laravel’s Router mocking or rewrite routes. |
| Fixture Complexity | Medium | Prefer Eloquent factories (e.g., laravel-shift/database-factories) over Alice. |
| PHPUnit Version Gaps | Low | Laravel’s PHPUnit 10+ is supported (v5.3.3+). |
| Performance Overhead | Low | Minimal; only adds assertion logic. |
Kernel → Application)?phpunit.xml overrides.)| Laravel Component | ApiTestCase Compatibility | Workaround Needed? |
|---|---|---|
| HTTP Client | ❌ (Symfony Client) |
Yes: Use Http::fake() or adapter. |
| Routing | ❌ (Symfony Router) |
Yes: Mock routes or rewrite tests. |
| Kernel | ❌ (Symfony Kernel) |
Yes: Abstract or replace with Laravel. |
| PHPUnit | ✅ (v9/10/11) | No. |
| Doctrine ORM | ✅ (via Alice) | Yes: Configure nelmio/alice bundles. |
| Eloquent | ⚠️ (No native Alice) | Yes: Use laravel-shift/factories. |
| JSON/XML Assertions | ✅ (PHP-Matcher) | No. |
Phase 1: Proof of Concept (PoC)
tests/ApiTestCase).Client with Laravel’s Http::fake() or a custom adapter:
// Example: Laravel Http Client Wrapper
class LaravelApiTestCase extends JsonApiTestCase {
protected function createClient(): Client {
return new Client($this->createLaravelHttpClient());
}
private function createLaravelHttpClient(): \Illuminate\Http\Client\PendingRequest {
return Http::fake();
}
}
assertResponse) against Laravel API endpoints.Phase 2: Fixture Integration
nelmio/alice and fidry/alice-data-fixtures.config/testing.php (Symfony-style).laravel-shift/factories or orchestra/testbench.create()/factory().Phase 3: Full Adoption
JsonApiTestCase.phpunit.xml to include ApiTestCase’s environment variables (e.g., EXPECTED_RESPONSE_DIR).assertResponse().doctrine/dbal in Laravel.Http::fake() alongside.Client with Laravel’s Http (highest risk).assertJsonStructure).assertResponse() replaces manual JSON diffing.Kernel may leak state in Laravel’s service container.TMP_DIR config).laravel-shift/database-factories + Pest’s assertJson.spatie/laravel-symfony + php-matcher directly.DatabaseTransactions or DatabaseMigrations per test.WebTestCase.assertResponse() syntax (e.g., @integer@ patterns).| Scenario | Impact | Mitigation |
|---|---|---|
Symfony Kernel mismatch |
Tests fail on createKernel(). |
Use adapter or fork the package. |
| Routing conflicts | 404 errors in tests. | Mock routes or rewrite tests |
How can I help you explore Laravel packages today?