2012-07-20 19 views
1

我在PHP中寫了兩個函數,str_to_utf8()seems_utf8()(好吧,它們是我從其他代碼中借用的部分)。現在我正在爲它們編寫單元測試,我想確保我有適當的單元測試。我目前採取了我從Facebook上的那些:什麼是我可以用於我的單元測試的一些有效和無效的UTF-8字符串?

public function test_str_to_utf8() 
{ 
    // Make sure ASCII characters are ignored 
    $this->assertEquals("this\x01 is a \x7f test string", str_to_utf8("this\x01 is a \x7f test string")); 

    // Make sure UTF8 characters are ignored 
    $this->assertEquals("\xc3\x9c \xc3\xbc \xe6\x9d\xb1!", str_to_utf8("\xc3\x9c \xc3\xbc \xe6\x9d\xb1!")); 

    // Test long strings 
    #str_to_utf8(str_repeat('x', 1024 * 1024)); 
    $this->assertEquals(TRUE, TRUE); 

    // Test some invalid UTF8 to see if it is properly fixed 
    $input = "\xc3 this has \xe6\x9d some invalid utf8 \xe6"; 
    $expect = "\xEF\xBF\xBD this has \xEF\xBF\xBD\xEF\xBF\xBD some invalid utf8 \xEF\xBF\xBD"; 
    $this->assertEquals($expect, str_to_utf8($input)); 
} 

那些有效的測試用例?

回答

1

我發現this resource在測試UTF-8時很有用。

如果您使用任何非latin-1文本,您需要確保您的PHP文件保存爲UTF-8或預先轉義它們

相關問題