/vendor/codeception/codeception/src/Codeception/Module/XMLRPC.php

https://gitlab.com/merial/WETE_Ryhma3 · PHP · 164 lines · 87 code · 19 blank · 58 comment · 10 complexity · 1e07275fefe7ce146bc06fc66b1c1664 MD5 · raw file

  1. <?php
  2. namespace Codeception\Module;
  3. use Codeception\Module as CodeceptionModule;
  4. use Codeception\Lib\Framework;
  5. use Codeception\Exception\ModuleConfigException;
  6. use Codeception\Exception\ModuleRequireException;
  7. use Codeception\TestCase;
  8. /**
  9. * Module for testing XMLRPC WebService.
  10. *
  11. * This module can be used either with frameworks or PHPBrowser.
  12. * It tries to guess the framework is is attached to.
  13. *
  14. * Whether framework is used it operates via standard framework modules.
  15. * Otherwise sends raw HTTP requests to url via PHPBrowser.
  16. *
  17. * ## Requirements
  18. *
  19. * * Module requires installed php_xmlrpc extension
  20. *
  21. * ## Status
  22. *
  23. * * Maintainer: **tiger-seo**
  24. * * Stability: **beta**
  25. * * Contact: tiger.seo@gmail.com
  26. *
  27. * ## Configuration
  28. *
  29. * * url *optional* - the url of api
  30. *
  31. * ## Public Properties
  32. *
  33. * * headers - array of headers going to be sent.
  34. * * params - array of sent data
  35. * * response - last response (string)
  36. *
  37. * @since 1.1.5
  38. * @author tiger.seo@gmail.com
  39. */
  40. class XMLRPC extends CodeceptionModule
  41. {
  42. protected $config = ['url' => ""];
  43. /**
  44. * @var \Symfony\Component\BrowserKit\Client
  45. */
  46. public $client = null;
  47. public $is_functional = false;
  48. public $headers = [];
  49. public $params = [];
  50. public $response = "";
  51. public function _initialize()
  52. {
  53. if (!function_exists('xmlrpc_encode_request')) {
  54. throw new ModuleRequireException(__CLASS__, "XMLRPC module requires installed php_xmlrpc extension");
  55. }
  56. parent::_initialize();
  57. }
  58. public function _before(TestCase $test)
  59. {
  60. if (!$this->client) {
  61. if (!strpos($this->config['url'], '://')) {
  62. // not valid url
  63. foreach ($this->getModules() as $module) {
  64. if ($module instanceof Framework) {
  65. $this->client = $module->client;
  66. $this->is_functional = true;
  67. break;
  68. }
  69. }
  70. } else {
  71. if (!$this->hasModule('PhpBrowser')) {
  72. throw new ModuleConfigException(
  73. __CLASS__,
  74. "For XMLRPC testing via HTTP please enable PhpBrowser module"
  75. );
  76. }
  77. $this->client = $this->getModule('PhpBrowser')->client;
  78. }
  79. if (!$this->client) {
  80. throw new ModuleConfigException(
  81. __CLASS__,
  82. "Client for XMLRPC requests not initialized.\n"
  83. . "Provide either PhpBrowser module, or a framework module which shares FrameworkInterface"
  84. );
  85. }
  86. }
  87. $this->headers = [];
  88. $this->params = [];
  89. $this->response = '';
  90. $this->client->setServerParameters([]);
  91. }
  92. /**
  93. * Sets HTTP header
  94. *
  95. * @param string $name
  96. * @param string $value
  97. */
  98. public function haveHttpHeader($name, $value)
  99. {
  100. $this->headers[$name] = $value;
  101. }
  102. /**
  103. * Checks response code.
  104. *
  105. * @param $num
  106. */
  107. public function seeResponseCodeIs($num)
  108. {
  109. \PHPUnit_Framework_Assert::assertEquals($num, $this->client->getInternalResponse()->getStatus());
  110. }
  111. /**
  112. * Checks weather last response was valid XMLRPC.
  113. * This is done with xmlrpc_decode function.
  114. *
  115. */
  116. public function seeResponseIsXMLRPC()
  117. {
  118. $result = xmlrpc_decode($this->response);
  119. \PHPUnit_Framework_Assert::assertNotNull($result, 'Invalid response document returned from XmlRpc server');
  120. }
  121. /**
  122. * Sends a XMLRPC method call to remote XMLRPC-server.
  123. *
  124. * @param string $methodName
  125. * @param array $parameters
  126. */
  127. public function sendXMLRPCMethodCall($methodName, $parameters = [])
  128. {
  129. if (!array_key_exists('Content-Type', $this->headers)) {
  130. $this->headers['Content-Type'] = 'text/xml';
  131. }
  132. foreach ($this->headers as $header => $val) {
  133. $this->client->setServerParameter("HTTP_$header", $val);
  134. }
  135. $url = $this->config['url'];
  136. if (is_array($parameters)) {
  137. $parameters = $this->scalarizeArray($parameters);
  138. }
  139. $requestBody = xmlrpc_encode_request($methodName, array_values($parameters));
  140. $this->debugSection('Request', $url . PHP_EOL . $requestBody);
  141. $this->client->request('POST', $url, [], [], [], $requestBody);
  142. $this->response = $this->client->getInternalResponse()->getContent();
  143. $this->debugSection('Response', $this->response);
  144. }
  145. }