| [ Index ] |
PHP Cross Reference of JPSpan 0.4 (beta) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @version $Id: unserializer_php.test.php,v 1.2 2004/11/17 20:41:01 harryf Exp $ 4 * @package JPSpan 5 * @subpackage Tests 6 */ 7 8 /** 9 * Includes 10 */ 11 require_once ('../config.php'); 12 require_once JPSPAN . 'Unserializer.php'; 13 require_once JPSPAN . 'Types.php'; 14 require_once JPSPAN . 'Unserializer/PHP.php'; 15 16 /** 17 * @package JPSpan 18 * @subpackage Tests 19 */ 20 class Unserializer_TestClass {} 21 22 /** 23 * @package JPSpan 24 * @subpackage Tests 25 */ 26 class Unserializer_TestIllegalClass {} 27 28 /** 29 * @package JPSpan 30 * @subpackage Tests 31 */ 32 class TestOfJPSpan_Unserializer_PHP extends UnitTestCase { 33 34 function TestOfJPSpan_Unserializer_PHP() { 35 $this->UnitTestCase('TestOfJPSpan_Unserializer_PHP'); 36 } 37 38 function setUp() { 39 $this->U = & new JPSpan_Unserializer_PHP(); 40 } 41 42 function tearDown() { 43 unset($this->U); 44 } 45 46 function testUnserialize() { 47 $var = 'foo'; 48 $this->assertEqual($this->U->unserialize(serialize($var),'php'),$var); 49 } 50 51 function testUnserializeNotSerialized() { 52 $var = 'foo'; 53 $this->assertEqual($this->U->unserialize($var,'php'),$var); 54 } 55 56 function testUnserializeObject() { 57 $var = new JPSpan_Object(); 58 $var->x = 'foo'; 59 $var->y = 'bar'; 60 $this->assertEqual($this->U->unserialize(serialize($var),'php'),$var); 61 } 62 63 function testUnserializeError() { 64 $var = new JPSpan_Error(); 65 $var->setError(3000,'foo','bar'); 66 $error = $this->U->unserialize(serialize($var),'php'); 67 $this->assertEqual($error->code,3000); 68 $this->assertEqual($error->name,'foo'); 69 $this->assertEqual($error->message,'bar'); 70 } 71 72 function testUnserializeUnknownClass() { 73 $serialized = 'O:16:"TestUnknownClass":0:{}'; 74 $this->U->unserialize($serialized,'php'); 75 $this->assertErrorPattern('/testunknownclass/'); 76 } 77 78 function testIllegalClass() { 79 $obj = new Unserializer_TestIllegalClass(); 80 $this->assertFalse($this->U->unserialize(serialize($obj),'php')); 81 $this->assertErrorPattern('/Illegal type: unserializer_testillegalclass/'); 82 } 83 84 function testNestedIllegalClass() { 85 $obj = new JPSpan_Object(); 86 $obj->x = new Unserializer_TestIllegalClass(); 87 $this->assertFalse($this->U->unserialize(serialize($obj),'php')); 88 $this->assertErrorPattern('/Illegal type: unserializer_testillegalclass/'); 89 } 90 91 function testArrayNestedIllegalClass() { 92 $array = array(); 93 $array['x'] = new Unserializer_TestIllegalClass(); 94 $this->assertFalse($this->U->unserialize(serialize($array),'php')); 95 $this->assertErrorPattern('/Illegal type: unserializer_testillegalclass/'); 96 } 97 98 function testRegisteredClass() { 99 JPSpan_Unserializer::addType('Unserializer_TestClass'); 100 $obj = new Unserializer_TestClass(); 101 $this->assertEqual($this->U->unserialize(serialize($obj),'php'),$obj); 102 $this->assertNoErrors(); 103 } 104 105 function testFalse() { 106 $var = FALSE; 107 $this->assertIdentical($this->U->unserialize(serialize($var),'php'),$var); 108 } 109 110 function testNull() { 111 $var = NULL; 112 $this->assertIdentical($this->U->unserialize(serialize($var),'php'),$var); 113 } 114 115 function testTrue() { 116 $var = TRUE; 117 $this->assertIdentical($this->U->unserialize(serialize($var),'php'),$var); 118 } 119 120 function testEmptyString() { 121 $var = ''; 122 $this->assertIdentical($this->U->unserialize(serialize($var),'php'),$var); 123 } 124 125 function testZero() { 126 $var = 0; 127 $this->assertIdentical($this->U->unserialize(serialize($var),'php'),$var); 128 } 129 130 function testStringZero() { 131 $var = '0'; 132 $this->assertIdentical($this->U->unserialize(serialize($var),'php'),$var); 133 } 134 135 } 136 137 /** 138 * @package JPSpan 139 * @subpackage Tests 140 */ 141 class Unserializer_ClassParser_A {} 142 143 /** 144 * @package JPSpan 145 * @subpackage Tests 146 */ 147 class Unserializer_ClassParser_Bar {} 148 149 /** 150 * @package JPSpan 151 * @subpackage Tests 152 */ 153 class TestOfJPSpan_Unserializer_PHP_getClasses extends UnitTestCase { 154 155 function TestOfJPSpan_Unserializer_PHP_getClasses() { 156 $this->UnitTestCase('TestOfJPSpan_Unserializer_PHP_getClasses'); 157 } 158 159 function setUp() { 160 $this->U = & new JPSpan_Unserializer_PHP(); 161 } 162 163 function tearDown() { 164 unset($this->U); 165 } 166 167 function testSingleClass() { 168 $parsedClasses = array('Unserializer_ClassParser_A'); 169 $s = serialize(new Unserializer_ClassParser_A()); 170 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 171 } 172 173 function testArraySingleClass() { 174 $data = array(new Unserializer_ClassParser_A()); 175 $parsedClasses = array('Unserializer_ClassParser_A'); 176 $s = serialize($data); 177 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 178 } 179 180 function testArrayTwoClasses() { 181 $data = array( 182 new Unserializer_ClassParser_A(), 183 new Unserializer_ClassParser_Bar() 184 ); 185 $parsedClasses = array( 186 'Unserializer_ClassParser_A', 187 'Unserializer_ClassParser_Bar', 188 ); 189 $s = serialize($data); 190 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 191 } 192 193 function testArrayThreeClasses() { 194 $data = array( 195 new Unserializer_ClassParser_A(), 196 new stdClass(), 197 new Unserializer_ClassParser_Bar() 198 ); 199 $parsedClasses = array( 200 'Unserializer_ClassParser_A', 201 'stdClass', 202 'Unserializer_ClassParser_Bar', 203 ); 204 $s = serialize($data); 205 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 206 } 207 208 function testArrayThreeClassesWithString() { 209 $data = array( 210 new Unserializer_ClassParser_A(), 211 'foo;O:bar', 212 new stdClass(), 213 new Unserializer_ClassParser_Bar() 214 ); 215 $parsedClasses = array( 216 'Unserializer_ClassParser_A', 217 'stdClass', 218 'Unserializer_ClassParser_Bar', 219 ); 220 $s = serialize($data); 221 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 222 } 223 224 function testHashThreeClassesWithString() { 225 $data = array( 226 'foo;O:bar' => new Unserializer_ClassParser_A(), 227 'a;O:b', 228 new stdClass(), 229 'c;O:d' => new Unserializer_ClassParser_Bar() 230 ); 231 $parsedClasses = array( 232 'Unserializer_ClassParser_A', 233 'stdClass', 234 'Unserializer_ClassParser_Bar', 235 ); 236 $s = serialize($data); 237 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 238 } 239 240 function testString() { 241 $s = serialize('foo;O:3:"Foo"'); 242 $this->assertEqual($this->U->getClasses($s),array()); 243 } 244 245 function testStringNewLine() { 246 $s = serialize("foo;\nO:3:\"Foo\""); 247 $this->assertEqual($this->U->getClasses($s),array()); 248 } 249 250 function testObjectWithStrings() { 251 $obj = new Unserializer_ClassParser_A(); 252 $obj->x = 'foo;O:3:"Foo"'; 253 $obj->y = "foo;\nO:3:\"Foo\""; 254 $s = serialize($obj); 255 $parsedClasses = array('Unserializer_ClassParser_A'); 256 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 257 } 258 259 function testArrayObjectWithStrings() { 260 $obj = new Unserializer_ClassParser_A(); 261 $obj->x = 'foo;O:3:"Foo"'; 262 $obj->y = "foo;\nO:3:\"Foo\""; 263 $data = array ( 264 $obj, 265 'foos:3"Zee"'=>$obj, 266 FALSE, 267 NULL, 268 TRUE, 269 1 270 ); 271 $s = serialize($data); 272 $parsedClasses = array('Unserializer_ClassParser_A'); 273 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 274 } 275 276 function testArrayStringLikeString() { 277 $data = array ( 278 "foo;\nO:3:\"Foo\";\ns:3:\"bar\";", 279 new Unserializer_ClassParser_A(), 280 "foo;\nO:3:\"Foo\";\ns:3:\"bar\";" => new Unserializer_ClassParser_A(), 281 ); 282 $parsedClasses = array('Unserializer_ClassParser_A'); 283 $s = serialize($data); 284 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 285 } 286 287 function testClassWithSerializedName() { 288 $s = 'a:4:{i:0;O:26:"Unserializer_ClassParser_A":0:{}i:1;O:8:"O:3:"Foo"";}:0:{}i:2;s:24:"foo;O:3:"Foo";s:3:"bar";"i:4;O:28:"Unserializer_ClassParser_Bar":0:{}}'; 289 $parsedClasses = array( 290 'Unserializer_ClassParser_A', 291 'O:3:', 292 'Unserializer_ClassParser_Bar' 293 ); 294 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 295 } 296 297 function testNestedObjects() { 298 $obj = new Unserializer_ClassParser_A(); 299 $obj->x = new Unserializer_ClassParser_A(); 300 $obj->y = "O:4:\"Test\""; 301 $obj->z = new Unserializer_ClassParser_Bar(); 302 $obj->z->a = new stdClass(); 303 $parsedClasses = array( 304 0=>'Unserializer_ClassParser_A', 305 2=>'Unserializer_ClassParser_Bar', 306 3=>'stdClass' 307 ); 308 $s = serialize($obj); 309 $this->assertEqual(array_map('strtolower',$this->U->getClasses($s)),array_map('strtolower',$parsedClasses)); 310 } 311 312 } 313 314 /** 315 * Conditional test runner 316 */ 317 if (!defined('TEST_RUNNING')) { 318 define('TEST_RUNNING', true); 319 $test = & new GroupTest('Unserializer_PHP Tests'); 320 $test->addTestCase(new TestOfJPSpan_Unserializer_PHP()); 321 $test->addTestCase(new TestOfJPSpan_Unserializer_PHP_getClasses()); 322 $test->run(new HtmlReporter()); 323 } 324 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Nov 26 11:42:46 2004 | Cross-referenced by PHPXref 0.6 |