merge upstream phpxmlrpc
[plcapi.git] / php / phpxmlrpc / tests / 1ValueTest.php
1 <?php
2
3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4 include_once __DIR__ . '/../lib/xmlrpcs.inc';
5
6 include_once __DIR__ . '/parse_args.php';
7
8 include_once __DIR__ . '/PolyfillTestCase.php';
9
10 use PHPUnit\Runner\BaseTestRunner;
11
12 /**
13  * Tests involving the Value class
14  */
15 class ValueTests extends PhpXmlRpc_PolyfillTestCase
16 {
17     public $args = array();
18
19     protected function set_up()
20     {
21         $this->args = argParser::getArgs();
22         if ($this->args['DEBUG'] == 1)
23             ob_start();
24     }
25
26     protected function tear_down()
27     {
28         if ($this->args['DEBUG'] != 1)
29             return;
30         $out = ob_get_clean();
31         $status = $this->getStatus();
32         if ($status == BaseTestRunner::STATUS_ERROR
33             || $status == BaseTestRunner::STATUS_FAILURE) {
34             echo $out;
35         }
36     }
37
38     public function testMinusOneString()
39     {
40         $v = new xmlrpcval('-1');
41         $u = new xmlrpcval('-1', 'string');
42         $t = new xmlrpcval(-1, 'string');
43         $this->assertEquals($v->scalarval(), $u->scalarval());
44         $this->assertEquals($v->scalarval(), $t->scalarval());
45     }
46
47     /**
48      * This looks funny, and we might call it a bug. But we strive for 100 backwards compat...
49      */
50     public function testMinusOneInt()
51     {
52         $u = new xmlrpcval();
53         $v = new xmlrpcval(-1);
54         $this->assertEquals($u->scalarval(), $v->scalarval());
55     }
56
57     public function testAddScalarToStruct()
58     {
59         $v = new xmlrpcval(array('a' => 'b'), 'struct');
60         // use @ operator in case error_log gets on screen
61         $r = @$v->addscalar('c');
62         $this->assertEquals(0, $r);
63     }
64
65     public function testAddStructToStruct()
66     {
67         $v = new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
68         $r = $v->addstruct(array('b' => new xmlrpcval('c')));
69         $this->assertEquals(2, $v->structsize());
70         $this->assertEquals(1, $r);
71         $r = $v->addstruct(array('b' => new xmlrpcval('b')));
72         $this->assertEquals(2, $v->structsize());
73     }
74
75     public function testAddArrayToArray()
76     {
77         $v = new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
78         $r = $v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
79         $this->assertEquals(4, $v->arraysize());
80         $this->assertEquals(1, $r);
81     }
82
83     public function testUTF8IntString()
84     {
85         $v = new xmlrpcval(100, 'int');
86         $s = $v->serialize('UTF-8');
87         $this->assertequals("<value><int>100</int></value>\n", $s);
88     }
89
90     public function testStringInt()
91     {
92         $v = new xmlrpcval('hello world', 'int');
93         $s = $v->serialize();
94         $this->assertequals("<value><int>0</int></value>\n", $s);
95     }
96
97     public function testStructMemExists()
98     {
99         $v = new xmlrpcval(array('hello' => new xmlrpcval('world')), 'struct');
100         $b = $v->structmemexists('hello');
101         $this->assertequals(true, $b);
102         $b = $v->structmemexists('world');
103         $this->assertequals(false, $b);
104     }
105
106     public function testLocale()
107     {
108         $locale = setlocale(LC_NUMERIC, 0);
109         /// @todo on php 5.3/win setting locale to german does not seem to set decimal separator to comma...
110         if (setlocale(LC_NUMERIC, 'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false) {
111             $v = new xmlrpcval(1.1, 'double');
112             if (strpos($v->scalarval(), ',') == 1) {
113                 $r = $v->serialize();
114                 $this->assertequals(false, strpos($r, ','));
115                 setlocale(LC_NUMERIC, $locale);
116             } else {
117                 setlocale(LC_NUMERIC, $locale);
118                 $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
119             }
120         } else {
121             $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
122         }
123     }
124
125     public function testArrayAccess()
126     {
127         $v1 = new xmlrpcval(array(new xmlrpcval('one'), new xmlrpcval('two')), 'array');
128         $this->assertequals(1, count($v1));
129         $out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
130
131         foreach($v1 as $key => $val)
132         {
133             $this->assertArrayHasKey($key, $out);
134             $expected = $out[$key];
135             if (gettype($expected) == 'array') {
136                 $this->assertequals('array', gettype($val));
137             } else {
138                 $this->assertequals($expected, $val);
139             }
140         }
141
142         $v2 = new \PhpXmlRpc\Value(array(new \PhpXmlRpc\Value('one'), new \PhpXmlRpc\Value('two')), 'array');
143         $this->assertequals(2, count($v2));
144         $out = array(array('key' => 0, 'value'  => 'object'), array('key' => 1, 'value'  => 'object'));
145         $i = 0;
146         foreach($v2 as $key => $val)
147         {
148             $expected = $out[$i];
149             $this->assertequals($expected['key'], $key);
150             $this->assertequals($expected['value'], gettype($val));
151             $i++;
152         }
153     }
154
155     /// @todo do not use \PhpXmlRpc\Encoder for this test
156     function testBigXML()
157     {
158         // nb: make sure that  the serialized xml corresponding to this is > 10MB in size
159         $data = array();
160         for ($i = 0; $i < 500000; $i++ ) {
161             $data[] = 'hello world';
162         }
163
164         $encoder = new \PhpXmlRpc\Encoder();
165         $val = $encoder->encode($data);
166         $req = new \PhpXmlRpc\Request('test', array($val));
167         $xml = $req->serialize();
168         $parser = new \PhpXmlRpc\Helper\XMLParser();
169         $parser->parse($xml);
170
171         $this->assertequals(0, $parser->_xh['isf']);
172     }
173 }