A Symfony bundle designed to facilitate testing REST APIs. This bundle provides a set of traits and classes to automatically test CRUD operations of an API with JWT authentication.
composer require --dev citizen63000/easy-api-tests
Add the bundle to config/bundles.php (for the test environment only):
<?php
return [
// ... other bundles
EasyApiTests\EasyApiTestsBundle::class => ['test' => true],
];
Create the file config/packages/test/easy_api_tests.yaml:
easy_api_tests:
debug: false # Enable debug logs, in all cases
skipped_as_true: false # Mark skipped tests as passed
user_class: 'App\Entity\User' # User entity class
user_identity_property: 'username' # Identification property (username/email)
error_prefix: 'core.error.' # Prefix for error messages
datetime_format: !php/const DateTimeInterface::ATOM # Default date format
<?php
namespace App\Tests\Api;
use EasyApiTests\Core\AbstractApiTestCase;
use EasyApiTests\Crud\CreateTestTrait;
use EasyApiTests\Crud\GetTestTrait;
use EasyApiTests\Crud\UpdateTestTrait;
use EasyApiTests\Crud\DeleteTestTrait;
use EasyApiTests\Crud\GetListTestTrait;
class ProductApiTest extends AbstractApiTestCase
{
use CreateTestTrait;
use GetTestTrait;
use UpdateTestTrait;
use DeleteTestTrait;
use GetListTestTrait;
// Entity configuration to test
protected const string baseRouteName = 'api_product';
protected const string entityClass = 'App\Entity\Product';
protected const string identifier = 'id';
protected const string defaultEntityId = '1';
protected const string defaultEntityNotFoundId = '999';
// Test users
protected const string USER_TEST_USERNAME = 'admin@example.com';
protected const string USER_TEST_PASSWORD = 'password';
protected const string USER_NORULES_TEST_USERNAME = 'user@example.com';
// Required fields for validation tests
protected static function getRequiredFields(): array
{
return ['name', 'price'];
}
}
<?php
namespace App\Tests\Api;
use EasyApiTests\Core\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
class CustomApiTest extends AbstractApiTestCase
{
public function testCustomEndpoint(): void
{
$apiOutput = self::httpGetWithLogin(['name' => 'api_custom_route']);
$this->assertEquals(Response::HTTP_OK, $apiOutput->getStatusCode());
$this->assertArrayHasKey('data', $apiOutput->getData());
}
}
The bundle automatically generates expected data files in:
tests/Api/{EntityName}/DataSent/ : Data sent during requeststests/Api/{EntityName}/Responses/ : Expected API responsesClone the repository :
git clone https://github.com/citizen63000/easy-api-tests.git
cd easy-api-tests
Start the Docker environment:
docker compose up -d
Install dependencies:
docker compose exec app composer install
docker compose exec app composer test
docker compose exec app composer test -- tests/Unit/Core/
docker compose exec app composer test -- tests/Unit/Crud/
docker compose exec app composer test -- --testdox
docker compose exec app composer clean-code
docker compose exec app sh
easy-api-tests/
├── src/ # Bundle source code
│ ├── Core/ # Main classes
│ │ ├── AbstractApiTestCase.php
│ │ ├── ApiOutput.php
│ │ └── ...
│ ├── Crud/ # Traits for CRUD tests
│ │ ├── CreateTestTrait.php
│ │ ├── Functions/ # Test implementations
│ │ └── ...
│ └── DependencyInjection/ # Symfony configuration
├── tests/ # Unit tests
│ └── Unit/
│ ├── Core/ # Core classes tests
│ └── Crud/ # Crud classes tests
├── docker-compose.yml # Docker configuration
├── Dockerfile # PHP Docker image
└── README.md
The docker-compose.yml file automatically configures:
docker compose exec app php vendor/bin/phpunitcomposer clean-codedebug: true in configuration/tests/Unit/How can I help you explore Laravel packages today?