| 1 |
<?php |
| 2 |
require_once(dirname(__FILE__)."/Test.class.php"); |
| 3 |
require_once(dirname(dirname(__FILE__))."/src/ForceUTF8/Encoding.php"); |
| 4 |
|
| 5 |
use \ForceUTF8\Encoding; |
| 6 |
|
| 7 |
// Test the testing class itself. |
| 8 |
Test::is("'yes' is true", 'yes', true); |
| 9 |
Test::not("1 is not false", 1, false); |
| 10 |
Test::identical("true is identical to true", true, true); |
| 11 |
Test::true("1 is true", 1); |
| 12 |
|
| 13 |
// ForceUTF8 tests. |
| 14 |
Test::not("Source files must not use the same encoding before conversion.", |
| 15 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 16 |
file_get_contents(dirname(__FILE__)."/data/test1Latin.txt")); |
| 17 |
|
| 18 |
Test::identical("Simple Encoding works.", |
| 19 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 20 |
Encoding::toUTF8(file_get_contents(dirname(__FILE__)."/data/test1Latin.txt"))); |
| 21 |
|
| 22 |
function test_arrays_are_different(){ |
| 23 |
$arr1 = array( |
| 24 |
file_get_contents(dirname(__FILE__)."/data/test1Latin.txt"), |
| 25 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 26 |
file_get_contents(dirname(__FILE__)."/data/test1Latin.txt")); |
| 27 |
$arr2 = array( |
| 28 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 29 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 30 |
file_get_contents(dirname(__FILE__)."/data/test1.txt")); |
| 31 |
return $arr1 != $arr2; |
| 32 |
} |
| 33 |
|
| 34 |
function test_encoding_of_arrays(){ |
| 35 |
$arr1 = array( |
| 36 |
file_get_contents(dirname(__FILE__)."/data/test1Latin.txt"), |
| 37 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 38 |
file_get_contents(dirname(__FILE__)."/data/test1Latin.txt")); |
| 39 |
$arr2 = array( |
| 40 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 41 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 42 |
file_get_contents(dirname(__FILE__)."/data/test1.txt")); |
| 43 |
return Encoding::toUTF8($arr1) == $arr2; |
| 44 |
} |
| 45 |
|
| 46 |
Test::true("Source arrays are different.", test_arrays_are_different()); |
| 47 |
Test::true("Encoding of array works.", test_encoding_of_arrays()); |
| 48 |
|
| 49 |
Test::identical("fixUTF8() maintains UTF-8 string.", |
| 50 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 51 |
Encoding::fixUTF8(file_get_contents(dirname(__FILE__)."/data/test1.txt"))); |
| 52 |
|
| 53 |
Test::not("An UTF-8 double encoded string differs from a correct UTF-8 string.", |
| 54 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 55 |
utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1.txt"))); |
| 56 |
|
| 57 |
Test::identical("fixUTF8() reverts to UTF-8 a double encoded string.", |
| 58 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 59 |
Encoding::fixUTF8(utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1.txt")))); |
| 60 |
|
| 61 |
function test_double_encoded_arrays_are_different(){ |
| 62 |
$arr1 = array( |
| 63 |
utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1Latin.txt")), |
| 64 |
utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1.txt")), |
| 65 |
utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1Latin.txt"))); |
| 66 |
$arr2 = array( |
| 67 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 68 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 69 |
file_get_contents(dirname(__FILE__)."/data/test1.txt")); |
| 70 |
return $arr1 != $arr2; |
| 71 |
} |
| 72 |
|
| 73 |
function test_double_encoded_arrays_fix(){ |
| 74 |
$arr1 = array( |
| 75 |
utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1Latin.txt")), |
| 76 |
utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1.txt")), |
| 77 |
utf8_encode(file_get_contents(dirname(__FILE__)."/data/test1Latin.txt"))); |
| 78 |
$arr2 = array( |
| 79 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 80 |
file_get_contents(dirname(__FILE__)."/data/test1.txt"), |
| 81 |
file_get_contents(dirname(__FILE__)."/data/test1.txt")); |
| 82 |
return Encoding::fixUTF8($arr1) == $arr2; |
| 83 |
} |
| 84 |
|
| 85 |
Test::true("Source arrays are different (fixUTF8).", test_double_encoded_arrays_are_different()); |
| 86 |
Test::true("Fixing of double encoded array works.", test_double_encoded_arrays_fix()); |
| 87 |
|
| 88 |
Test::identical("fixUTF8() Example 1 still working.", |
| 89 |
Encoding::fixUTF8("Fédération Camerounaise de Football\n"), |
| 90 |
"Fédération Camerounaise de Football\n"); |
| 91 |
Test::identical("fixUTF8() Example 2 still working.", |
| 92 |
Encoding::fixUTF8("Fédération Camerounaise de Football\n"), |
| 93 |
"Fédération Camerounaise de Football\n"); |
| 94 |
Test::identical("fixUTF8() Example 3 still working.", |
| 95 |
Encoding::fixUTF8("Fédération Camerounaise de Football\n"), |
| 96 |
"Fédération Camerounaise de Football\n"); |
| 97 |
Test::identical("fixUTF8() Example 4 still working.", |
| 98 |
Encoding::fixUTF8("Fédération Camerounaise de Football\n"), |
| 99 |
"Fédération Camerounaise de Football\n"); |
| 100 |
|
| 101 |
Test::totals(); |
| 102 |
|