Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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_Serializer_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/Serializer.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_Serializer_Objects_TestCase extends PHPUnit_Framework_TestCase {
34
 
35
    private $options = array(
36
        XML_SERIALIZER_OPTION_INDENT     => '',
37
        XML_SERIALIZER_OPTION_LINEBREAKS => '',
38
    );
39
 
40
    public static function main() {
41
        $suite  = new PHPUnit_Framework_TestSuite('XML_Serializer_Objects_TestCase');
42
        $result = PHPUnit_TextUI_TestRunner::run($suite);
43
    }
44
 
45
    protected function setUp() {}
46
 
47
    protected function tearDown() {}
48
 
49
   /**
50
    * Test serializing an object without any properties
51
    */
52
    public function testEmptyObject()
53
    {
54
        $s = new XML_Serializer($this->options);
55
        $s->serialize(new stdClass());
56
        $this->assertEquals('<stdClass />', $s->getSerializedData());
57
    }
58
 
59
   /**
60
    * Test serializing a simple object
61
    */
62
    public function testSimpleObject()
63
    {
64
        $obj = new stdClass();
65
        $obj->foo = 'bar';
66
        $s = new XML_Serializer($this->options);
67
        $s->serialize($obj);
68
        $this->assertEquals('<stdClass><foo>bar</foo></stdClass>', $s->getSerializedData());
69
    }
70
 
71
   /**
72
    * Test serializing a nested object
73
    */
74
    public function testNestedObject()
75
    {
76
        $obj = new stdClass();
77
        $obj->foo = new stdClass();
78
        $obj->foo->bar = 'nested';
79
        $s = new XML_Serializer($this->options);
80
        $s->serialize($obj);
81
        $this->assertEquals('<stdClass><foo><bar>nested</bar></foo></stdClass>', $s->getSerializedData());
82
    }
83
 
84
   /**
85
    * Test serializing an object, that supports __sleep
86
    */
87
    public function testSleep()
88
    {
89
        $obj = new MyClass('foo', 'bar');
90
        $s = new XML_Serializer($this->options);
91
        $s->serialize($obj);
92
        $this->assertEquals('<MyClass><foo>foo</foo></MyClass>', $s->getSerializedData());
93
    }
94
 
95
}
96
 
97
class MyClass
98
{
99
    var $foo;
100
    var $bar;
101
 
102
    public function MyClass($foo, $bar)
103
    {
104
        $this->foo = $foo;
105
        $this->bar = $bar;
106
    }
107
 
108
    public function __sleep()
109
    {
110
        return array('foo');
111
    }
112
}
113
 
114
/**
115
 * PHPUnit main() hack
116
 * "Call class::main() if this source file is executed directly."
117
 */
118
if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Objects_TestCase::main') {
119
    XML_Serializer_Objects_TestCase::main();
120
}
121
?>