Installation:
composer require liip/functional-test-bundle
Register the bundle in config/bundles.php:
return [
// ...
Liip\FunctionalTestBundle\LiipFunctionalTestBundle::class => ['test' => true],
];
Configure config/packages/test/alexis_lefebvre_test.yaml:
alexis_lefebvre_test:
authentication:
username: "test_user"
password: "test_pass"
Extend WebTestCase:
use Liip\FunctionalTestBundle\Test\WebTestCase;
class MyTest extends WebTestCase { ... }
Test a controller with pre-loaded fixtures and auto-login:
public function testHomepage()
{
$this->loadFixtures(['AppBundle\DataFixtures\ORM\LoadUserData']);
$client = $this->makeClient(); // Auto-logged in via config
$crawler = $client->request('GET', '/');
$this->assertStatusCode(200, $client);
}
$this->loadFixtures([
'AppBundle\DataFixtures\ORM\LoadUsers',
'AppBundle\DataFixtures\ORM\LoadPosts'
]);
$fixtures = $this->loadFixtures(['LoadUsers'])->getReferenceRepository();
$user = $fixtures->getReference('user_alpha');
Auto-Login via Config:
alexis_lefebvre_test:
authentication:
username: "admin"
password: "secret"
$client = $this->makeClient(); // Auto-logged in
Dynamic Login:
$this->loginAs($fixtures->getReference('user_beta'), 'main');
$client = $this->makeClient();
HTTP Basic Auth (Recommended):
security:
firewalls:
main:
http_basic: ~
Assertions:
$this->assertStatusCode(200, $client);
$this->assertValidationErrors(['field.name'], $client->getContainer());
Crawler/Content:
$crawler = $this->fetchCrawler('/profile');
$content = $this->fetchContent('/dashboard');
Routing:
$url = $this->getUrl('app_home', ['_locale' => 'en']);
$mockService = $this->getServiceMockBuilder('AppBundle\Service\FooService')
->disableOriginalConstructor()
->getMock();
$mockService->expects($this->once())
->method('doSomething')
->willReturn('mocked');
alexis_lefebvre_test:
query:
max_query_count: 10
/**
* @QueryCount(50)
*/
public function testComplexPage() { ... }
Session Storage Error:
Missing session.storage.options#name error.config/packages/test/framework.yaml:
session:
name: MOCKSESSID
Query Counter Conflicts:
@dataProvider or @depends annotations break.@IgnoreAnnotation:
/**
* @IgnoreAnnotation("dataProvider")
* @IgnoreAnnotation("depends")
*/
Fixture Dependencies:
orderBy: [ { "LoadUsers", "LoadPosts" } ] in LoadUserData.php to enforce load order.Inspect Fixtures:
$fixtures = $this->loadFixtures(['LoadUsers']);
dump($fixtures->getReferenceRepository()->getReferences());
Log Client Requests:
$client = $this->makeClient();
$client->enableProfiler(); // Enable Symfony profiler
Disable Query Counter Temporarily:
alexis_lefebvre_test:
query:
enabled: false
Custom Fixture Providers:
Faker\Provider\Base and register in config/packages/test/alexis_lefebvre_test.yaml:
alexis_lefebvre_test:
fixtures:
providers:
- AppBundle\DataFixtures\Faker\Provider\CustomProvider
Override WebTestCase:
class CustomWebTestCase extends WebTestCase {
public function assertContainsText(string $text, string $content) { ... }
}
Parallel Testing:
paratest or fastest with:
alexis_lefebvre_test:
parallel:
enabled: true
Fixture Loading:
purgeDatabase() between tests if needed:
$this->purgeDatabase();
Query Counter Overhead:
$this->disableQueryCounter();
How can I help you explore Laravel packages today?