| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Unit Tests for serializing arrays
|
|
|
4 |
*
|
|
|
5 |
* @package XML_Serializer
|
|
|
6 |
* @subpackage tests
|
|
|
7 |
* @author Stephan Schmidt <schst@php-tools.net>
|
|
|
8 |
* @author Chuck Burgess <ashnazg@php.net>
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* PHPUnit main() hack
|
|
|
13 |
*
|
|
|
14 |
* "Call class::main() if this source file is executed directly."
|
|
|
15 |
*/
|
|
|
16 |
if (!defined('PHPUnit_MAIN_METHOD')) {
|
|
|
17 |
define('PHPUnit_MAIN_METHOD', 'XML_Unserializer_Objects_TestCase::main');
|
|
|
18 |
}
|
|
|
19 |
require_once 'PHPUnit/Framework/TestCase.php';
|
|
|
20 |
require_once 'PHPUnit/Framework/TestSuite.php';
|
|
|
21 |
require_once 'PHPUnit/TextUI/TestRunner.php';
|
|
|
22 |
|
|
|
23 |
require_once 'XML/Unserializer.php';
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Unit Tests for serializing arrays
|
|
|
27 |
*
|
|
|
28 |
* @package XML_Serializer
|
|
|
29 |
* @subpackage tests
|
|
|
30 |
* @author Stephan Schmidt <schst@php-tools.net>
|
|
|
31 |
* @author Chuck Burgess <ashnazg@php.net>
|
|
|
32 |
*/
|
|
|
33 |
class XML_Unserializer_Objects_TestCase extends PHPUnit_Framework_TestCase {
|
|
|
34 |
|
|
|
35 |
public static function main() {
|
|
|
36 |
$suite = new PHPUnit_Framework_TestSuite('XML_Unserializer_Objects_TestCase');
|
|
|
37 |
$result = PHPUnit_TextUI_TestRunner::run($suite);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
protected function setUp() {}
|
|
|
41 |
|
|
|
42 |
protected function tearDown() {}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Test unserializing to a stdClass object
|
|
|
46 |
*/
|
|
|
47 |
public function testStdClass()
|
|
|
48 |
{
|
|
|
49 |
$u = new XML_Unserializer();
|
|
|
50 |
$u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object');
|
|
|
51 |
$xml = '<xml><foo>bar</foo></xml>';
|
|
|
52 |
$u->unserialize($xml);
|
|
|
53 |
$result = new stdClass();
|
|
|
54 |
$result->foo = 'bar';
|
|
|
55 |
$this->assertEquals($result, $u->getUnserializedData());
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Test unserializing to a custom class
|
|
|
60 |
*/
|
|
|
61 |
public function testDefaultClass()
|
|
|
62 |
{
|
|
|
63 |
$u = new XML_Unserializer();
|
|
|
64 |
$u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object');
|
|
|
65 |
$u->setOption(XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, 'Foo');
|
|
|
66 |
$xml = '<xml><foo>bar</foo></xml>';
|
|
|
67 |
$u->unserialize($xml);
|
|
|
68 |
$result = new Foo();
|
|
|
69 |
$result->foo = 'bar';
|
|
|
70 |
$this->assertEquals($result, $u->getUnserializedData());
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Test unserializing to a class based on the tag name
|
|
|
75 |
*/
|
|
|
76 |
public function testTagnameAsClass()
|
|
|
77 |
{
|
|
|
78 |
$u = new XML_Unserializer();
|
|
|
79 |
$u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object');
|
|
|
80 |
$u->setOption(XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, 'Foo');
|
|
|
81 |
$xml = '<foo><bar><boo>tomato</boo></bar></foo>';
|
|
|
82 |
$u->unserialize($xml);
|
|
|
83 |
$result = new Foo();
|
|
|
84 |
$result->bar = new Bar();
|
|
|
85 |
$result->bar->boo = 'tomato';
|
|
|
86 |
$this->assertEquals($result, $u->getUnserializedData());
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Test unserializing with a setter method
|
|
|
91 |
*/
|
|
|
92 |
public function testSetterMethod()
|
|
|
93 |
{
|
|
|
94 |
$u = new XML_Unserializer();
|
|
|
95 |
$u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object');
|
|
|
96 |
$u->setOption(XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, 'Foo');
|
|
|
97 |
$xml = '<SetterExample><foo>tomato</foo></SetterExample>';
|
|
|
98 |
$u->unserialize($xml);
|
|
|
99 |
$result = new SetterExample();
|
|
|
100 |
$result->setFoo('tomato');
|
|
|
101 |
$this->assertEquals($result, $u->getUnserializedData());
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
class Foo {}
|
|
|
107 |
|
|
|
108 |
class Bar {}
|
|
|
109 |
|
|
|
110 |
class SetterExample {
|
|
|
111 |
private $_hidden = null;
|
|
|
112 |
|
|
|
113 |
public function setFoo($foo)
|
|
|
114 |
{
|
|
|
115 |
$this->_hidden = $foo;
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* PHPUnit main() hack
|
|
|
121 |
* "Call class::main() if this source file is executed directly."
|
|
|
122 |
*/
|
|
|
123 |
if (PHPUnit_MAIN_METHOD == 'XML_Unserializer_Objects_TestCase::main') {
|
|
|
124 |
XML_Unserializer_Objects_TestCase::main();
|
|
|
125 |
}
|
|
|
126 |
?>
|