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