1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class Jyxo_Beholder_TestCase_Mysql extends Jyxo_Beholder_TestCase
24: {
25: 26: 27: 28: 29:
30: private $query;
31:
32: 33: 34: 35: 36:
37: private $database;
38:
39: 40: 41: 42: 43:
44: private $host;
45:
46: 47: 48: 49: 50:
51: private $user;
52:
53: 54: 55: 56: 57:
58: private $password;
59:
60: 61: 62: 63: 64:
65: private $port;
66:
67: 68: 69: 70: 71:
72: private $timeout;
73:
74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public function __construct($description, $query, $database, $host = 'localhost', $user = '', $password = '', $port = 3306, $timeout = 2)
87: {
88: parent::__construct($description);
89:
90: $this->query = (string) $query;
91: $this->database = (string) $database;
92: $this->host = (string) $host;
93: $this->user = (string) $user;
94: $this->password = (string) $password;
95: $this->port = (int) $port;
96: $this->timeout = (int) $timeout;
97: }
98:
99: 100: 101: 102: 103:
104: public function run()
105: {
106:
107: if (!extension_loaded('mysqli')) {
108: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::NOT_APPLICABLE, 'Extension mysqli missing');
109: }
110:
111:
112: $description = sprintf('%s@%s:%s/%s', $this->user, $this->host, $this->port, $this->database);
113:
114:
115: $db = mysqli_init();
116: if (!$db) {
117: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::FAILURE, sprintf('Connection error %s', $description));
118: }
119: if (false === mysqli_options($db, MYSQLI_OPT_CONNECT_TIMEOUT, $this->timeout)) {
120: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::FAILURE, sprintf('Connection error %s', $description));
121: }
122: if (false === mysqli_real_connect($db, $this->host, $this->user, $this->password, $this->database, $this->port)) {
123: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::FAILURE, sprintf('Connection error %s', $description));
124: }
125:
126:
127: $result = mysqli_query($db, $this->query);
128: if (false === $result) {
129: mysqli_close($db);
130: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::FAILURE, sprintf('Query error %s', $description));
131: }
132: mysqli_free_result($result);
133: mysqli_close($db);
134:
135:
136: return new Jyxo_Beholder_Result(Jyxo_Beholder_Result::SUCCESS, $description);
137: }
138: }
139: