How can I populate/imitate server variables in PHPUnit Test cases using Symfony3?
I try to create functional tests for my symfony3 application.
I use LiipFunctionalTestBundle.
I have a special $_SERVER
variable which contains the country of the visitor. The behavior of the action strongly depends on it.
I try to populate it in my test case.
Here is the content of the phpunit.xml.dist
file which is based on Setting PHP INI settings, Constants and Global Variables in PHPUnit
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="app/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="app/" />
<server name="COUNTRY_CODE" value="AT" />
<server name="HTTP_COUNTRY_CODE" value="AT" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
<exclude>
<directory>src/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
$client = static::createClient(array(), array(
'PHP_AUTH_USER' => "test_login",
'PHP_AUTH_PW' => "test_password",
'HTTP_HOST' => "phpunit.symfony.dev",
'REQUEST_URI' => "/app/setting",
'DOCUMENT_ROOT' => "/path/to/docroot/app/web"
));
and
$this->client->request(
'GET',
'/app/profile',
array(),
array(),
array(
'COUNTRY_CODE' => "AT",
'HTTP_COUNTRY_CODE' => "AT",
'SCRIPT_NAME' => "thing/thong",
'HTTP_REFERER' => '/foo/bar'
)
);
when I get the request in the unittest by dump($this->client->getRequest());
then I see my variables:
Symfony\Component\HttpFoundation\Request {#1009
+attributes: Symfony\Component\HttpFoundation\ParameterBag {#1008
#parameters: []
}
+request: Symfony\Component\HttpFoundation\ParameterBag {#1093
#parameters: []
}
+query: Symfony\Component\HttpFoundation\ParameterBag {#1092
#parameters: []
}
+server: Symfony\Component\HttpFoundation\ServerBag {#993
#parameters: array:22 [
"PHP_AUTH_USER" => "test_login"
"PHP_AUTH_PW" => "test_password"
"SERVER_NAME" => "phpunit.symfony.dev"
"HTTP_HOST" => "phpunit.symfony.dev"
"REQUEST_URI" => "/app/setting"
"DOCUMENT_ROOT" => "/path/to/docroot/app/web"
"COUNTRY_CODE" => "AT"
"HTTP_COUNTRY_CODE" => "AT"
"SCRIPT_NAME" => "thing/thong"
"HTTP_REFERER" => "/foo/bar"
"SERVER_PORT" => 80
"HTTP_USER_AGENT" => "Symfony BrowserKit"
"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
"HTTP_ACCEPT_LANGUAGE" => "en-us,en;q=0.5"
"HTTP_ACCEPT_CHARSET" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
"REMOTE_ADDR" => "127.0.0.1"
"SCRIPT_FILENAME" => ""
"SERVER_PROTOCOL" => "HTTP/1.1"
"REQUEST_TIME" => 1485369277
"PATH_INFO" => ""
"REQUEST_METHOD" => "GET"
"QUERY_STRING" => ""
]
}
but for some reason, in the action
public function indexAction(Request $request)
{
dump($_SERVER);
}
the $_SERVER
variable contains the Shell and Environmental Variables instead:
"LESSOPEN" => "| /usr/bin/lesspipe %s"
"PROFILEHOME" => ""
"GNOME_KEYRING_PID" => ""
"USER" => "developer"
"LANGUAGE" => "en_US"
"UPSTART_INSTANCE" => ""
"XDG_SEAT" => "seat0"
"SESSION" => "sess$58479"
"XDG_SESSION_TYPE" => "x11"
"SHLVL" => "2"
"HOME" => "/home/developer"
"DESKTOP_SESSION" => "sess$58479"
"QT_LINUX_ACCESSIBILITY_ALWAYS_ON" => "1"
"SHELL_SESSION_ID" => "78c26972fe6040009409a2bd8748034d"
"GTK_MODULES" => "gail:atk-bridge:unity-gtk-module"
"XDG_SEAT_PATH" => "/org/freedesktop/DisplayManager/Seat0"
"KONSOLE_DBUS_SESSION" => "/Sessions/1"
What do I do wrong? Is there any way to imitate the server variables in command line, using Symfony3 and phpunit?
I was on a wrong way. I can access the server variables through the $request
object:
public function indexAction(Request $request)
{
dump($request->server->get('HTTP_HOST'));
dump($request->server->get('COUNTRY_CODE'));
}