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

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

问题描述

我正在使用PHPUnit和类 WebTestCase

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

但是我得到这个错误:

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

问题在于,在网页和源代码中看起来不错,所以我认为PHPUnit可能在以UTF8格式获取文本时遇到了一些麻烦?

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?

我想念什么?

推荐答案

解决方案:

确保已启用 mbstring 扩展名.

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

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

提示:

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

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


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

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

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

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