Installation
Add the bundle to your composer.json:
composer require beelab/test-bundle
Register it in config/bundles.php:
return [
// ...
Beelab\TestBundle\BeelabTestBundle::class => ['test' => true],
];
First Test Case
Extend BeelabTestBundle\TestCase instead of Symfony’s WebTestCase:
use Beelab\TestBundle\TestCase;
class MyTest extends TestCase
{
public function testHomepage(): void
{
$this->client->request('GET', '/');
$this->assertResponseStatusCodeSame(200);
}
}
Key Features to Explore
src/Assertions/ for custom helpers (e.g., assertJsonStructure()).loadFixtures() in TestCase for database seeding.createMock() with built-in trait Mockable.Database Testing
public function testUserCreation(): void
{
$this->loadFixtures(['UserFixture']);
$this->client->request('POST', '/api/users', [
'json' => ['name' => 'Test User']
]);
$this->assertDatabaseHas('users', ['name' => 'Test User']);
}
API Response Validation
public function testApiResponse(): void
{
$this->client->request('GET', '/api/data');
$this->assertJsonStructure([
'data' => [
'*' => [
'id',
'name'
]
]
]);
}
Mocking Services
use Beelab\TestBundle\Traits\Mockable;
class MyTest extends TestCase
{
use Mockable;
public function testMockedService(): void
{
$mock = $this->createMock(MyService::class);
$mock->method('doSomething')->willReturn('mocked');
$this->container->set(MyService::class, $mock);
}
}
getKernel() in TestCase for custom environments:
protected function getKernel(): KernelInterface
{
return new Kernel('test', true);
}
dispatchEvent() for testing event listeners:
$this->dispatchEvent(new UserRegisteredEvent($user));
$this->assertEventDispatched(UserRegisteredEvent::class);
setUp() if needed:
$this->container->get('cache')->clear();
Fixture Loading
tests/Fixtures/ and follow the FixtureInterface.FixtureNotFoundException → Verify namespace/class names.Assertion Overrides
assertJsonStructure() vs. assertJson()).Container Mocking
ContainerAware traits).APP_ENV=test and APP_DEBUG=1 in .env.test.dump() from symfony/var-dumper for complex objects:
$this->dump($this->client->getResponse()->getContent());
Custom Assertions
Add assertions to src/Assertions/ and autoload them in composer.json:
"autoload": {
"psr-4": {
"Beelab\\TestBundle\\Assertions\\": "src/Assertions/"
}
}
Hooks
Override setUp()/tearDown() in TestCase for pre/post-test logic:
protected function setUp(): void
{
parent::setUp();
$this->enableProfiler();
}
Configuration
Override bundle config in config/packages/test.yaml:
beelab_test:
default_locale: 'en'
fixtures_dir: '%kernel.project_dir%/tests/Fixtures/Custom'
How can I help you explore Laravel packages today?