mappertestutility.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Db;
  23. /**
  24. * Simple utility class for testing mappers
  25. */
  26. abstract class MapperTestUtility extends \Test\TestCase {
  27. protected $db;
  28. private $query;
  29. private $queryAt;
  30. private $prepareAt;
  31. private $fetchAt;
  32. private $iterators;
  33. /**
  34. * Run this function before the actual test to either set or initialize the
  35. * db. After this the db can be accessed by using $this->db
  36. */
  37. protected function setUp(){
  38. parent::setUp();
  39. $this->db = $this->getMockBuilder(
  40. '\OCP\IDBConnection')
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->query = $this->getMock('\PDOStatement');
  44. $this->queryAt = 0;
  45. $this->prepareAt = 0;
  46. $this->iterators = [];
  47. $this->fetchAt = 0;
  48. }
  49. /**
  50. * Checks if an array is associative
  51. * @param array $array
  52. * @return bool true if associative
  53. */
  54. private function isAssocArray(array $array) {
  55. return array_values($array) !== $array;
  56. }
  57. /**
  58. * Returns the correct PDO constant based on the value type
  59. * @param $value
  60. * @return PDO constant
  61. */
  62. private function getPDOType($value) {
  63. switch (gettype($value)) {
  64. case 'integer':
  65. return \PDO::PARAM_INT;
  66. case 'boolean':
  67. return \PDO::PARAM_BOOL;
  68. default:
  69. return \PDO::PARAM_STR;
  70. }
  71. }
  72. /**
  73. * Create mocks and set expected results for database queries
  74. * @param string $sql the sql query that you expect to receive
  75. * @param array $arguments the expected arguments for the prepare query
  76. * method
  77. * @param array $returnRows the rows that should be returned for the result
  78. * of the database query. If not provided, it wont be assumed that fetch
  79. * will be called on the result
  80. */
  81. protected function setMapperResult($sql, $arguments=array(), $returnRows=array(),
  82. $limit=null, $offset=null, $expectClose=false){
  83. if($limit === null && $offset === null) {
  84. $this->db->expects($this->at($this->prepareAt))
  85. ->method('prepare')
  86. ->with($this->equalTo($sql))
  87. ->will(($this->returnValue($this->query)));
  88. } elseif($limit !== null && $offset === null) {
  89. $this->db->expects($this->at($this->prepareAt))
  90. ->method('prepare')
  91. ->with($this->equalTo($sql), $this->equalTo($limit))
  92. ->will(($this->returnValue($this->query)));
  93. } elseif($limit === null && $offset !== null) {
  94. $this->db->expects($this->at($this->prepareAt))
  95. ->method('prepare')
  96. ->with($this->equalTo($sql),
  97. $this->equalTo(null),
  98. $this->equalTo($offset))
  99. ->will(($this->returnValue($this->query)));
  100. } else {
  101. $this->db->expects($this->at($this->prepareAt))
  102. ->method('prepare')
  103. ->with($this->equalTo($sql),
  104. $this->equalTo($limit),
  105. $this->equalTo($offset))
  106. ->will(($this->returnValue($this->query)));
  107. }
  108. $this->iterators[] = new ArgumentIterator($returnRows);
  109. $iterators = $this->iterators;
  110. $fetchAt = $this->fetchAt;
  111. $this->query->expects($this->any())
  112. ->method('fetch')
  113. ->will($this->returnCallback(
  114. function() use ($iterators, $fetchAt){
  115. $iterator = $iterators[$fetchAt];
  116. $result = $iterator->next();
  117. if($result === false) {
  118. $fetchAt++;
  119. }
  120. $this->queryAt++;
  121. return $result;
  122. }
  123. ));
  124. if ($this->isAssocArray($arguments)) {
  125. foreach($arguments as $key => $argument) {
  126. $pdoConstant = $this->getPDOType($argument);
  127. $this->query->expects($this->at($this->queryAt))
  128. ->method('bindValue')
  129. ->with($this->equalTo($key),
  130. $this->equalTo($argument),
  131. $this->equalTo($pdoConstant));
  132. $this->queryAt++;
  133. }
  134. } else {
  135. $index = 1;
  136. foreach($arguments as $argument) {
  137. $pdoConstant = $this->getPDOType($argument);
  138. $this->query->expects($this->at($this->queryAt))
  139. ->method('bindValue')
  140. ->with($this->equalTo($index),
  141. $this->equalTo($argument),
  142. $this->equalTo($pdoConstant));
  143. $index++;
  144. $this->queryAt++;
  145. }
  146. }
  147. $this->query->expects($this->at($this->queryAt))
  148. ->method('execute')
  149. ->will($this->returnCallback(function($sql, $p=null, $o=null, $s=null) {
  150. }));
  151. $this->queryAt++;
  152. if ($expectClose) {
  153. $closing = $this->at($this->queryAt);
  154. } else {
  155. $closing = $this->any();
  156. }
  157. $this->query->expects($closing)->method('closeCursor');
  158. $this->queryAt++;
  159. $this->prepareAt++;
  160. $this->fetchAt++;
  161. }
  162. }
  163. class ArgumentIterator {
  164. private $arguments;
  165. public function __construct($arguments){
  166. $this->arguments = $arguments;
  167. }
  168. public function next(){
  169. $result = array_shift($this->arguments);
  170. if($result === null){
  171. return false;
  172. } else {
  173. return $result;
  174. }
  175. }
  176. }