VPN licensing server
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

313 lines
11KB

  1. <?php
  2. class ObjectTest extends SimpleTest {
  3. public $mdb;
  4. function __construct() {
  5. $this->mdb = new MeekroDB();
  6. foreach ($this->mdb->tableList() as $table) {
  7. $this->mdb->query("DROP TABLE %b", $table);
  8. }
  9. }
  10. function test_1_create_table() {
  11. $this->mdb->query("CREATE TABLE `accounts` (
  12. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  13. `username` VARCHAR( 255 ) NOT NULL ,
  14. `password` VARCHAR( 255 ) NULL ,
  15. `age` INT NOT NULL DEFAULT '10',
  16. `height` DOUBLE NOT NULL DEFAULT '10.0',
  17. `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
  18. ) ENGINE = InnoDB");
  19. }
  20. function test_1_5_empty_table() {
  21. $counter = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts");
  22. $this->assert($counter === strval(0));
  23. $row = $this->mdb->queryFirstRow("SELECT * FROM accounts");
  24. $this->assert($row === null);
  25. $field = $this->mdb->queryFirstField("SELECT * FROM accounts");
  26. $this->assert($field === null);
  27. $field = $this->mdb->queryOneField('nothere', "SELECT * FROM accounts");
  28. $this->assert($field === null);
  29. $column = $this->mdb->queryFirstColumn("SELECT * FROM accounts");
  30. $this->assert(is_array($column) && count($column) === 0);
  31. $column = $this->mdb->queryOneColumn('nothere', "SELECT * FROM accounts"); //TODO: is this what we want?
  32. $this->assert(is_array($column) && count($column) === 0);
  33. }
  34. function test_2_insert_row() {
  35. $this->mdb->insert('accounts', array(
  36. 'username' => 'Abe',
  37. 'password' => 'hello'
  38. ));
  39. $this->assert($this->mdb->affectedRows() === 1);
  40. $counter = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts");
  41. $this->assert($counter === strval(1));
  42. }
  43. function test_3_more_inserts() {
  44. $this->mdb->insert('`accounts`', array(
  45. 'username' => 'Bart',
  46. 'password' => 'hello',
  47. 'age' => 15,
  48. 'height' => 10.371
  49. ));
  50. $dbname = $this->mdb->dbName;
  51. $this->mdb->insert("`$dbname`.`accounts`", array(
  52. 'username' => 'Charlie\'s Friend',
  53. 'password' => 'goodbye',
  54. 'age' => 30,
  55. 'height' => 155.23,
  56. 'favorite_word' => null,
  57. ));
  58. $this->assert($this->mdb->insertId() === 3);
  59. $counter = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts");
  60. $this->assert($counter === strval(3));
  61. $password = $this->mdb->queryFirstField("SELECT password FROM accounts WHERE favorite_word IS NULL");
  62. $this->assert($password === 'goodbye');
  63. $this->mdb->param_char = '###';
  64. $bart = $this->mdb->queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
  65. array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
  66. $this->assert($bart['username'] === 'Bart');
  67. $this->mdb->param_char = '%';
  68. $charlie_password = $this->mdb->queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
  69. array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
  70. $this->assert($charlie_password === 'goodbye');
  71. $charlie_password = $this->mdb->queryOneField('password', "SELECT * FROM accounts WHERE username IN %ls AND username = %s",
  72. array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
  73. $this->assert($charlie_password === 'goodbye');
  74. $passwords = $this->mdb->queryFirstColumn("SELECT password FROM accounts WHERE username=%s", 'Bart');
  75. $this->assert(count($passwords) === 1);
  76. $this->assert($passwords[0] === 'hello');
  77. $username = $password = $age = null;
  78. list($age, $username, $password) = $this->mdb->queryOneList("SELECT age,username,password FROM accounts WHERE username=%s", 'Bart');
  79. $this->assert($username === 'Bart');
  80. $this->assert($password === 'hello');
  81. $this->assert($age == 15);
  82. $mysqli_result = $this->mdb->queryRaw("SELECT * FROM accounts WHERE favorite_word IS NULL");
  83. $this->assert($mysqli_result instanceof MySQLi_Result);
  84. $row = $mysqli_result->fetch_assoc();
  85. $this->assert($row['password'] === 'goodbye');
  86. $this->assert($mysqli_result->fetch_assoc() === null);
  87. }
  88. function test_4_query() {
  89. $results = $this->mdb->query("SELECT * FROM accounts WHERE username=%s", 'Charlie\'s Friend');
  90. $this->assert(count($results) === 1);
  91. $this->assert($results[0]['age'] == 30 && $results[0]['password'] == 'goodbye');
  92. $results = $this->mdb->query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
  93. $this->assert(count($results) === 2);
  94. $columnlist = $this->mdb->columnList('accounts');
  95. $this->assert(count($columnlist) === 6);
  96. $this->assert($columnlist[0] === 'id');
  97. $this->assert($columnlist[4] === 'height');
  98. $tablelist = $this->mdb->tableList();
  99. $this->assert(count($tablelist) === 1);
  100. $this->assert($tablelist[0] === 'accounts');
  101. $tablelist = null;
  102. $tablelist = $this->mdb->tableList($this->mdb->dbName);
  103. $this->assert(count($tablelist) === 1);
  104. $this->assert($tablelist[0] === 'accounts');
  105. }
  106. function test_4_1_query() {
  107. $this->mdb->insert('accounts', array(
  108. 'username' => 'newguy',
  109. 'password' => $this->mdb->sqleval("REPEAT('blah', %i)", '3'),
  110. 'age' => $this->mdb->sqleval('171+1'),
  111. 'height' => 111.15
  112. ));
  113. $row = $this->mdb->queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah');
  114. $this->assert($row['username'] === 'newguy');
  115. $this->assert($row['age'] === '172');
  116. $this->mdb->update('accounts', array(
  117. 'password' => $this->mdb->sqleval("REPEAT('blah', %i)", 4),
  118. 'favorite_word' => null,
  119. ), 'username=%s', 'newguy');
  120. $row = null;
  121. $row = $this->mdb->queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
  122. $this->assert($row['password'] === 'blahblahblahblah');
  123. $this->assert($row['favorite_word'] === null);
  124. $this->mdb->query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
  125. $this->assert($this->mdb->affectedRows() === 1);
  126. }
  127. function test_4_2_delete() {
  128. $this->mdb->insert('accounts', array(
  129. 'username' => 'gonesoon',
  130. 'password' => 'something',
  131. 'age' => 61,
  132. 'height' => 199.194
  133. ));
  134. $ct = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194);
  135. $this->assert(intval($ct) === 1);
  136. $ct = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s1 AND height=%d0 AND height=%d", 199.194, 'gonesoon');
  137. $this->assert(intval($ct) === 1);
  138. $this->mdb->delete('accounts', 'username=%s AND age=%i AND height=%d', 'gonesoon', '61', '199.194');
  139. $this->assert($this->mdb->affectedRows() === 1);
  140. $ct = $this->mdb->queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', '199.194');
  141. $this->assert(intval($ct) === 0);
  142. }
  143. function test_4_3_insertmany() {
  144. $ins[] = array(
  145. 'username' => '1ofmany',
  146. 'password' => 'something',
  147. 'age' => 23,
  148. 'height' => 190.194
  149. );
  150. $ins[] = array(
  151. 'password' => 'somethingelse',
  152. 'username' => '2ofmany',
  153. 'age' => 25,
  154. 'height' => 190.194
  155. );
  156. $this->mdb->insert('accounts', $ins);
  157. $this->assert($this->mdb->affectedRows() === 2);
  158. $rows = $this->mdb->query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194);
  159. $this->assert(count($rows) === 2);
  160. $this->assert($rows[0]['username'] === '1ofmany');
  161. $this->assert($rows[0]['age'] === '23');
  162. $this->assert($rows[1]['age'] === '25');
  163. $this->assert($rows[1]['password'] === 'somethingelse');
  164. $this->assert($rows[1]['username'] === '2ofmany');
  165. }
  166. function test_5_insert_blobs() {
  167. $this->mdb->query("CREATE TABLE `storedata` (
  168. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  169. `picture` BLOB
  170. ) ENGINE = InnoDB");
  171. $smile = file_get_contents('smile1.jpg');
  172. $this->mdb->insert('storedata', array(
  173. 'picture' => $smile,
  174. ));
  175. $this->mdb->query("INSERT INTO storedata (picture) VALUES (%s)", $smile);
  176. $getsmile = $this->mdb->queryFirstField("SELECT picture FROM storedata WHERE id=1");
  177. $getsmile2 = $this->mdb->queryFirstField("SELECT picture FROM storedata WHERE id=2");
  178. $this->assert($smile === $getsmile);
  179. $this->assert($smile === $getsmile2);
  180. }
  181. function test_6_insert_ignore() {
  182. $this->mdb->insertIgnore('accounts', array(
  183. 'id' => 1, //duplicate primary key
  184. 'username' => 'gonesoon',
  185. 'password' => 'something',
  186. 'age' => 61,
  187. 'height' => 199.194
  188. ));
  189. }
  190. function test_7_insert_update() {
  191. $this->mdb->insertUpdate('accounts', array(
  192. 'id' => 2, //duplicate primary key
  193. 'username' => 'gonesoon',
  194. 'password' => 'something',
  195. 'age' => 61,
  196. 'height' => 199.194
  197. ), 'age = age + %i', 1);
  198. $this->assert($this->mdb->affectedRows() === 2); // a quirk of MySQL, even though only 1 row was updated
  199. $result = $this->mdb->query("SELECT * FROM accounts WHERE age = %i", 16);
  200. $this->assert(count($result) === 1);
  201. $this->assert($result[0]['height'] === '10.371');
  202. $this->mdb->insertUpdate('accounts', array(
  203. 'id' => 2, //duplicate primary key
  204. 'username' => 'blahblahdude',
  205. 'age' => 233,
  206. 'height' => 199.194
  207. ));
  208. $result = $this->mdb->query("SELECT * FROM accounts WHERE age = %i", 233);
  209. $this->assert(count($result) === 1);
  210. $this->assert($result[0]['height'] === '199.194');
  211. $this->assert($result[0]['username'] === 'blahblahdude');
  212. $this->mdb->insertUpdate('accounts', array(
  213. 'id' => 2, //duplicate primary key
  214. 'username' => 'gonesoon',
  215. 'password' => 'something',
  216. 'age' => 61,
  217. 'height' => 199.194
  218. ), array(
  219. 'age' => 74,
  220. ));
  221. $result = $this->mdb->query("SELECT * FROM accounts WHERE age = %i", 74);
  222. $this->assert(count($result) === 1);
  223. $this->assert($result[0]['height'] === '199.194');
  224. $this->assert($result[0]['username'] === 'blahblahdude');
  225. $multiples[] = array(
  226. 'id' => 3, //duplicate primary key
  227. 'username' => 'gonesoon',
  228. 'password' => 'something',
  229. 'age' => 61,
  230. 'height' => 199.194
  231. );
  232. $multiples[] = array(
  233. 'id' => 1, //duplicate primary key
  234. 'username' => 'gonesoon',
  235. 'password' => 'something',
  236. 'age' => 61,
  237. 'height' => 199.194
  238. );
  239. $this->mdb->insertUpdate('accounts', $multiples, array('age' => 914));
  240. $this->assert($this->mdb->affectedRows() === 4);
  241. $result = $this->mdb->query("SELECT * FROM accounts WHERE age=914 ORDER BY id ASC");
  242. $this->assert(count($result) === 2);
  243. $this->assert($result[0]['username'] === 'Abe');
  244. $this->assert($result[1]['username'] === 'Charlie\'s Friend');
  245. $this->mdb->query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
  246. $this->assert($this->mdb->affectedRows() === 1);
  247. }
  248. }
  249. ?>