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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Codeception\Module;
use Codeception\Lib\Interfaces\API;
use Codeception\Module as CodeceptionModule;
use Codeception\Lib\Framework;
use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleRequireException;
use Codeception\TestInterface;
/**
* Module for testing XMLRPC WebService.
*
* This module can be used either with frameworks or PHPBrowser.
* It tries to guess the framework is is attached to.
*
* Whether framework is used it operates via standard framework modules.
* Otherwise sends raw HTTP requests to url via PHPBrowser.
*
* ## Requirements
*
* * Module requires installed php_xmlrpc extension
*
* ## Status
*
* * Maintainer: **tiger-seo**
* * Stability: **beta**
* * Contact: tiger.seo@gmail.com
*
* ## Configuration
*
* * url *optional* - the url of api
*
* ## Public Properties
*
* * headers - array of headers going to be sent.
* * params - array of sent data
* * response - last response (string)
*
* @since 1.1.5
* @author tiger.seo@gmail.com
*/
class XMLRPC extends CodeceptionModule implements API
{
protected $config = ['url' => ""];
/**
* @var \Symfony\Component\BrowserKit\Client
*/
public $client = null;
public $is_functional = false;
public $headers = [];
public $params = [];
public $response = "";
public function _initialize()
{
if (!function_exists('xmlrpc_encode_request')) {
throw new ModuleRequireException(__CLASS__, "XMLRPC module requires installed php_xmlrpc extension");
}
parent::_initialize();
}
public function _before(TestInterface $test)
{
if (!$this->client) {
if (!strpos($this->config['url'], '://')) {
// not valid url
foreach ($this->getModules() as $module) {
if ($module instanceof Framework) {
$this->client = $module->client;
$this->is_functional = true;
break;
}
}
} else {
if (!$this->hasModule('PhpBrowser')) {
throw new ModuleConfigException(
__CLASS__,
"For XMLRPC testing via HTTP please enable PhpBrowser module"
);
}
$this->client = $this->getModule('PhpBrowser')->client;
}
if (!$this->client) {
throw new ModuleConfigException(
__CLASS__,
"Client for XMLRPC requests not initialized.\n"
. "Provide either PhpBrowser module, or a framework module which shares FrameworkInterface"
);
}
}
$this->headers = [];
$this->params = [];
$this->response = '';
$this->client->setServerParameters([]);
}
/**
* Sets HTTP header
*
* @param string $name
* @param string $value
*/
public function haveHttpHeader($name, $value)
{
$this->headers[$name] = $value;
}
/**
* Checks response code.
*
* @param $num
*/
public function seeResponseCodeIs($num)
{
\PHPUnit_Framework_Assert::assertEquals($num, $this->client->getInternalResponse()->getStatus());
}
/**
* Checks weather last response was valid XMLRPC.
* This is done with xmlrpc_decode function.
*
*/
public function seeResponseIsXMLRPC()
{
$result = xmlrpc_decode($this->response);
\PHPUnit_Framework_Assert::assertNotNull($result, 'Invalid response document returned from XmlRpc server');
}
/**
* Sends a XMLRPC method call to remote XMLRPC-server.
*
* @param string $methodName
* @param array $parameters
*/
public function sendXMLRPCMethodCall($methodName, $parameters = [])
{
if (!array_key_exists('Content-Type', $this->headers)) {
$this->headers['Content-Type'] = 'text/xml';
}
foreach ($this->headers as $header => $val) {
$this->client->setServerParameter("HTTP_$header", $val);
}
$url = $this->config['url'];
if (is_array($parameters)) {
$parameters = $this->scalarizeArray($parameters);
}
$requestBody = xmlrpc_encode_request($methodName, array_values($parameters));
$this->debugSection('Request', $url . PHP_EOL . $requestBody);
$this->client->request('POST', $url, [], [], [], $requestBody);
$this->response = $this->client->getInternalResponse()->getContent();
$this->debugSection('Response', $this->response);
}
}