使用 PHP Unit 测试特殊字符

Enr*_*ent 4 phpunit utf-8 character-encoding symfony

我正在使用 PHPUnit 和类从 Symfony2 测试我的控制器WebTestCase

\n\n
return self::$client->request(\n    \'POST\', \'/withdraw\',\n    array("amount" => 130),\n    array(),array());\n\n$this->assertEquals(\n    "You can withdraw up to \xc2\xa3100.00.",\n     $crawler->filter("#error-notification")->text());\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我收到这个错误:

\n\n
Expected: "You can withdraw up to \xc2\xa3100.00."\nActual:   "You can withdraw up to \xc3\x82\xc2\xa3100.00."\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题是,在网页和源代码中它看起来不错,所以我想也许 PHPUnit 在获取 UTF8 文本时遇到了一些麻烦?

\n\n

我缺少什么?

\n

Nic*_*ich 5

解决方案:

确保mbstring扩展程序已启用。

kohana bugtracker 中报告了一个关于测试失败的错误。iconv


尖端:

正如此问题/答案中所建议的- 您可以测试正确的 UTF-8 输出:

$this->assertEquals(
    mb_detect_encoding(
        crawler->filter("#error-notification")->text(),
        'UTF-8'
    ),
    'UTF-8'
);
Run Code Online (Sandbox Code Playgroud)

您可以accept-charset在客户端发送的请求中包含标头:

$client->request(
    'POST', '/withdraw',
    array("amount" => 130),
    array(),
    array(),
    array('HTTP_ACCEPT_CHARSET' => 'utf-8')
);
Run Code Online (Sandbox Code Playgroud)