[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/webservice/xmlrpc/tests/ -> lib_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for the XML-RPC web service.
  19   *
  20   * @package    webservice_xmlrpc
  21   * @category   test
  22   * @copyright  2015 Jun Pataleta <jun@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/webservice/xmlrpc/lib.php');
  30  
  31  /**
  32   * Unit tests for the XML-RPC web service.
  33   *
  34   * @package    webservice_xmlrpc
  35   * @category   test
  36   * @copyright  2015 Jun Pataleta <jun@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class webservice_xmlrpc_test extends advanced_testcase {
  40  
  41      /**
  42       * Setup.
  43       */
  44      public function setUp() {
  45          $this->resetAfterTest();
  46  
  47          // All tests require xmlrpc. Skip tests, if xmlrpc is not installed.
  48          if (!function_exists('xmlrpc_decode')) {
  49              $this->markTestSkipped('XMLRPC is not installed.');
  50          }
  51      }
  52  
  53      /**
  54       * Test for array response.
  55       */
  56      public function test_client_with_array_response() {
  57          global $CFG;
  58  
  59          $client = new webservice_xmlrpc_client_mock('/webservice/xmlrpc/server.php', 'anytoken');
  60          $mockresponse = file_get_contents($CFG->dirroot . '/webservice/xmlrpc/tests/fixtures/array_response.xml');
  61          $client->set_mock_response($mockresponse);
  62          $result = $client->call('testfunction');
  63          $this->assertEquals(xmlrpc_decode($mockresponse), $result);
  64      }
  65  
  66      /**
  67       * Test for value response.
  68       */
  69      public function test_client_with_value_response() {
  70          global $CFG;
  71  
  72          $client = new webservice_xmlrpc_client_mock('/webservice/xmlrpc/server.php', 'anytoken');
  73          $mockresponse = file_get_contents($CFG->dirroot . '/webservice/xmlrpc/tests/fixtures/value_response.xml');
  74          $client->set_mock_response($mockresponse);
  75          $result = $client->call('testfunction');
  76          $this->assertEquals(xmlrpc_decode($mockresponse), $result);
  77      }
  78  
  79      /**
  80       * Test for fault response.
  81       */
  82      public function test_client_with_fault_response() {
  83          global $CFG;
  84  
  85          $client = new webservice_xmlrpc_client_mock('/webservice/xmlrpc/server.php', 'anytoken');
  86          $mockresponse = file_get_contents($CFG->dirroot . '/webservice/xmlrpc/tests/fixtures/fault_response.xml');
  87          $client->set_mock_response($mockresponse);
  88          $this->expectException('moodle_exception');
  89          $client->call('testfunction');
  90      }
  91  }
  92  
  93  /**
  94   * Class webservice_xmlrpc_client_mock.
  95   *
  96   * Mock class that returns the processed XML-RPC response.
  97   *
  98   * @package    webservice_xmlrpc
  99   * @category   test
 100   * @copyright  2015 Jun Pataleta <jun@moodle.com>
 101   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 102   */
 103  class webservice_xmlrpc_client_mock extends webservice_xmlrpc_client {
 104  
 105      /** @var string The mock XML-RPC response string.  */
 106      private $mockresponse;
 107  
 108      /**
 109       * XML-RPC mock response setter.
 110       *
 111       * @param string $mockresponse
 112       */
 113      public function set_mock_response($mockresponse) {
 114          $this->mockresponse = $mockresponse;
 115      }
 116  
 117      /**
 118       * Since the call method uses download_file_content and it is hard to make an actual call to a web service,
 119       * we'll just have to simulate the receipt of the response from the server using the mock response so we
 120       * can test the processing result of this method.
 121       *
 122       * @param string $functionname the function name
 123       * @param array $params the parameters of the function
 124       * @return mixed The decoded XML RPC response.
 125       * @throws moodle_exception
 126       */
 127      public function call($functionname, $params = array()) {
 128          // Get the response.
 129          $response = $this->mockresponse;
 130  
 131          // This is the part of the code in webservice_xmlrpc_client::call() what we would like to test.
 132          // Decode the response.
 133          $result = xmlrpc_decode($response);
 134          if (is_array($result) && xmlrpc_is_fault($result)) {
 135              throw new moodle_exception($result['faultString']);
 136          }
 137  
 138          return $result;
 139      }
 140  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1