phpunit认为两个相等的字符串不相等

时间:2015-02-01 19:26:49

标签: php testing encoding phpunit

我正在使用一些代码将ISO-8859字符串转换为utf8。然后我想声明它已被正确转换。代码是:

class EncodingTest extends TestCase {

    public function testImapUtf8()
    {
        $pairs = [
            ['=?ISO-8859-13?Q?Darba_s=E2k=F0ana_ar_Gmail?=', 'Darba sākšana ar Gmail'],
            ['Normal text', 'Normal text'], 
            ['Darba sākšana ar Gmail', 'Darba sākšana ar Gmail'],
        ];

        foreach ($pairs as $pair) {
            $this->assertEquals(($pair[1]), (imap_utf8($pair[0])));
        }
    }
}

然而,对于第一对,它给了我一个错误:

1) EncodingTest::testImapUtf8
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Darba sākšana ar Gmail'
+'Darba sākšana ar Gmail'

字符串看起来一样。我的客人有一些看不见的角色。如何测试imap_utf8是否按预期工作?

1 个答案:

答案 0 :(得分:2)

我var_dumped这两个变量并看到长度不相等

var_dump(imap_utf8('=?ISO-8859-13?Q?Darba_s=E2k=F0ana_ar_Gmail?='));
var_dump( 'Darba sākšana ar Gmail');

string(26)“DarbasākšananaGmail”

string(24)“DarbasākšananaGmail”

您可以使用 utf8_decode 功能修复长度问题。试试这个

$this->assertEquals(($pair[1]), (utf8_decode(imap_utf8($pair[0]))));