1
0

MapperTest.php 7.4 KB

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