如何使用Symfony3在PHPUnit测试用例中填充/模仿服务器变量?
我尝试为我的symfony3应用程序创建功能测试。
我有一个特殊的$_SERVER
变量,其中包含访问者的国家/地区。行为的行为很大程度上取决于它。
我尝试在我的测试用例中填充它。
以下是基于Setting PHP INI settings, Constants and Global Variables in PHPUnit
的phpunit.xml.dist
文件的内容
<?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"
));
和
$this->client->request(
'GET',
'/app/profile',
array(),
array(),
array(
'COUNTRY_CODE' => "AT",
'HTTP_COUNTRY_CODE' => "AT",
'SCRIPT_NAME' => "thing/thong",
'HTTP_REFERER' => '/foo/bar'
)
);
当我通过dump($this->client->getRequest());
获取unittest中的请求时,我会看到我的变量:
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" => ""
]
}
但出于某种原因,在行动中
public function indexAction(Request $request)
{
dump($_SERVER);
}
$_SERVER
变量包含Shell和Environmental Variables:
"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"
我做错了什么? 有没有办法在命令行中模仿服务器变量,使用Symfony3和phpunit?
答案 0 :(得分:2)
我走错了路。我可以通过$request
对象访问服务器变量:
public function indexAction(Request $request)
{
dump($request->server->get('HTTP_HOST'));
dump($request->server->get('COUNTRY_CODE'));
}