Passed
Push — master ( 8ff19d...02b7f4 )
by Gaetano
05:49
created

EncoderTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A set_up() 0 6 2
A testEncodeArray() 0 12 1
A testLatin15InternalEncoding() 0 27 3
A testEncodeDate() 0 5 1
A testAutoCoDec() 0 17 1
A tear_down() 0 9 4
A testEncodeRecursive() 0 4 1
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 EncoderTest extends PhpXmlRpc_PolyfillTestCase
19
{
20
    public $args = array();
21
22
    protected function set_up()
23
    {
24
        $this->args = argParser::getArgs();
25
        // hide parsing errors unless in debug mode
26
        if ($this->args['DEBUG'] == 1)
27
            ob_start();
28
    }
29
30
    protected function tear_down()
31
    {
32
        if ($this->args['DEBUG'] != 1)
33
            return;
34
        $out = ob_get_clean();
35
        $status = $this->getStatus();
36
        if ($status == BaseTestRunner::STATUS_ERROR
37
            || $status == BaseTestRunner::STATUS_FAILURE) {
38
            echo $out;
39
        }
40
    }
41
42
    public function testEncodeArray()
43
    {
44
        $v = php_xmlrpc_encode(array());
45
        $this->assertEquals('array', $v->kindof());
46
47
        $r = range(1, 10);
48
        $v = php_xmlrpc_encode($r);
49
        $this->assertEquals('array', $v->kindof());
50
51
        $r['.'] = '...';
52
        $v = php_xmlrpc_encode($r);
53
        $this->assertEquals('struct', $v->kindof());
54
    }
55
56
    public function testEncodeDate()
57
    {
58
        $r = new DateTime();
59
        $v = php_xmlrpc_encode($r);
60
        $this->assertEquals('dateTime.iso8601', $v->scalartyp());
61
    }
62
63
    public function testEncodeRecursive()
64
    {
65
        $v = php_xmlrpc_encode(php_xmlrpc_encode('a simple string'));
66
        $this->assertEquals('scalar', $v->kindof());
67
    }
68
69
    public function testAutoCoDec()
70
    {
71
        $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
72
        $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
73
        $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
74
        //$keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
75
        $v1 = php_xmlrpc_encode($data, array('auto_dates'));
76
        $v2 = php_xmlrpc_decode_xml($v1->serialize());
77
        $this->assertEquals($v1, $v2);
78
        $r1 = new PhpXmlRpc\Response($v1);
79
        $r2 = php_xmlrpc_decode_xml($r1->serialize());
80
        $r2->serialize(); // needed to set internal member payload
81
        $this->assertEquals($r1, $r2);
82
        $m1 = new PhpXmlRpc\Request('hello dolly', array($v1));
83
        $m2 = php_xmlrpc_decode_xml($m1->serialize());
84
        $m2->serialize(); // needed to set internal member payload
85
        $this->assertEquals($m1, $m2);
86
    }
87
88
    public function testLatin15InternalEncoding()
89
    {
90
        if (!function_exists('mb_convert_encoding')) {
91
            $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
92
            return;
93
        }
94
95
        $string = chr(164);
96
        $e = new \PhpXmlRpc\Encoder();
97
98
        $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding;
99
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-15';
100
101
        $a = $e->decodeXml('<?xml version="1.0" encoding="US-ASCII" ?><value><string>&#8364;</string></value>');
102
        $this->assertEquals($string, $a->scalarVal());
0 ignored issues
show
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

102
        $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\Request. ( Ignorable by Annotation )

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

102
        $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...
103
104
        /// @todo it seems that old php versions can not automatically transform latin to utf8 upon xml parsing.
105
        ///       We should fix that, then re-enable this test
106
        if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
107
            $i = $e->decodeXml('<?xml version="1.0" encoding="ISO-8859-15" ?><value><string>' . $string . '</string></value>');
108
            $this->assertEquals($string, $i->scalarVal());
109
        }
110
111
        $u = $e->decodeXml('<?xml version="1.0" encoding="UTF-8" ?><value><string>€</string></value>');
112
        $this->assertEquals($string, $u->scalarVal());
113
114
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding;
115
    }
116
}
117