- How do I integrate Pact PHP with Laravel’s HTTP client (e.g., Http::get()) for contract testing?
- Pact PHP replaces Laravel’s Http::fake() for external API interactions. Use the Pact mock server to define expected requests/responses in your tests, then verify interactions with Pact’s DSL. For example, define a contract for an external API call using `Pact::given()->expects()->willRespondWith()`, then run your Laravel tests against the mock server. The package intercepts HTTP calls via Laravel’s service container or test suite.
- Does pact-foundation/pact-php support Laravel’s async features like queues or events?
- Yes, Pact PHP supports asynchronous interactions via Laravel’s Bus or Events. Use the Pact DSL to define expected queue jobs (e.g., `DispatchesJobs`) or event publishing/consumption (e.g., `DispatchesEvents`). The mock server validates these interactions alongside HTTP requests, ensuring your async workflows adhere to contracts.
- Can I use Pact with Laravel’s Pest testing framework instead of PHPUnit?
- Absolutely. Pact PHP works seamlessly with Pest alongside PHPUnit. Use Pact’s annotations (e.g., `@pact`) or custom test traits in Pest test files. The mock server and verification logic remain the same, so you can leverage Pest’s syntax while maintaining Pact’s contract testing benefits.
- What Laravel versions does pact-foundation/pact-php officially support?
- Pact PHP is compatible with Laravel 8.x, 9.x, and 10.x, as it targets PHP 8.0+. Check the [compatibility suite](https://github.com/pact-foundation/pact-php/actions/workflows/compatibility-suite.yml) for the latest supported versions. The package avoids Laravel-specific dependencies, ensuring broad compatibility across versions.
- How do I store and version Pact contracts in a Laravel project?
- Contracts can be stored in version control (e.g., `tests/contracts/`) or published to a Pact Broker for centralized management. Use Laravel’s config system (e.g., `config/pact.php`) to define contract paths or broker URLs. For versioning, align contract files with API versions (e.g., `v1-contract.json`) and automate updates via CI/CD pipelines.
- Is Pact PHP suitable for testing GraphQL APIs in Laravel?
- Pact PHP primarily focuses on HTTP/REST and event-driven systems, but it can test GraphQL APIs if they expose HTTP endpoints. Define GraphQL queries/mutations as request bodies in Pact contracts, then verify responses. For stateful GraphQL APIs, use Pact’s state management (e.g., `given()->willRespondWith()`) to simulate complex workflows.
- How do I integrate Pact verification into Laravel’s CI/CD pipeline?
- Add a `pact:verify` step to your CI pipeline (e.g., GitHub Actions, GitLab CI) to validate provider contracts against consumer expectations. Use Laravel’s Artisan commands to wrap Pact verification (e.g., `php artisan pact:verify`). For Pact Broker integration, publish contracts to the broker post-merge and verify them in downstream pipelines.
- What alternatives exist for contract testing in Laravel, and why choose Pact?
- Alternatives include Laravel’s built-in `Http::fake()` (for unit testing) or custom mock servers, but these lack Pact’s formal contract verification and cross-service validation. Pact excels in microservices by enforcing explicit consumer-provider agreements, reducing flaky E2E tests, and integrating with tools like Pact Broker for CI/CD workflows.
- How do I handle stateful APIs (e.g., multi-step workflows) in Pact tests?
- Use Pact’s state management DSL to define preconditions (e.g., `given('user exists')`) and expected responses (e.g., `willRespondWith()`). For complex state transitions, combine Pact with Laravel’s `DatabaseTransactions` or custom providers. Example: `Pact::given('user logged in')->expects()->willRespondWith(['status' => 200])`.
- Can Pact PHP mock entire Laravel services or just specific endpoints?
- Pact PHP is flexible: mock entire services by defining broad contracts (e.g., all `/api/*` routes) or granular endpoints (e.g., `/api/users`). For Laravel, use route-based matching (e.g., `Route::get('/users')`) in contracts. Start with specific endpoints to isolate changes, then expand contracts as your API grows.