MapperTest.php 7.5 KB

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