使用 PHP 单元测试特殊字符 [英] Testing special characters with PHP Unit

查看:40
本文介绍了使用 PHP 单元测试特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

解决方案

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

这篇关于使用 PHP 单元测试特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆