Skip to content

Commit c98b88a

Browse files
Daniel Perssoncmb69
Daniel Persson
authored andcommitted
Added two tests to check the main functionallity of recode extension
1 parent 874dcd8 commit c98b88a

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

ext/recode/tests/001.phpt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
recode_string() function - Testing string conversions between latin1, UTF-8 and html
3+
--SKIPIF--
4+
<?php if (!extension_loaded("recode")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
function ascii2hex($ascii) {
8+
$hex = '';
9+
for ($i = 0; $i < strlen($ascii); $i++) {
10+
$byte = dechex(ord($ascii{$i}));
11+
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
12+
$hex .= $byte . " ";
13+
}
14+
return $hex;
15+
}
16+
17+
function hex2ascii($hex){
18+
$ascii='';
19+
$hex=str_replace(" ", "", $hex);
20+
for($i=0; $i<strlen($hex); $i=$i+2) {
21+
$ascii .= chr(hexdec(substr($hex, $i, 2)));
22+
}
23+
return($ascii);
24+
}
25+
26+
$lat1_hex_org = '31 32 33 e5 e4 f6 61 62 63';
27+
$utf8_hex = ascii2hex(recode_string('lat1..utf-8', hex2ascii($lat1_hex_org)));
28+
$html = recode_string('utf-8..html', hex2ascii($utf8_hex));
29+
$lat1_hex = ascii2hex(recode_string('html..lat1', $html));
30+
31+
echo "#" . $utf8_hex . "#\n";
32+
echo "#" . $html . "#\n";
33+
echo "#" . $lat1_hex . "#\n";
34+
?>
35+
--EXPECT--
36+
#31 32 33 c3 a5 c3 a4 c3 b6 61 62 63 #
37+
#123&aring;&auml;&ouml;abc#
38+
#31 32 33 e5 e4 f6 61 62 63 #

ext/recode/tests/002.phpt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
recode_string() function - Testing string conversions between latin1, UTF-8 and html
3+
--SKIPIF--
4+
<?php if (!extension_loaded("recode")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
function ascii2hex($ascii) {
8+
$hex = '';
9+
for ($i = 0; $i < strlen($ascii); $i++) {
10+
$byte = dechex(ord($ascii{$i}));
11+
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
12+
$hex .= $byte . " ";
13+
}
14+
return $hex;
15+
}
16+
17+
$html_file = fopen(realpath(dirname(__FILE__)) . '/html.raw', 'r');
18+
$utf_8_filepath = realpath(dirname(__FILE__)) . '/utf8.raw';
19+
$utf_8_file = fopen($utf_8_filepath, 'w+');
20+
21+
recode_file('html..utf8', $html_file, $utf_8_file);
22+
23+
rewind($utf_8_file);
24+
echo '#' . ascii2hex(fread($utf_8_file, filesize($utf_8_filepath))) . "#\n";
25+
26+
fclose($html_file);
27+
fclose($utf_8_file);
28+
29+
unlink($utf_8_filepath);
30+
?>
31+
--EXPECT--
32+
#31 32 33 c3 a5 c3 a4 c3 b6 61 62 63 #

ext/recode/tests/html.raw

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123&aring;&auml;&ouml;abc

0 commit comments

Comments
 (0)