boulzy/behat-api-platform-bundle
Installation Add the bundle via Composer in your Symfony/Laravel (via Symfony bridge) project:
composer require boulzy/behat-api-platform-bundle
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
return [
// ...
Boulzy\BehatApiPlatformBundle\BoulzyBehatApiPlatformBundle::class => ['all' => true],
];
Configure Behat
Update behat.yml to include the bundle’s extensions:
default:
extensions:
- Boulzy\BehatApiPlatformBundle\Behat\ApiPlatformExtension
- Behat\MinkExtension:
base_url: 'http://api.test' # Your API Platform endpoint
First Test Case
Create a feature file (e.g., features/api/authentication.feature) and use the bundle’s custom steps:
Feature: API Authentication
Scenario: Successful login
Given I am authenticated as a user with email "test@example.com" and password "password"
When I request "POST" to "/api/login_check"
Then the response status code should be 200
Key Directories
src/Behat/ApiPlatformContext.php: Default context class (extend for custom logic).config/packages/boulzy_behat_api_platform.yaml: Bundle configuration (if needed).Authentication Handling
Given I am authenticated as a user with ... to auto-generate JWT/Bearer tokens.Given I am authenticated as a user with:
| email | password |
| test@example.com | password |
API Requests
When I request steps with API Platform’s routes:
When I request "GET" to "/api/users/1"
When I send a POST request to "/api/users" with:
| name | email |
| John | john@example.com |
Response Validation
Then the response status code should be 201
And the response should contain JSON:
"""
{
"id": 1,
"name": "John"
}
"""
Then the response should match schema for JSON Schema validation.Data Fixtures
Given steps:
Given there is a user with:
| email | password | roles |
| admin@example.com | secret | ROLE_ADMIN |
Integration with API Platform
When I send a POST request to "/api/users" with invalid JSON
Then the response status code should be 400
fetch requests in Behat.?where[name]=John):
When I request "GET" to "/api/users?where[name]=John"
ApiPlatformContext to add domain-specific steps:
// src/Behat/CustomContext.php
use Boulzy\BehatApiPlatformBundle\Behat\ApiPlatformContext;
class CustomContext extends ApiPlatformContext {
public function assertUserHasRole($userId, $role) {
// Custom logic
}
}
Register in behat.yml:
extensions:
- Behat\MinkExtension:
goutte_driver:
custom_context: App\Behat\CustomContext
Token Expiry
Given I refresh my authentication token or configure shorter expiry in config/packages/security.yaml for tests:
lexik_jwt_authentication:
token_ttl: 3600 # 1 hour (reduce for tests)
Mink Session Management
Given I clear the Mink session
@BeforeScenario hooks in a context to reset state.API Platform Version Mismatch
api_platform.core.serializer).config/packages/api_platform.yaml if needed:
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
CORS Issues
localhost or configure CORS in config/packages/nelmio_cors.yaml:
nelmio_cors:
defaults:
allow_origin: ['*']
Database Transactions
Given the database is in a transaction
doctrine/doctrine-bundle and behat/mink-doctrine-extension.Enable Mink Debugging
Add to behat.yml:
extensions:
Behat\MinkExtension:
base_url: 'http://api.test'
goutte_driver:
options:
debug: true
Check logs in var/log/test.log for failed requests.
Inspect Raw Responses Use custom steps to dump responses:
// In a context
public function dumpResponse() {
$response = $this->getSession()->getResponse();
file_put_contents('response.json', $response->getContent());
}
Then call in Gherkin:
When I dump the response
Network Tab Emulation
Use mink-debug-extension to simulate browser dev tools:
composer require --dev behat/mink-debug-extension
Custom Steps
Extend ApiPlatformContext to add domain logic:
public function assertUserExists($email) {
$client = static::createClient();
$response = $client->request('GET', '/api/users?email='.$email);
Assert::jsonContains($response->getContent(), ['email' => $email]);
}
Hooks for Setup/Teardown
Use @BeforeScenario/@AfterScenario in contexts to manage state:
use Behat\Behat\Hook\Scope\ScenarioScope;
/**
* @BeforeScenario
*/
public function gatherContexts(ScenarioScope $scope) {
$this->user = User::factory()->create();
}
Mock External Services
Use HttpClient in contexts to mock API calls:
public function mockPaymentService() {
$this->client->addMockResponse(
new Response(200, [], json_encode(['success' => true]))
);
}
Parallel Testing Configure Behat’s parallel runner to avoid session conflicts:
./vendor/bin/behat --tags=~@wip --parallel=4
Ensure your API can handle concurrent requests (e.g., with Puma/Unicorn).
How can I help you explore Laravel packages today?