Projet

Général

Profil

Paste
Télécharger (6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / simplepie / tests / EncodingTest.php @ 41cc1b08

1
<?php
2
/**
3
 * Encoding tests for SimplePie_Misc::change_encoding() and SimplePie_Misc::encoding()
4
 *
5
 * SimplePie
6
 *
7
 * A PHP-Based RSS and Atom Feed Framework.
8
 * Takes the hard work out of managing a complete RSS/Atom solution.
9
 *
10
 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without modification, are
14
 * permitted provided that the following conditions are met:
15
 *
16
 *         * Redistributions of source code must retain the above copyright notice, this list of
17
 *           conditions and the following disclaimer.
18
 *
19
 *         * Redistributions in binary form must reproduce the above copyright notice, this list
20
 *           of conditions and the following disclaimer in the documentation and/or other materials
21
 *           provided with the distribution.
22
 *
23
 *         * Neither the name of the SimplePie Team nor the names of its contributors may be used
24
 *           to endorse or promote products derived from this software without specific prior
25
 *           written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
28
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
30
 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @package SimplePie
38
 * @version 1.3.1
39
 * @copyright 2004-2011 Ryan Parman, Geoffrey Sneddon, Ryan McCue
40
 * @author Ryan Parman
41
 * @author Geoffrey Sneddon
42
 * @author Ryan McCue
43
 * @link http://simplepie.org/ SimplePie
44
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
45
 */
46

    
47
require_once dirname(__FILE__) . '/bootstrap.php';
48

    
49
class EncodingTest extends PHPUnit_Framework_TestCase
50
{
51
        /**
52
         * Test if we have mbstring
53
         *
54
         * Used for depends
55
         */
56
        public function test_has_mbstring()
57
        {
58
                $this->assertTrue(function_exists('mb_convert_encoding'));
59
        }
60

    
61
        /**
62
         * Test if we have iconv (crazy if we don't)
63
         *
64
         * Used for depends
65
         */
66
        public function test_has_iconv()
67
        {
68
                $this->assertTrue(function_exists('iconv'));
69
        }
70

    
71
        /**#@+
72
         * UTF-8 methods
73
         */
74
        /**
75
         * Provider for the convert toUTF8* tests
76
         */
77
        public static function toUTF8()
78
        {
79
                return array(
80
                        array('A', 'A', 'ASCII'),
81
                        array("\xa1\xdb", "\xe2\x88\x9e", 'Big5'),
82
                        array("\xa1\xe7", "\xe2\x88\x9e", 'EUC-JP'),
83
                        array("\xa1\xde", "\xe2\x88\x9e", 'GBK'),
84
                        array("\x81\x87", "\xe2\x88\x9e", 'Shift_JIS'),
85
                        array("\x2b\x49\x68\x34\x2d", "\xe2\x88\x9e", 'UTF-7'),
86
                        array("\xfe\xff\x22\x1e", "\xe2\x88\x9e", 'UTF-16'),
87
                        array("\xff\xfe\x1e\x22", "\xe2\x88\x9e", 'UTF-16'),
88
                        array("\x22\x1e", "\xe2\x88\x9e", 'UTF-16BE'),
89
                        array("\x1e\x22", "\xe2\x88\x9e", 'UTF-16LE'),
90
                );
91
        }
92

    
93
        /**
94
         * Special cases with mbstring handling
95
         */
96
        public static function toUTF8_mbstring()
97
        {
98
                return array(
99
                        array("\xa1\xc4", "\xe2\x88\x9e", 'EUC-KR'),
100
                );
101
        }
102

    
103
        /**
104
         * Special cases with iconv handling
105
         */
106
        public static function toUTF8_iconv()
107
        {
108
                return array(
109
                        array("\xfe\xff\x22\x1e", "\xe2\x88\x9e", 'UTF-16'),
110
                );
111
        }
112

    
113
        /**
114
         * Convert * to UTF-8
115
         *
116
         * @dataProvider toUTF8
117
         */
118
        public function test_convert_UTF8($input, $expected, $encoding)
119
        {
120
                $encoding = SimplePie_Misc::encoding($encoding);
121
                $this->assertEquals($expected, SimplePie_Misc::change_encoding($input, $encoding, 'UTF-8'));
122
        }
123

    
124
        /**
125
         * Convert * to UTF-8 using mbstring
126
         *
127
         * Special cases only
128
         * @depends test_has_mbstring
129
         * @dataProvider toUTF8_mbstring
130
         */
131
        public function test_convert_UTF8_mbstring($input, $expected, $encoding)
132
        {
133
                $encoding = SimplePie_Misc::encoding($encoding);
134
                if (version_compare(phpversion(), '5.3', '<'))
135
                {
136
                        $this->assertEquals($expected, Mock_Misc::__callStatic('change_encoding_mbstring', array($input, $encoding, 'UTF-8')));
137
                }
138
                else
139
                {
140
                        $this->assertEquals($expected, Mock_Misc::change_encoding_mbstring($input, $encoding, 'UTF-8'));
141
                }
142
        }
143

    
144
        /**
145
         * Convert * to UTF-8 using iconv
146
         *
147
         * Special cases only
148
         * @depends test_has_iconv
149
         * @dataProvider toUTF8_iconv
150
         */
151
        public function test_convert_UTF8_iconv($input, $expected, $encoding)
152
        {
153
                $encoding = SimplePie_Misc::encoding($encoding);
154
                if (version_compare(phpversion(), '5.3', '<'))
155
                {
156
                        $this->assertEquals($expected, Mock_Misc::__callStatic('change_encoding_iconv', array($input, $encoding, 'UTF-8')));
157
                }
158
                else {
159
                        $this->assertEquals($expected, Mock_Misc::change_encoding_iconv($input, $encoding, 'UTF-8'));
160
                }
161
        }
162
        /**#@-*/
163

    
164
        /**#@+
165
         * UTF-16 methods
166
         */
167
        public static function toUTF16()
168
        {
169
                return array(
170
                        array("\x22\x1e", "\x22\x1e", 'UTF-16BE'),
171
                        array("\x1e\x22", "\x22\x1e", 'UTF-16LE'),
172
                );
173
        }
174

    
175
        /**
176
         * Convert * to UTF-16
177
         * @dataProvider toUTF16
178
         */
179
        public function test_convert_UTF16($input, $expected, $encoding)
180
        {
181
                $encoding = SimplePie_Misc::encoding($encoding);
182
                $this->assertEquals($expected, SimplePie_Misc::change_encoding($input, $encoding, 'UTF-16'));
183
        }
184
        /**#@-*/
185

    
186
        public function test_nonexistant()
187
        {
188
                $this->assertFalse(SimplePie_Misc::change_encoding('', 'TESTENC', 'UTF-8'));
189
        }
190

    
191
        public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
192
        {
193
                if (is_string($expected))
194
                {
195
                        $expected = bin2hex($expected);
196
                }
197
                if (is_string($actual))
198
                {
199
                        $actual = bin2hex($actual);
200
                }
201
                parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
202
        }
203
}
204

    
205
class Mock_Misc extends SimplePie_Misc
206
{
207
        public static function __callStatic($name, $args)
208
        {
209
                return call_user_func_array(array('SimplePie_Misc', $name), $args);
210
        }
211
}