Installation:
composer require liip/functional-test-bundle
Ensure config_test.yml includes the bundle in bundles and configures the session and profiler sections as shown in the docs.
Extend Base Class:
Replace Symfony\Bundle\FrameworkBundle\Test\WebTestCase with:
use Liip\FunctionalTestBundle\Test\WebTestCase;
First Test Case:
class MyTest extends WebTestCase
{
public function testHomepage()
{
$client = $this->makeClient();
$crawler = $client->request('GET', '/');
$this->assertStatusCode(200, $client);
}
}
makeClient(true) with config_test.yml credentials or pass credentials directly.loadFixtures() (e.g., LoadUserData).config_test.yml to track excessive queries.Basic Fixture Loading:
$this->loadFixtures([
'AppBundle\DataFixtures\ORM\LoadUserData',
'AppBundle\DataFixtures\ORM\LoadProductData'
]);
Dependency Injection in Fixtures:
Use services in fixtures by declaring them in config.yml:
services:
my_fixture_service:
class: AppBundle\Service\MyFixtureService
Then inject via constructor in your fixture class.
Alice Fixtures:
Create YAML files (e.g., user.yml) and load them:
$this->loadFixtures(['AppBundle\DataFixtures\ORM\user.yml']);
Config-Driven Login:
alexis_lefebvre_fixtures:
authentication:
username: "testuser"
password: "password"
Then use:
$client = $this->makeClient(true); // Auto-logged in
Dynamic Login:
$credentials = ['username' => 'user', 'password' => 'pass'];
$client = $this->makeClient($credentials);
Fixture-Based Login:
$fixtures = $this->loadFixtures(['LoadUserData'])->getReferenceRepository();
$this->loginAs($fixtures->getReference('user_ref'), 'main');
$client = $this->makeClient();
Assertions:
$this->assertStatusCode(200, $client);
$this->assertValidationErrors(['field.name'], $client->getContainer());
Crawler/Content Fetching:
$crawler = $this->fetchCrawler('/path');
$content = $this->fetchContent('/path');
Routing:
$url = $this->getUrl('route_name', ['param' => 'value']);
$mock = $this->getServiceMockBuilder('MyService')->getMock();
$mock->expects($this->once())->method('doSomething');
Session Storage Errors:
Missing session.storage.options#name.name: MOCKSESSID under framework.session in config_test.yml.Query Counter Conflicts:
@QueryCount breaks PHPUnit annotations (e.g., @dataProvider).@IgnoreAnnotation:
/**
* @IgnoreAnnotation("dataProvider")
*/
Fixture Dependencies:
getReferenceRepository() to access loaded data.HTTP Basic Auth:
security.firewalls in config_test.yml for seamless auth:
security:
firewalls:
main:
http_basic: ~
Inspect Fixtures:
Use getReferenceRepository() to debug loaded fixtures:
$fixtures = $this->loadFixtures(['LoadUserData'])->getReferenceRepository();
dump($fixtures->getReferences());
Query Logging:
Enable Symfony’s profiler (profiler.enabled: true) to inspect queries during tests.
Client State: Reset client state between tests if needed:
$client->getContainer()->get('session')->invalidate();
Custom Fixture Providers: Extend Alice’s providers for reusable fixture logic:
# user_with_custom_provider.yml
App\DataFixtures\Faker\Provider\CustomProvider:
methods: ['customUser']
Query Count Overrides:
Use @QueryCount annotations for test-specific limits:
/**
* @QueryCount(100)
*/
public function testHighQueryPage() { ... }
Custom Assertions:
Extend WebTestCase to add domain-specific assertions:
class CustomTestCase extends WebTestCase {
protected function assertPageContains($text) {
$content = $this->fetchContent('/');
$this->assertContains($text, $content);
}
}
Parallel Testing:
Use paratest or fastest for faster test suites (see doc/paratest.md).
Fixture Caching:
Cache fixtures in phpunit.xml:
<php>
<env name="FIxturesBundle_CACHE" value="true"/>
</php>
Selective Fixture Loading: Load only necessary fixtures per test to reduce overhead.
How can I help you explore Laravel packages today?