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: namespace Jyxo\Beholder\TestCase;
15:
16: /**
17: * Tests JSON-RPC server availability.
18: *
19: * @category Jyxo
20: * @package Jyxo\Beholder
21: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
22: * @license https://github.com/jyxo/php/blob/master/license.txt
23: * @author Jan Pěček
24: * @author Jaroslav Hanslík
25: */
26: class JsonRpc extends \Jyxo\Beholder\TestCase
27: {
28: /**
29: * JSON-RPC server URL.
30: *
31: * @var string
32: */
33: private $url;
34:
35: /**
36: * Tested method.
37: *
38: * @var string
39: */
40: private $method;
41:
42: /**
43: * Method parameters.
44: *
45: * @var string
46: */
47: private $params = array();
48:
49: /**
50: * Timeout.
51: *
52: * @var integer
53: */
54: private $timeout;
55:
56: /**
57: * Constructor.
58: *
59: * @param string $description Test description
60: * @param string $url Server URL
61: * @param string $method Tested method
62: * @param array $params Method parameters
63: * @param integer $timeout Timeout
64: */
65: public function __construct($description, $url, $method, array $params, $timeout = 2)
66: {
67: parent::__construct($description);
68:
69: $this->url = (string) $url;
70: $this->method = (string) $method;
71: $this->params = $params;
72: $this->timeout = (int) $timeout;
73: }
74:
75: /**
76: * Performs the test.
77: *
78: * @return \Jyxo\Beholder\Result
79: */
80: public function run()
81: {
82: // The json extension is required
83: if (!extension_loaded('json')) {
84: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension json missing');
85: }
86:
87: // The curl extension is required
88: if (!extension_loaded('curl')) {
89: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension curl missing');
90: }
91:
92: // The \Jyxo\Rpc\Json\Client class is required
93: if (!class_exists('\Jyxo\Rpc\Json\Client')) {
94: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Class \Jyxo\Rpc\Json\Client missing');
95: }
96:
97: // Creates a client
98: $rpc = new \Jyxo\Rpc\Json\Client();
99: $rpc->setUrl($this->url)
100: ->setTimeout($this->timeout);
101:
102: // Sends the request
103: try {
104: $rpc->send($this->method, $this->params);
105: } catch (\Exception $e) {
106: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $this->url);
107: }
108:
109: // OK
110: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $this->url);
111: }
112: }
113: