1
0

mappertest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 OCP\AppFramework\Db;
  23. use \OCP\IDBConnection;
  24. use Test\AppFramework\Db\MapperTestUtility;
  25. /**
  26. * @method integer getId()
  27. * @method void setId(integer $id)
  28. * @method string getEmail()
  29. * @method void setEmail(string $email)
  30. * @method string getPreName()
  31. * @method void setPreName(string $preName)
  32. */
  33. class Example extends Entity {
  34. protected $preName;
  35. protected $email;
  36. };
  37. class ExampleMapper extends Mapper {
  38. public function __construct(IDBConnection $db){ parent::__construct($db, 'table'); }
  39. public function find($table, $id){ return $this->findOneQuery($table, $id); }
  40. public function findOneEntity($table, $id){ return $this->findEntity($table, $id); }
  41. public function findAllEntities($table){ return $this->findEntities($table); }
  42. public function mapRow($row){ return $this->mapRowToEntity($row); }
  43. public function execSql($sql, $params){ return $this->execute($sql, $params); }
  44. }
  45. class MapperTest extends MapperTestUtility {
  46. /**
  47. * @var Mapper
  48. */
  49. private $mapper;
  50. protected function setUp(){
  51. parent::setUp();
  52. $this->mapper = new ExampleMapper($this->db);
  53. }
  54. public function testMapperShouldSetTableName(){
  55. $this->assertEquals('*PREFIX*table', $this->mapper->getTableName());
  56. }
  57. public function testFindQuery(){
  58. $sql = 'hi';
  59. $params = array('jo');
  60. $rows = array(
  61. array('hi')
  62. );
  63. $this->setMapperResult($sql, $params, $rows);
  64. $this->mapper->find($sql, $params);
  65. }
  66. public function testFindEntity(){
  67. $sql = 'hi';
  68. $params = array('jo');
  69. $rows = array(
  70. array('pre_name' => 'hi')
  71. );
  72. $this->setMapperResult($sql, $params, $rows, null, null, true);
  73. $this->mapper->findOneEntity($sql, $params);
  74. }
  75. public function testFindNotFound(){
  76. $sql = 'hi';
  77. $params = array('jo');
  78. $rows = array();
  79. $this->setMapperResult($sql, $params, $rows);
  80. $this->setExpectedException(
  81. '\OCP\AppFramework\Db\DoesNotExistException');
  82. $this->mapper->find($sql, $params);
  83. }
  84. public function testFindEntityNotFound(){
  85. $sql = 'hi';
  86. $params = array('jo');
  87. $rows = array();
  88. $this->setMapperResult($sql, $params, $rows, null, null, true);
  89. $this->setExpectedException(
  90. '\OCP\AppFramework\Db\DoesNotExistException');
  91. $this->mapper->findOneEntity($sql, $params);
  92. }
  93. public function testFindMultiple(){
  94. $sql = 'hi';
  95. $params = array('jo');
  96. $rows = array(
  97. array('jo'), array('ho')
  98. );
  99. $this->setMapperResult($sql, $params, $rows, null, null, true);
  100. $this->setExpectedException(
  101. '\OCP\AppFramework\Db\MultipleObjectsReturnedException');
  102. $this->mapper->find($sql, $params);
  103. }
  104. public function testFindEntityMultiple(){
  105. $sql = 'hi';
  106. $params = array('jo');
  107. $rows = array(
  108. array('jo'), array('ho')
  109. );
  110. $this->setMapperResult($sql, $params, $rows, null, null, true);
  111. $this->setExpectedException(
  112. '\OCP\AppFramework\Db\MultipleObjectsReturnedException');
  113. $this->mapper->findOneEntity($sql, $params);
  114. }
  115. public function testDelete(){
  116. $sql = 'DELETE FROM `*PREFIX*table` WHERE `id` = ?';
  117. $params = array(2);
  118. $this->setMapperResult($sql, $params, [], null, null, true);
  119. $entity = new Example();
  120. $entity->setId($params[0]);
  121. $this->mapper->delete($entity);
  122. }
  123. public function testCreate(){
  124. $this->db->expects($this->once())
  125. ->method('lastInsertId')
  126. ->with($this->equalTo('*PREFIX*table'))
  127. ->will($this->returnValue(3));
  128. $this->mapper = new ExampleMapper($this->db);
  129. $sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
  130. 'VALUES(?,?)';
  131. $params = array('john', 'my@email');
  132. $entity = new Example();
  133. $entity->setPreName($params[0]);
  134. $entity->setEmail($params[1]);
  135. $this->setMapperResult($sql, $params, [], null, null, true);
  136. $this->mapper->insert($entity);
  137. }
  138. public function testCreateShouldReturnItemWithCorrectInsertId(){
  139. $this->db->expects($this->once())
  140. ->method('lastInsertId')
  141. ->with($this->equalTo('*PREFIX*table'))
  142. ->will($this->returnValue(3));
  143. $this->mapper = new ExampleMapper($this->db);
  144. $sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
  145. 'VALUES(?,?)';
  146. $params = array('john', 'my@email');
  147. $entity = new Example();
  148. $entity->setPreName($params[0]);
  149. $entity->setEmail($params[1]);
  150. $this->setMapperResult($sql, $params);
  151. $result = $this->mapper->insert($entity);
  152. $this->assertEquals(3, $result->getId());
  153. }
  154. public function testAssocParameters() {
  155. $sql = 'test';
  156. $params = [':test' => 1, ':a' => 2];
  157. $this->setMapperResult($sql, $params);
  158. $this->mapper->execSql($sql, $params);
  159. }
  160. public function testUpdate(){
  161. $sql = 'UPDATE `*PREFIX*table` ' .
  162. 'SET ' .
  163. '`pre_name` = ?,'.
  164. '`email` = ? ' .
  165. 'WHERE `id` = ?';
  166. $params = array('john', 'my@email', 1);
  167. $entity = new Example();
  168. $entity->setPreName($params[0]);
  169. $entity->setEmail($params[1]);
  170. $entity->setId($params[2]);
  171. $this->setMapperResult($sql, $params, [], null, null, true);
  172. $this->mapper->update($entity);
  173. }
  174. public function testUpdateNoId(){
  175. $params = array('john', 'my@email');
  176. $entity = new Example();
  177. $entity->setPreName($params[0]);
  178. $entity->setEmail($params[1]);
  179. $this->setExpectedException('InvalidArgumentException');
  180. $this->mapper->update($entity);
  181. }
  182. public function testUpdateNothingChangedNoQuery(){
  183. $params = array('john', 'my@email');
  184. $entity = new Example();
  185. $entity->setId(3);
  186. $entity->setEmail($params[1]);
  187. $entity->resetUpdatedFields();
  188. $this->db->expects($this->never())
  189. ->method('prepare');
  190. $this->db->expects($this->never())
  191. ->method('prepare');
  192. $this->mapper->update($entity);
  193. }
  194. public function testMapRowToEntity(){
  195. $entity1 = $this->mapper->mapRow(array('pre_name' => 'test1', 'email' => 'test2'));
  196. $entity2 = new Example();
  197. $entity2->setPreName('test1');
  198. $entity2->setEmail('test2');
  199. $entity2->resetUpdatedFields();
  200. $this->assertEquals($entity2, $entity1);
  201. }
  202. public function testFindEntities(){
  203. $sql = 'hi';
  204. $rows = array(
  205. array('pre_name' => 'hi')
  206. );
  207. $entity = new Example();
  208. $entity->setPreName('hi');
  209. $entity->resetUpdatedFields();
  210. $this->setMapperResult($sql, array(), $rows, null, null, true);
  211. $result = $this->mapper->findAllEntities($sql);
  212. $this->assertEquals(array($entity), $result);
  213. }
  214. public function testFindEntitiesNotFound(){
  215. $sql = 'hi';
  216. $rows = array();
  217. $this->setMapperResult($sql, array(), $rows);
  218. $result = $this->mapper->findAllEntities($sql);
  219. $this->assertEquals(array(), $result);
  220. }
  221. public function testFindEntitiesMultiple(){
  222. $sql = 'hi';
  223. $rows = array(
  224. array('pre_name' => 'jo'), array('email' => 'ho')
  225. );
  226. $entity1 = new Example();
  227. $entity1->setPreName('jo');
  228. $entity1->resetUpdatedFields();
  229. $entity2 = new Example();
  230. $entity2->setEmail('ho');
  231. $entity2->resetUpdatedFields();
  232. $this->setMapperResult($sql, array(), $rows);
  233. $result = $this->mapper->findAllEntities($sql);
  234. $this->assertEquals(array($entity1, $entity2), $result);
  235. }
  236. }