Installation:
composer require-dev brightmarch/testing-bundle --dev
Add to config/bundles.php (Laravel uses config/app.php for bundles, but this is Symfony-focused; adapt via config/testing.php or service provider):
return [
// ...
Brightmarch\TestingBundle\BrightmarchTestingBundle::class => ['dev' => true, 'test' => true],
];
First Test Case:
Extend Brightmarch\TestingBundle\TestCase in your test class:
use Brightmarch\TestingBundle\TestCase;
class UserTest extends TestCase
{
public function testHomepage()
{
$client = $this->createClient();
$client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
First Fixture:
Define a YAML fixture in tests/fixtures/users.yml:
users:
user1:
email: test@example.com
password: secret
Load it in your test:
public function setUp()
{
$this->loadFixtures(['users']);
}
Client Creation:
$client = $this->createClient();$client = $this->createAuthenticatedClient('user1'); // Uses fixtures
$client = $this->createClient([
'headers' => ['X-Custom' => 'Header'],
]);
Fixtures Management:
$this->loadFixtures(['users', 'posts']); // Loads YAML files
$user = $this->getFixture('users.user1'); // Access loaded fixture data
DatabaseSeeder-like logic in setUp() to hydrate fixtures via Eloquent.Service Container Access:
$service = $this->getContainer()->get('service_id');
Assertions:
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertContains('Welcome', $client->getResponse()->getContent());
$this->assertDatabaseHas('users', ['email' => 'test@example.com']);
Laravel-Specific:
AppServiceProvider:
if ($this->app->environment('testing')) {
$this->app->register(Brightmarch\TestingBundle\BrightmarchTestingBundle::class);
}
createApplication() in phpunit.xml:
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
TestCase in Laravel:
use Brightmarch\TestingBundle\TestCase as SymfonyTestCase;
use Illuminate\Foundation\Testing\TestCase as LaravelTestCase;
abstract class BaseTestCase extends LaravelTestCase
{
use SymfonyTestCase\Traits\ClientTrait;
use SymfonyTestCase\Traits\FixturesTrait;
}
Test Isolation:
Use setUp()/tearDown() to reset state:
public function tearDown()
{
$this->clearFixtures();
parent::tearDown();
}
Bundle Registration:
AppKernel (or config/bundles.php).config/app.php.Fixture Loading:
tests/fixtures/ by default. Customize via:
$this->setFixturesPath(__DIR__.'/../data/fixtures');
clearFixtures() to reset between tests if needed.Authenticated Clients:
$client = $this->createClient();
$client->loginUser($this->getFixture('users.user1'));
Container Access:
$service = app('service_id');
Database Transactions:
RefreshDatabase, this bundle does not auto-rollback transactions. Use:
public function setUp()
{
DB::beginTransaction();
}
public function tearDown()
{
DB::rollBack();
}
Fixture Errors:
var_dump($this->getFixture('users.user1')) to inspect loaded data.Client Issues:
$client->getProfile() to inspect requests/responses.dd($client->getResponse()->getContent()).Container Problems:
Custom Fixture Loaders:
Extend Brightmarch\TestingBundle\Loader\FixtureLoader to support JSON/XML:
class JsonFixtureLoader extends FixtureLoader
{
protected function loadFile($file)
{
return json_decode(file_get_contents($file), true);
}
}
Test Traits: Create reusable traits for common test logic:
trait AuthenticatedClientTrait
{
protected function createAuthenticatedClient($fixtureName)
{
$client = $this->createClient();
$user = $this->getFixture($fixtureName);
// Laravel-specific auth logic here
return $client;
}
}
Event Listeners: Hook into fixture loading to modify data:
$this->addFixtureListener(function ($fixtures) {
$fixtures['users.user1']['password'] = bcrypt('secret');
return $fixtures;
});
Custom Assertions:
Extend Brightmarch\TestingBundle\TestCase to add domain-specific assertions:
class CustomTestCase extends TestCase
{
protected function assertApiResponse($client, $expectedStatus, $expectedData)
{
$response = $client->getResponse();
$this->assertEquals($expectedStatus, $response->getStatusCode());
$this->assertJson($response->getContent());
$this->assertEquals($expectedData, json_decode($response->getContent(), true));
}
}
How can I help you explore Laravel packages today?