This is the main static constructor, it is mandatory to use it when creating an instance of FunctionalTestData
:
$testData = FunctionalTestData::withUrl('/');
Will send this as the HTTP_HOST
environment variable to the Symfony Kernel, allowing it to mimic the Host:
HTTP Request header:
$testData = FunctionalTestData::withUrl('/')
->withHost('example.com');
Is used to provide another HTTP method than GET
. Will automatically be transformed to uppercase.
$testData = FunctionalTestData::withUrl('/')
->withMethod('POST');
Is sent as HTTP Request body. Must be a string
.
$testData = FunctionalTestData::withUrl('/')
->withPayload(json_encode(['message'=>'Ok!']);
Shorthand for setting the HTTP_ACCEPT_LANGUAGE
HTTP Request header to the provided value.
$testData = FunctionalTestData::withUrl('/')
->withUserLocale('en');
Will send these as HTTP Request headers to the Symfony Kernel.
Since we are using Symfony's native KernelBrowser, requests are uppercased & normalized to HTTP_*
server parameters in the process.
$testData = FunctionalTestData::withUrl('/')
->withHttpHeader('Accept', 'text/plain');
Will send these as Server parameters to the Symfony Kernel.
$testData = FunctionalTestData::withUrl('/')
->withServerParameters('APP_ENV', 'test');
Note: this part is experimental, and env vars processing differs depending on how you set them up in your project.
Will execute a callback with the KernelBrowser
instance as first argument just before making the HTTP request.
Very useful to perform logins, setting cookies, or populate a database with test data.
$testData = FunctionalTestData::withUrl('/')
->withCallbackBeforeRequest(function (KernelBrowser $browser): void {
$browser->getCookieJar()->set(new Cookie('test_cookie', 'test value'));
});