Installation
Add to composer.json under require-dev:
"axstrad/test-bundle": "dev-main"
Run composer update axstrad/test-bundle --dev.
Enable the Bundle
Register in config/bundles.php:
Axstrad\TestBundle\AxstradTestBundle::class => ['dev' => true, 'test' => true],
First Use Case
Use the TestCase base class for functional tests:
use Axstrad\TestBundle\Test\WebTestCase;
class MyBundleTest extends WebTestCase
{
public function testSomething()
{
$client = static::createClient();
$client->request('GET', '/some-route');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
Dependency Injection for Testing
Override services in config/packages/test/axstrad_test.yaml:
services:
_defaults:
bind:
App\Service\MyService: '@Axstrad\TestBundle\Test\MockMyService'
Database Testing
Use the DatabaseTestCase for DB-aware tests:
use Axstrad\TestBundle\Test\DatabaseTestCase;
class UserRepositoryTest extends DatabaseTestCase
{
protected function getDatabaseConfig()
{
return [
'driver' => 'pdo_sqlite',
'memory' => true,
];
}
}
Mocking External Services
Leverage the MockBuilder trait:
use Axstrad\TestBundle\Test\MockBuilder;
class PaymentServiceTest extends WebTestCase
{
use MockBuilder;
public function testPayment()
{
$this->mockService('payment.gateway', function ($mock) {
$mock->method('charge')->willReturn(true);
});
}
}
Symfony Kernel Integration
Extend AxstradTestBundle to add custom test utilities:
namespace App\Tests;
use Axstrad\TestBundle\AxstradTestBundle;
class AppTestBundle extends AxstradTestBundle
{
public function load(array $config)
{
// Custom test configurations
}
}
PHPUnit Configuration
Add to phpunit.xml.dist:
<php>
<env name="KERNEL_CLASS" value="App\Kernel"/>
<env name="APP_ENV" value="test"/>
</php>
Bundle Version Mismatch The package targets Symfony 2.3 and PHPUnit 4.1. Ensure compatibility:
composer require symfony/symfony:^2.3 --dev
Database Isolation SQLite in-memory DBs (default) do not persist between tests. Use transactions or fixtures:
protected function setUp()
{
$this->loadFixtures(['user.yml']);
}
Service Overrides
Overrides in axstrad_test.yaml must match the exact service ID. Use:
php bin/console debug:container | grep "payment.gateway"
Enable Debug Mode
Set APP_DEBUG=1 in .env.testing for detailed error logs.
Test Case Isolation Clear caches between test suites:
public static function tearDownAfterClass()
{
self::bootKernel();
self::$kernel->getContainer()->get('cache')->clear();
}
Custom Test Traits
Extend AxstradTestBundle to add reusable traits:
namespace Axstrad\TestBundle\Test;
trait AssertionHelpers
{
protected function assertJsonResponse($client, $expected)
{
$this->assertEquals($expected, json_decode($client->getResponse()->getContent(), true));
}
}
Event Listeners
Add test-specific listeners in config/packages/test/axstrad_test.yaml:
services:
app.test.event_listener:
class: App\Tests\EventListener\TestListener
tags:
- { name: kernel.event_listener, event: kernel.test, method: onKernelTest }
How can I help you explore Laravel packages today?