File: Xmlrpc_test.php

package info (click to toggle)
php-codeigniter-framework 3.1.13%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,228 kB
  • sloc: php: 37,178; xml: 205; makefile: 138; python: 66; sh: 65
file content (130 lines) | stat: -rw-r--r-- 3,067 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

class Xmlrpc_test extends CI_TestCase {

	protected $input;
	protected $input_lib_raw_stream;
	protected $method_param = '';

	public function set_up()
	{
		$security = new Mock_Core_Security('UTF-8');
		$this->input = new Mock_Core_Input($security, new Mock_Core_Utf8());

		$this->input_lib_raw_stream = new ReflectionProperty($this->input, '_raw_input_stream');
		$this->input_lib_raw_stream->setAccessible(TRUE);

		$this->ci_instance_var('input', $this->input);
		$this->ci_instance_var('security', $security);
	}

	// --------------------------------------------------------------------

	public function test_xmlrpc_client()
	{
		$xmlrpc = new Mock_Libraries_Xmlrpc();
		$xmlrpc->server('http://rpc.test/');
		$xmlrpc->method('testcontroller.test');

		$request = array('My Blog', 'http://www.myrpc.com/test/');
		$message = 'test'.time();
		$xml_response = $this->xml_response($message);
		$xmlrpc->client->mock_response = "HTTP/1.1 200 OK\r\nContent-Type: text/xml\r\nContent-Length: ".strlen($xml_response)."\r\n\r\n$xml_response";

		// Perform in the same request multiple calls
		for ($attempt = 1; $attempt <= 2; $attempt++)
		{
			$xmlrpc->request($request);

			$this->assertTrue($xmlrpc->send_request());

			$response = $xmlrpc->display_response();

			$this->assertEquals('theuser', $response['name']);
			$this->assertEquals(123435, $response['member_id']);
			$this->assertEquals($message, $response['request']);
		}
	}

	// --------------------------------------------------------------------

	public function test_xmlrpc_server()
	{
		$xmlrpcs = new Mock_Libraries_Xmlrpcs();

		$config['functions']['Testmethod'] = array('function' => __CLASS__.'.mock_method_new_entry');
		$config['object'] = $this;

		$xmlrpcs->initialize($config);

		$_SERVER['REQUEST_METHOD'] = 'POST';
		$this->input_lib_raw_stream->setValue($this->input, $this->xml_request());

		$xmlrpcs->serve();

		$this->assertEquals('Test', $this->method_param);
	}

	// --------------------------------------------------------------------

	/**
	 * @param XML_RPC_Message $param
	 */
	public function mock_method_new_entry($param)
	{
		$this->method_param = $param->params[0]->scalarval();

		return new XML_RPC_Response(new XML_RPC_Values(true, 'boolean'));
	}

	// --------------------------------------------------------------------

	private function xml_response($message)
	{
		return '<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>name</name>
<value>
<string>theuser</string>
</value>
</member>
<member>
<name>member_id</name>
<value>
<int>123435</int>
</value>
</member>
<member>
<name>request</name>
<value>
<string>'.$message.'</string>
</value>
</member>
</struct></value>
</param>
</params>
</methodResponse>';
	}

	// --------------------------------------------------------------------

	public function xml_request()
	{
		return '<?xml version="1.0"?>
<methodCall>
<methodName>Testmethod</methodName>
<params>
<param>
<value>
<string>Test</string>
</value>
</param>
</params>
</methodCall>';
	}
}