MapperTest.php 7.5 KB

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