Skip to content

Latest commit

 

History

History
83 lines (54 loc) · 2.17 KB

RequestData.md

File metadata and controls

83 lines (54 loc) · 2.17 KB

← Back to main documentation

FunctionalTestData API for Request data and input

Url

This is the main static constructor, it is mandatory to use it when creating an instance of FunctionalTestData:

$testData = FunctionalTestData::withUrl('/');

Host

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');

Method (default GET)

Is used to provide another HTTP method than GET. Will automatically be transformed to uppercase.

$testData = FunctionalTestData::withUrl('/')
    ->withMethod('POST');

Payload

Is sent as HTTP Request body. Must be a string.

$testData = FunctionalTestData::withUrl('/')
    ->withPayload(json_encode(['message'=>'Ok!']);

User locale

Shorthand for setting the HTTP_ACCEPT_LANGUAGE HTTP Request header to the provided value.

$testData = FunctionalTestData::withUrl('/')
    ->withUserLocale('en');

Http header

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');

Server / Env parameters (experimental)

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.

Execute an action before making the actual HTTP request

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'));
    });