Passed
Push — master ( 06696a...b81870 )
by Gaetano
07:05
created

EncoderTests::testEncodeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
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 automatic encoding/decoding of php values into xmlrpc values (the Encoder class).
14
 *
15
 * @todo add tests for encoding options: 'encode_php_objs', 'auto_dates', 'null_extension' and 'extension_api'
16
 * @todo add tests for php_xmlrpc_decode options
17
 */
18
class EncoderTests extends PhpXmlRpc_PolyfillTestCase
19
{
20
    public $args = array();
21
22
    protected function set_up()
23
    {
24
        $this->args = argParser::getArgs();
25
        if ($this->args['DEBUG'] == 1)
26
            ob_start();
27
    }
28
29
    protected function tear_down()
30
    {
31
        if ($this->args['DEBUG'] != 1)
32
            return;
33
        $out = ob_get_clean();
34
        $status = $this->getStatus();
35
        if ($status == BaseTestRunner::STATUS_ERROR
36
            || $status == BaseTestRunner::STATUS_FAILURE) {
37
            echo $out;
38
        }
39
    }
40
41
    public function testEncodeArray()
42
    {
43
        $v = php_xmlrpc_encode(array());
44
        $this->assertEquals('array', $v->kindof());
45
46
        $r = range(1, 10);
47
        $v = php_xmlrpc_encode($r);
48
        $this->assertEquals('array', $v->kindof());
49
50
        $r['.'] = '...';
51
        $v = php_xmlrpc_encode($r);
52
        $this->assertEquals('struct', $v->kindof());
53
    }
54
55
    public function testEncodeDate()
56
    {
57
        $r = new DateTime();
58
        $v = php_xmlrpc_encode($r);
59
        $this->assertEquals('dateTime.iso8601', $v->scalartyp());
60
    }
61
62
    public function testEncodeRecursive()
63
    {
64
        $v = php_xmlrpc_encode(php_xmlrpc_encode('a simple string'));
65
        $this->assertEquals('scalar', $v->kindof());
66
    }
67
68
    public function testAutoCoDec()
69
    {
70
        $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
71
        $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
72
        $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
73
        //$keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
74
        $v1 = php_xmlrpc_encode($data, array('auto_dates'));
75
        $v2 = php_xmlrpc_decode_xml($v1->serialize());
76
        $this->assertEquals($v1, $v2);
77
        $r1 = new PhpXmlRpc\Response($v1);
78
        $r2 = php_xmlrpc_decode_xml($r1->serialize());
79
        $r2->serialize(); // needed to set internal member payload
80
        $this->assertEquals($r1, $r2);
81
        $m1 = new PhpXmlRpc\Request('hello dolly', array($v1));
82
        $m2 = php_xmlrpc_decode_xml($m1->serialize());
83
        $m2->serialize(); // needed to set internal member payload
84
        $this->assertEquals($m1, $m2);
85
    }
86
87
    public function testLatin15InternalEncoding()
88
    {
89
        if (!function_exists('mb_convert_encoding')) {
90
            $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
91
            return;
92
        }
93
94
        $string = chr(164);
95
        $e = new \PhpXmlRpc\Encoder();
96
97
        $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding;
98
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-15';
99
100
        $a = $e->decodeXml('<?xml version="1.0" encoding="US-ASCII" ?><value><string>&#8364;</string></value>');
101
        $this->assertEquals($string, $a->scalarVal());
0 ignored issues
show
Bug introduced by
The method scalarVal() does not exist on PhpXmlRpc\Request. ( Ignorable by Annotation )

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

101
        $this->assertEquals($string, $a->/** @scrutinizer ignore-call */ scalarVal());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method scalarVal() does not exist on PhpXmlRpc\Response. ( Ignorable by Annotation )

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

101
        $this->assertEquals($string, $a->/** @scrutinizer ignore-call */ scalarVal());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
103
        $i = $e->decodeXml('<?xml version="1.0" encoding="ISO-8859-15" ?><value><string>' . $string . '</string></value>');
104
        $this->assertEquals($string, $i->scalarVal());
105
106
        $u = $e->decodeXml('<?xml version="1.0" encoding="UTF-8" ?><value><string>€</string></value>');
107
        $this->assertEquals($string, $u->scalarVal());
108
109
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding;
110
    }
111
}
112