1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Tests JSON-RPC server availability.
16: *
17: * @category Jyxo
18: * @package Jyxo_Beholder
19: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
20: * @license https://github.com/jyxo/php/blob/master/license.txt
21: * @author Jan Pěček
22: * @author Jaroslav Hanslík
23: */
24: class Jyxo_Beholder_TestCase_JsonRpc extends Jyxo_Beholder_TestCase
25: {
26: /**
27: * JSON-RPC server URL.
28: *
29: * @var string
30: */
31: private $url;
32:
33: /**
34: * Tested method.
35: *
36: * @var string
37: */
38: private $method;
39:
40: /**
41: * Method parameters.
42: *
43: * @var string
44: */
45: private $params = array();
46:
47: /**
48: * Timeout.
49: *
50: * @var integer
51: */
52: private $timeout;
53:
54: /**
55: * Constructor.
56: *
57: * @param string $description Test description
58: * @param string $url Server URL
59: * @param string $method Tested method
60: * @param array $params Method parameters
61: * @param integer $timeout Timeout
62: */
63: public function __construct($description, $url, $method, array $params, $timeout = 2)
64: {
65: parent::__construct($description);
66:
67: $this->url = (string) $url;
68: $this->method = (string) $method;
69: $this->params = $params;
70: $this->timeout = (int) $timeout;
71: }
72:
73: /**
74: * Performs the test.
75: *
76: * @return Jyxo_Beholder_Result
77: */
78: public function run()
79: {
80: // The json extension is required
81: if (!extension_loaded('json')) {
82: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::NOT_APPLICABLE, 'Extension json missing');
83: }
84:
85: // The curl extension is required
86: if (!extension_loaded('curl')) {
87: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::NOT_APPLICABLE, 'Extension curl missing');
88: }
89:
90: // The Jyxo_Rpc_Json_Client class is required
91: if (!class_exists('Jyxo_Rpc_Json_Client')) {
92: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::NOT_APPLICABLE, 'Class Jyxo_Rpc_Json_Client missing');
93: }
94:
95: // Creates a client
96: $rpc = new Jyxo_Rpc_Json_Client();
97: $rpc->setUrl($this->url)
98: ->setTimeout($this->timeout);
99:
100: // Sends the request
101: try {
102: $rpc->send($this->method, $this->params);
103: } catch (Exception $e) {
104: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::FAILURE, $this->url);
105: }
106:
107: // OK
108: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::SUCCESS, $this->url);
109: }
110: }
111: