Testing special characters with PHP Unit

…衆ロ難τιáo~ 提交于 2021-02-07 08:20:07

问题


I am testing my controller from Symfony2 with PHPUnit and the class WebTestCase

return self::$client->request(
    'POST', '/withdraw',
    array("amount" => 130),
    array(),array());

$this->assertEquals(
    "You can withdraw up to £100.00.",
     $crawler->filter("#error-notification")->text());

But I get this error:

Expected: "You can withdraw up to £100.00."
Actual:   "You can withdraw up to £100.00."

The thing is that in the webpage and the source code it looks fine, so I am thinking that maybe PHPUnit is having some trouble to fetch the text as UTF8?

What am I missing?


回答1:


solution:

Make sure the mbstring extension is enabled.

There was a bug about failing tests with iconv reported in the kohana bugtracker.


tips:

As proposed in this question/answer - you can test for correct UTF-8 output:

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

You can include accept-charset headers with requests sent by the client:

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


来源:https://stackoverflow.com/questions/22017744/testing-special-characters-with-php-unit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!