Install the Bundle
composer require cosma/testing-bundle '2.0.*'
Ensure Cosma\TestingBundle\CosmaTestingBundle is registered in config/bundles.php.
Configure Fixtures
Create a YAML fixture file (e.g., tests/fixtures/users.yml):
App\Entity\User:
user1:
email: <email()>
roles: ['ROLE_USER']
plainPassword: 'password123'
First Test Case
Use the Cosma\TestingBundle\Test\WebTestCase base class:
use Cosma\TestingBundle\Test\WebTestCase;
class UserTest extends WebTestCase
{
public function testUserCreation()
{
$this->loadFixtures(['users.yml']);
$client = static::createClient();
$client->request('GET', '/users');
$this->assertResponseIsSuccessful();
}
}
$this->loadFixtures() in test methods.<faker()> placeholders in fixtures (e.g., <name()>, <email()>).Define Fixtures
Store fixtures in tests/fixtures/ (e.g., users.yml, posts.yml).
Example with relationships:
App\Entity\User:
admin:
email: admin@example.com
roles: ['ROLE_ADMIN']
App\Entity\Post:
post1:
title: '<sentence(3)>'
author: '@admin'
Load Fixtures Dynamically
public function testPostCreation()
{
$this->loadFixtures(['users.yml', 'posts.yml']);
// Test logic here...
}
Partial Fixture Loading
Use loadFixtures() with specific fixture names:
$this->loadFixtures(['users.yml:admin']); // Load only 'admin' fixture
# config/packages/cosma_testing.yaml
cosma_testing:
reset_database: false
Use Mockery via the bundle’s helper:
public function testMockedService()
{
$mock = $this->getMockBuilder('App\Service\ExampleService')
->disableOriginalConstructor()
->getMock();
$mock->method('doSomething')->willReturn('mocked!');
$this->container->set('App\Service\ExampleService', $mock);
// Test logic...
}
Extend Cosma\TestingBundle\Test\WebTestCase for web tests or Cosma\TestingBundle\Test\TestCase for non-web tests. Override getFixtures() to define default fixtures:
protected function getFixtures()
{
return ['users.yml', 'posts.yml'];
}
Fixture Loading Order
@ references to enforce dependencies:
App\Entity\Post:
post1:
title: '<sentence(3)>'
author: '@admin' # Requires 'admin' fixture to exist
Database Transactions
@database annotation for transactional tests:
/**
* @database
*/
public function testTransactional()
{
// Changes roll back after test.
}
Faker Locale
config/packages/cosma_testing.yaml:
cosma_testing:
faker_locale: 'fr_FR'
Schema Reset Performance
reset_database improves speed but may cause test pollution:
cosma_testing:
reset_database: false
Fixture Validation
dumpFixtures() to inspect loaded data:
$this->loadFixtures(['users.yml']);
$this->dumpFixtures(); // Outputs loaded entities to console.
Mockery Debugging
phpunit.xml:
<php>
<env name="MOCKERY_VERBOSE" value="1"/>
</php>
Fixture Paths
tests/fixtures/ or configured via:
cosma_testing:
fixtures_dir: '%kernel.project_dir%/custom/fixtures'
Custom Fixture Loaders
Implement Cosma\TestingBundle\Loader\FixtureLoaderInterface for custom formats (e.g., JSON).
Test Retries
Use @retry annotation to retry flaky tests (configurable in cosma_testing.yaml):
/**
* @retry(3)
*/
public function testFlaky()
{
// ...
}
Event Listeners
Subscribe to cosma_testing.fixtures.load or cosma_testing.test.start events for pre/post-test logic:
// src/EventListener/TestListener.php
public function onTestStart(TestEvent $event)
{
// Setup logic...
}
How can I help you explore Laravel packages today?