Installation Add the package via Composer:
composer require axstrad/use-case-test-bundle
Ensure your composer.json includes the required dev dependencies (e.g., liip/functional-test-bundle, phpunit/phpunit).
Bundle Registration
Add the bundle to your AppKernel.php under test environment:
if (in_array($this->getEnvironment(), ['test'], true)) {
$bundles[] = new Axstrad\UseCaseTestBundle\AxstradUseCaseTestBundle();
}
First Use Case
Create a test class extending Axstrad\UseCaseTestBundle\Test\WebTestCase:
use Axstrad\UseCaseTestBundle\Test\WebTestCase;
class MyBundleTest extends WebTestCase
{
public function testUserCreationUseCase()
{
$this->loadFixtures(['path/to/fixtures']);
$this->client->request('POST', '/register', [
'user' => ['email' => 'test@example.com', 'password' => 'secret']
]);
$this->assertTrue($this->client->getResponse()->isSuccessful());
}
}
Key Configuration
Check config/packages/axstrad_use_case_test.yaml for default settings (e.g., fixture paths, client defaults).
Define Use Cases
Group tests by business logic (e.g., UserRegistrationUseCase, OrderProcessingUseCase). Use traits or base classes for shared setup:
trait UserRegistrationTrait
{
protected function registerUser(array $data)
{
$this->client->request('POST', '/register', $data);
return $this->client->getResponse();
}
}
Fixture Management Load fixtures per use case to isolate test data:
protected function setUp(): void
{
$this->loadFixtures([
'user_fixtures.yml',
'product_fixtures.yml'
]);
}
Client Configuration Reuse client setup across tests:
protected function createAuthenticatedClient()
{
$client = static::createClient();
$client->loginUser($this->createTestUser());
return $client;
}
Integration with Symfony Components
Combine with Sensio\Bundle\FrameworkExtraBundle for routing/parameter testing:
$this->assertRouteSame('app_register', $this->client->getRequest());
Test Data Factories
Use Axstrad\Common factories (if available) for dynamic test data:
$user = $this->factory->create(User::class, ['email' => 'dynamic@example.com']);
Symfony 2.3 Dependency
AppKernel and dependencies align (e.g., Doctrine ORM 2.3).Fixture Loading Quirks
doctrine/orm or doctrine-fixtures-bundle config.config/packages/doctrine.yaml includes:
doctrine:
dbal:
driver: 'pdo_sqlite'
memory: true
Client State Persistence
WebTestCase and override setUp():
protected function setUp(): void
{
parent::setUp();
$this->client->getContainer()->get('session')->invalidate();
}
Archived Package Risks
Enable Debug Mode
Set APP_DEBUG=1 in .env.test to see detailed errors.
Log Fixture Loading
Add debug output in setUp():
$this->logger->debug('Loaded fixtures:', ['fixtures' => $this->fixtures]);
Isolate Tests
Use @group annotations to run specific use cases:
/**
* @group user-registration
*/
public function testRegistrationFlow()
Custom Test Cases
Extend WebTestCase to add domain-specific helpers:
class ApiTestCase extends WebTestCase
{
protected function assertApiSuccess($response)
{
$this->assertEquals(200, $response->getStatusCode());
$this->assertJson($response->getContent());
}
}
Hook into Fixture Pipeline
Override loadFixtures() to add pre/post-processing:
protected function loadFixtures(array $fixtures)
{
$this->preprocessFixtures($fixtures);
parent::loadFixtures($fixtures);
}
Mock External Services
Use Symfony’s HttpClient or Psr\Http\Client to mock APIs:
$this->client->getContainer()->set('payment_gateway', $this->createMock(PaymentGateway::class));
How can I help you explore Laravel packages today?