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');Will send these as Request parameters to the Symfony Client.
Symfony adds them to $request->request when using HttpFoundation (which is the default).
$testData = FunctionalTestData::withUrl('/')
->withRequestParameter('some_data', 'value');Will send these as Files to the Symfony Client.
Remember that you muse an instance of the Symfony\Component\HttpFoundation\File\File class for this when using the default test client with Symfony.
Symfony adds them to $request->files when using HttpFoundation (which is the default).
$testData = FunctionalTestData::withUrl('/')
->withFile('file_name', new UploadedFile('/tmp/filename.png', 'original_filename.png'));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'));
});