PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/classes/class.TestMySQLDAO.php

https://github.com/unruthless/ThinkUp
PHP | 155 lines | 93 code | 17 blank | 45 comment | 1 complexity | c79bf70b248190c9bd8b4b7f62b8671e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/tests/classes/class.TestMySQLDAO.php
  5. *
  6. * Copyright (c) 2009-2011 Christoffer Viken, Gina Trapani, Mark Wilkie
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. * @author Christoffer Viken <christoffer[at]viken[dot]me>
  24. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  25. * @author Mark Wilkie <mark[at]bitterpill[dot]org>
  26. * @license http://www.gnu.org/licenses/gpl.html
  27. * @copyright 2009-2011 Christoffer Viken, Gina Trapani, Mark Wilkie
  28. *
  29. * MySQL TestDAO implementation class for TestOfPDODAO and TestOfDAOFactory
  30. */
  31. class TestMySQLDAO extends PDODAO implements TestDAO {
  32. // test select query using $stmt directly...
  33. public function getUserCount($min_id, $user_name) {
  34. $sql = "select * from #prefix#users where id > :id and user_name like :user_name order by user_id";
  35. $stmt = $this->execute($sql, array(':id'=>$min_id, ':user_name'=>'%'.$user_name.'%'));
  36. $result = $stmt->fetchAll();
  37. return $result;
  38. }
  39. // test insert and parent getInsertCount()
  40. public function insertDataGetCount($name, $id) {
  41. $sql = "insert into #prefix#test_table (test_name, test_id) values (:test_name, :test_id)";
  42. $stmt = $this->execute($sql, array(':test_name'=>$name, ':test_id'=>$id));
  43. return $this->getInsertCount($stmt);
  44. }
  45. // test insert and parent getInsertId()
  46. public function insertDataGetId($name, $id) {
  47. $sql = "insert into #prefix#test_table (test_name, test_id) values (:test_name, :test_id)";
  48. $stmt = $this->execute($sql, array(':test_name'=>$name, ':test_id'=>$id));
  49. return $this->getInsertId($stmt);
  50. }
  51. // test multi insert and parent getInsertCount()
  52. public function insertMultiDataGetCount($insert_data) {
  53. $sql = "insert into #prefix#test_table (test_name, test_id) values ";
  54. $values = null;
  55. $i = 0;
  56. $binds = array();
  57. foreach ($insert_data as $data) {
  58. if (!is_null($values)) {
  59. $values .= ',';
  60. }
  61. $values .= '(:name'.$i.', :id'.$i.')';
  62. $binds[':name'.$i] = $data[0];
  63. $binds[':id'.$i] = $data[1];
  64. $i++;
  65. }
  66. $sql .= $values;
  67. $stmt = $this->execute($sql, $binds);
  68. return $this->getInsertCount($stmt);
  69. }
  70. // test bad sql
  71. public function badSql() {
  72. $sql = "select na form mooo";
  73. $stmt = $this->execute($sql);
  74. }
  75. // test bad binds...
  76. public function badBinds() {
  77. $sql = "select test_name from #prefix#test_table where test_id = :test_id";
  78. $stmt = $this->execute($sql);
  79. }
  80. // test update test_name record
  81. public function update($name, $id) {
  82. $sql = "update #prefix#test_table set test_name = :test_name where test_id = :test_id";
  83. $stmt = $this->execute($sql, array(':test_name'=>$name, ':test_id'=>$id));
  84. return $this->getUpdateCount($stmt);
  85. }
  86. // test update test_name record(s)
  87. public function updateMulti($name, $id) {
  88. $sql = "update #prefix#test_table set test_name = :test_name where test_id > :test_id";
  89. $stmt = $this->execute($sql, array(':test_name'=>$name, ':test_id'=>$id));
  90. return $this->getUpdateCount($stmt);
  91. }
  92. // test select one record
  93. public function selectRecord($id) {
  94. $sql = "select id, test_name, test_id from #prefix#test_table where test_id = :test_id";
  95. $stmt = $this->execute($sql, array(':test_id'=>$id));
  96. return $this->getDataRowAsObject($stmt, 'TestData');
  97. }
  98. //select one record array
  99. public function selectRecordAsArray($id) {
  100. $sql = "select id, test_name, test_id from #prefix#test_table where test_id = :test_id";
  101. $stmt = $this->execute($sql, array(':test_id'=>$id));
  102. return $this->getDataRowAsArray($stmt);
  103. }
  104. // test select many records
  105. public function selectRecords($id) {
  106. $sql = "select id, test_name, test_id from #prefix#test_table where test_id >= :test_id order by test_id";
  107. $stmt = $this->execute($sql, array(':test_id'=>$id));
  108. return $this->getDataRowsAsObjects($stmt, 'TestData');
  109. }
  110. // test select many records with limit
  111. public function selectRecordsWithLimit($limit) {
  112. $sql = "select id, test_name, test_id from #prefix#test_table order by test_id LIMIT :limit";
  113. $stmt = $this->execute($sql, array(':limit' => $limit));
  114. return $this->getDataRowsAsObjects($stmt, 'TestData');
  115. }
  116. // test select many records as array
  117. public function selectRecordsAsArrays($id) {
  118. $sql = "select id, test_name, test_id from #prefix#test_table where test_id >= :test_id order by test_id";
  119. $stmt = $this->execute($sql, array(':test_id'=>$id));
  120. return $this->getDataRowsAsArrays($stmt, 'TestData');
  121. }
  122. // test delete records
  123. public function delete($id) {
  124. $sql = "delete from #prefix#test_table where test_id > :test_id";
  125. $stmt = $this->execute($sql, array(':test_id'=>$id));
  126. return $this->getUpdateCount($stmt);
  127. }
  128. // test record exist
  129. public function isExisting($id) {
  130. $sql = "select id, test_name, test_id from #prefix#test_table where test_id = :test_id";
  131. $stmt = $this->execute($sql, array(':test_id'=>$id));
  132. return $this->getDataIsReturned($stmt);
  133. }
  134. //test BoolToDB
  135. public function testBoolToDB($val) {
  136. return $this->convertBoolToDB($val);
  137. }
  138. }