Passed
Push — master ( 248e0b...37ab4f )
by Gaetano
09:47
created

ValueTests   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 91
c 2
b 0
f 1
dl 0
loc 167
rs 10
wmc 24

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddScalarToStruct() 0 6 1
A testMinusOneString() 0 7 1
A testAddStructToStruct() 0 8 1
A testAddArrayToArray() 0 6 1
A set_up() 0 5 2
A tear_down() 0 9 4
A testStringInt() 0 5 1
A testMinusOneInt() 0 5 1
A testUTF8IntString() 0 5 1
A testArrayAccess() 0 27 4
A testLocale() 0 16 3
A testStructMemExists() 0 7 1
A testDate() 0 8 1
A testBigXML() 0 16 2
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());
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

69
        $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...
70
        $this->assertEquals(1, $r);
71
        $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...
72
        $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

72
        $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...
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());
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

79
        $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...
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 testDate()
98
    {
99
        $tz = date_default_timezone_get();
100
        date_default_timezone_set('UTC');
101
        $v = new xmlrpcval(86401, 'dateTime.iso8601');
102
        $s = $v->serialize();
103
        $this->assertequals("<value><dateTime.iso8601>19700102T00:00:01</dateTime.iso8601></value>\n", $s);
104
        date_default_timezone_set($tz);
105
    }
106
107
    public function testStructMemExists()
108
    {
109
        $v = new xmlrpcval(array('hello' => new xmlrpcval('world')), 'struct');
110
        $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

110
        $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...
111
        $this->assertequals(true, $b);
112
        $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

112
        $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...
113
        $this->assertequals(false, $b);
114
    }
115
116
    public function testLocale()
117
    {
118
        $locale = setlocale(LC_NUMERIC, 0);
119
        /// @todo on php 5.3/win setting locale to german does not seem to set decimal separator to comma...
120
        if (setlocale(LC_NUMERIC, 'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false) {
121
            $v = new xmlrpcval(1.1, 'double');
122
            if (strpos($v->scalarval(), ',') == 1) {
123
                $r = $v->serialize();
124
                $this->assertequals(false, strpos($r, ','));
125
                setlocale(LC_NUMERIC, $locale);
126
            } else {
127
                setlocale(LC_NUMERIC, $locale);
128
                $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
129
            }
130
        } else {
131
            $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
132
        }
133
    }
134
135
    public function testArrayAccess()
136
    {
137
        $v1 = new xmlrpcval(array(new xmlrpcval('one'), new xmlrpcval('two')), 'array');
138
        $this->assertequals(1, count($v1));
139
        $out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
140
141
        foreach($v1 as $key => $val)
142
        {
143
            $this->assertArrayHasKey($key, $out);
144
            $expected = $out[$key];
145
            if (gettype($expected) == 'array') {
146
                $this->assertequals('array', gettype($val));
147
            } else {
148
                $this->assertequals($expected, $val);
149
            }
150
        }
151
152
        $v2 = new \PhpXmlRpc\Value(array(new \PhpXmlRpc\Value('one'), new \PhpXmlRpc\Value('two')), 'array');
153
        $this->assertequals(2, count($v2));
154
        $out = array(array('key' => 0, 'value'  => 'object'), array('key' => 1, 'value'  => 'object'));
155
        $i = 0;
156
        foreach($v2 as $key => $val)
157
        {
158
            $expected = $out[$i];
159
            $this->assertequals($expected['key'], $key);
160
            $this->assertequals($expected['value'], gettype($val));
161
            $i++;
162
        }
163
    }
164
165
    /// @todo do not use \PhpXmlRpc\Encoder for this test
166
    function testBigXML()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
167
    {
168
        // nb: make sure that  the serialized xml corresponding to this is > 10MB in size
169
        $data = array();
170
        for ($i = 0; $i < 500000; $i++ ) {
171
            $data[] = 'hello world';
172
        }
173
174
        $encoder = new \PhpXmlRpc\Encoder();
175
        $val = $encoder->encode($data);
176
        $req = new \PhpXmlRpc\Request('test', array($val));
177
        $xml = $req->serialize();
178
        $parser = new \PhpXmlRpc\Helper\XMLParser();
179
        $parser->parse($xml);
180
181
        $this->assertequals(0, $parser->_xh['isf']);
182
    }
183
}
184