Passed
Push — master ( 02b7f4...c1cbf5 )
by Gaetano
07:18
created

ValueTest::testAddScalarToStruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
include_once __DIR__ . '/LogAwareTestCase.php';
4
5
/**
6
 * Tests involving the Value class.
7
 * NB: these tests do not involve the parsing of xml into Value objects - look in 03ParsingTest for that
8
 */
9
class ValueTest extends PhpXmlRpc_LogAwareTestCase
10
{
11
    public function testMinusOneString()
12
    {
13
        $v = new xmlrpcval('-1');
14
        $u = new xmlrpcval('-1', 'string');
15
        $t = new xmlrpcval(-1, 'string');
16
        $this->assertEquals($v->scalarval(), $u->scalarval());
17
        $this->assertEquals($v->scalarval(), $t->scalarval());
18
    }
19
20
    /**
21
     * This looks funny, and we might call it a bug. But we strive for 100 backwards compat...
22
     */
23
    public function testMinusOneInt()
24
    {
25
        $u = new xmlrpcval();
26
        $v = new xmlrpcval(-1);
27
        $this->assertEquals($u->scalarval(), $v->scalarval());
28
    }
29
30
    public function testAddScalarToStruct()
31
    {
32
        $v = new xmlrpcval(array('a' => 'b'), 'struct');
33
        // use @ operator in case error_log gets on screen
34
        $r = @$v->addscalar('c');
35
        $this->assertEquals(0, $r);
36
    }
37
38
    public function testAddStructToStruct()
39
    {
40
        $v = new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
41
        $r = $v->addstruct(array('b' => new xmlrpcval('c')));
42
        $this->assertEquals(2, $v->structsize());
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structsize() has been deprecated: use count() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

42
        $this->assertEquals(2, /** @scrutinizer ignore-deprecated */ $v->structsize());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
43
        $this->assertEquals(1, $r);
44
        $r = $v->addstruct(array('b' => new xmlrpcval('b')));
0 ignored issues
show
Unused Code introduced by
The assignment to $r is dead and can be removed.
Loading history...
45
        $this->assertEquals(2, $v->structsize());
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structsize() has been deprecated: use count() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

45
        $this->assertEquals(2, /** @scrutinizer ignore-deprecated */ $v->structsize());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
46
    }
47
48
    public function testAddArrayToArray()
49
    {
50
        $v = new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
51
        $r = $v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
52
        $this->assertEquals(4, $v->arraysize());
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::arraysize() has been deprecated: use count() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
        $this->assertEquals(4, /** @scrutinizer ignore-deprecated */ $v->arraysize());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
53
        $this->assertEquals(1, $r);
54
    }
55
56
    /// @todo does this test check something useful at all?
57
    public function testUTF8IntString()
58
    {
59
        $v = new xmlrpcval(100, 'int');
60
        $s = $v->serialize('UTF-8');
61
        $this->assertequals("<value><int>100</int></value>\n", $s);
62
    }
63
64
    public function testUTF8String()
65
    {
66
        $sendstring = 'κόσμε'; // Greek word 'kosme'
67
        $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
68
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
69
        $f = new xmlrpcval($sendstring, 'string');
70
        $v = $f->serialize();
71
        $this->assertEquals("<value><string>&#954;&#8057;&#963;&#956;&#949;</string></value>\n", $v);
72
        $v = $f->serialize('UTF-8');
73
        $this->assertEquals("<value><string>$sendstring</string></value>\n", $v);
74
        $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
75
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
76
    }
77
78
    public function testStringInt()
79
    {
80
        $v = new xmlrpcval('hello world', 'int');
81
        $s = $v->serialize();
82
        $this->assertequals("<value><int>0</int></value>\n", $s);
83
    }
84
85
    public function testDate()
86
    {
87
        $tz = date_default_timezone_get();
88
        date_default_timezone_set('UTC');
89
90
        $ts = 86401;
91
        $dt = new DateTime('@86401');
92
93
        $v = new xmlrpcval(86401, 'dateTime.iso8601');
94
        $s = $v->serialize();
95
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
96
97
        $v = new xmlrpcval($dt, 'dateTime.iso8601');
98
        $s = $v->serialize();
99
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
100
101
        $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($ts), 'dateTime.iso8601');
102
        $s = $v->serialize();
103
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
104
105
        $v = new xmlrpcval(\PhpXmlRpc\Helper\Date::iso8601Encode($dt), 'dateTime.iso8601');
106
        $s = $v->serialize();
107
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
108
109
        date_default_timezone_set($tz);
110
    }
111
112
    /// @todo is this included in the above?
113
    public function testDateTime()
114
    {
115
        $time = time();
116
        $t1 = new xmlrpcval($time, 'dateTime.iso8601');
117
        $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
118
        $this->assertEquals($t1->serialize(), $t2->serialize());
119
        $datetime = new DateTime();
120
        $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
121
        $this->assertEquals($t1->serialize(), $t3->serialize());
122
    }
123
124
    public function testStructMemExists()
125
    {
126
        $v = new xmlrpcval(array('hello' => new xmlrpcval('world')), 'struct');
127
        $b = $v->structmemexists('hello');
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structmemexists() has been deprecated: use array access, e.g. isset($val[$key]) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

127
        $b = /** @scrutinizer ignore-deprecated */ $v->structmemexists('hello');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
128
        $this->assertequals(true, $b);
129
        $b = $v->structmemexists('world');
0 ignored issues
show
Deprecated Code introduced by
The function PhpXmlRpc\Value::structmemexists() has been deprecated: use array access, e.g. isset($val[$key]) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
        $b = /** @scrutinizer ignore-deprecated */ $v->structmemexists('world');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
130
        $this->assertequals(false, $b);
131
    }
132
133
    public function testLocale()
134
    {
135
        $locale = setlocale(LC_NUMERIC, 0);
136
        /// @todo on php 5.3/win, possibly later versions, setting locale to german does not seem to set decimal separator to comma...
137
        if (setlocale(LC_NUMERIC, 'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false) {
138
            $v = new xmlrpcval(1.1, 'double');
139
            if (strpos($v->scalarval(), ',') == 1) {
140
                $r = $v->serialize();
141
                $this->assertequals(false, strpos($r, ','));
142
                setlocale(LC_NUMERIC, $locale);
143
            } else {
144
                setlocale(LC_NUMERIC, $locale);
145
                $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
146
            }
147
        } else {
148
            $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
149
        }
150
    }
151
152
    public function testArrayAccess()
153
    {
154
        $v1 = new xmlrpcval(array(new xmlrpcval('one'), new xmlrpcval('two')), 'array');
155
        $this->assertequals(1, count($v1));
156
        $out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
157
158
        foreach($v1 as $key => $val)
159
        {
160
            $this->assertArrayHasKey($key, $out);
161
            $expected = $out[$key];
162
            if (gettype($expected) == 'array') {
163
                $this->assertequals('array', gettype($val));
164
            } else {
165
                $this->assertequals($expected, $val);
166
            }
167
        }
168
169
        $v2 = new \PhpXmlRpc\Value(array(new \PhpXmlRpc\Value('one'), new \PhpXmlRpc\Value('two')), 'array');
170
        $this->assertequals(2, count($v2));
171
        $out = array(array('key' => 0, 'value'  => 'object'), array('key' => 1, 'value'  => 'object'));
172
        $i = 0;
173
        foreach($v2 as $key => $val)
174
        {
175
            $expected = $out[$i];
176
            $this->assertequals($expected['key'], $key);
177
            $this->assertequals($expected['value'], gettype($val));
178
            $i++;
179
        }
180
    }
181
182
    /// @todo do not use \PhpXmlRpc\Encoder for this test
183
    public function testBigXML()
184
    {
185
        // nb: make sure that  the serialized xml corresponding to this is > 10MB in size
186
        $data = array();
187
        for ($i = 0; $i < 500000; $i++ ) {
188
            $data[] = 'hello world';
189
        }
190
191
        $encoder = new \PhpXmlRpc\Encoder();
192
        $val = $encoder->encode($data);
193
        $req = new \PhpXmlRpc\Request('test', array($val));
194
        $xml = $req->serialize();
195
        $parser = new \PhpXmlRpc\Helper\XMLParser();
196
        $parser->parse($xml);
197
198
        $this->assertequals(0, $parser->_xh['isf']);
199
    }
200
201
    public function testLatin15InternalEncoding()
202
    {
203
        if (!function_exists('mb_convert_encoding')) {
204
            $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
205
            return;
206
        }
207
208
        $string = chr(164);
209
        $v = new \PhpXmlRpc\Value($string);
210
211
        $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding;
212
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-15';
213
214
        $this->assertEquals("<value><string>&#8364;</string></value>", trim($v->serialize('US-ASCII')));
215
        $this->assertEquals("<value><string>$string</string></value>", trim($v->serialize('ISO-8859-15')));
216
        $this->assertEquals("<value><string>€</string></value>", trim($v->serialize('UTF-8')));
217
218
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding;
219
    }
220
}
221