Skip to content

Commit

Permalink
Adds PHPUnit test for XML-RPC lib
Browse files Browse the repository at this point in the history
  • Loading branch information
gxgpet committed Feb 10, 2024
1 parent 7371a21 commit 562431e
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
130 changes: 130 additions & 0 deletions tests/codeigniter/libraries/Xmlrpc_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 CI_Input($security);

$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>';
}
}
1 change: 1 addition & 0 deletions tests/mocks/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function autoload($class)
'Upload',
'User_agent',
'Xmlrpc',
'Xmlrpcs',
'Zip'
);

Expand Down
33 changes: 33 additions & 0 deletions tests/mocks/libraries/xmlrpc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class Mock_Libraries_Xmlrpc extends CI_Xmlrpc {
public function server($url, $port = 80, $proxy = FALSE, $proxy_port = 8080)
{
$this->client = new Mock_Libraries_XML_RPC_Client('/', $url, $port, $proxy, $proxy_port);
}
}

class Mock_Libraries_XML_RPC_Client extends XML_RPC_Client {
public $mock_response = '';

/**
* @param XML_RPC_Message $msg
*/
public function sendPayload($msg)
{
if (empty($msg->payload))
{
$msg->createPayload();
}

$fp = fopen('php://memory', 'rw+');
fwrite($fp, $this->mock_response);
fseek($fp, 0);

$parsed = $msg->parseResponse($fp);
fclose($fp);

return $parsed;
}
}

23 changes: 23 additions & 0 deletions tests/mocks/libraries/xmlrpcs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class Mock_Libraries_Xmlrpcs extends CI_Xmlrpcs {
public $mock_payload = '';

/**
* Act as a XML-RPC server, but save the response into $mock_public property instead of making output of it
*/
public function serve()
{
$r = $this->parseRequest();

$payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n".$this->debug_msg.$r->prepare_response();

$this->mock_payload = "HTTP/1.1 200 OK\r\n";
$this->mock_payload .= "Content-Type: text/xml\r\n";
$this->mock_payload .= 'Content-Length: '.strlen($payload)."\r\n";

$this->mock_payload .= "\r\n";

$this->mock_payload .= $payload;
}
}

0 comments on commit 562431e

Please sign in to comment.