2014-04-19 16:56:16 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud - App Framework
|
|
|
|
*
|
|
|
|
* @author Bernhard Posselt
|
|
|
|
* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-04-08 15:47:23 +03:00
|
|
|
namespace Test\AppFramework\Db;
|
2014-04-19 16:56:16 +04:00
|
|
|
|
2018-01-24 20:10:16 +03:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\AppFramework\Db\Entity;
|
|
|
|
use OCP\AppFramework\Db\Mapper;
|
2018-01-24 20:10:16 +03:00
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IDBConnection;
|
2014-04-19 16:56:16 +04:00
|
|
|
|
2014-04-20 04:55:21 +04:00
|
|
|
/**
|
|
|
|
* @method integer getId()
|
|
|
|
* @method void setId(integer $id)
|
|
|
|
* @method string getEmail()
|
|
|
|
* @method void setEmail(string $email)
|
|
|
|
* @method string getPreName()
|
|
|
|
* @method void setPreName(string $preName)
|
|
|
|
*/
|
2014-04-19 16:56:16 +04:00
|
|
|
class Example extends Entity {
|
2014-08-27 15:28:04 +04:00
|
|
|
protected $preName;
|
|
|
|
protected $email;
|
2014-04-19 16:56:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ExampleMapper extends Mapper {
|
2020-04-10 15:19:56 +03:00
|
|
|
public function __construct(IDBConnection $db) {
|
|
|
|
parent::__construct($db, 'table');
|
|
|
|
}
|
|
|
|
public function find($table, $id) {
|
|
|
|
return $this->findOneQuery($table, $id);
|
|
|
|
}
|
|
|
|
public function findOneEntity($table, $id) {
|
|
|
|
return $this->findEntity($table, $id);
|
|
|
|
}
|
|
|
|
public function findAllEntities($table) {
|
|
|
|
return $this->findEntities($table);
|
|
|
|
}
|
|
|
|
public function mapRow($row) {
|
|
|
|
return $this->mapRowToEntity($row);
|
|
|
|
}
|
|
|
|
public function execSql($sql, $params) {
|
|
|
|
return $this->execute($sql, $params);
|
|
|
|
}
|
2014-04-19 16:56:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MapperTest extends MapperTestUtility {
|
|
|
|
|
2014-07-17 18:35:00 +04:00
|
|
|
/**
|
|
|
|
* @var Mapper
|
|
|
|
*/
|
2014-04-19 16:56:16 +04:00
|
|
|
private $mapper;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-04-19 16:56:16 +04:00
|
|
|
parent::setUp();
|
|
|
|
$this->mapper = new ExampleMapper($this->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testMapperShouldSetTableName() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->assertEquals('*PREFIX*table', $this->mapper->getTableName());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindQuery() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['jo'];
|
|
|
|
$rows = [
|
|
|
|
['hi']
|
|
|
|
];
|
2015-01-29 21:23:57 +03:00
|
|
|
$this->setMapperResult($sql, $params, $rows);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper->find($sql, $params);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindEntity() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['jo'];
|
|
|
|
$rows = [
|
|
|
|
['pre_name' => 'hi']
|
|
|
|
];
|
2015-01-29 21:23:57 +03:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper->findOneEntity($sql, $params);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindNotFound() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['jo'];
|
|
|
|
$rows = [];
|
2015-01-29 21:23:57 +03:00
|
|
|
$this->setMapperResult($sql, $params, $rows);
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(DoesNotExistException::class);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper->find($sql, $params);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindEntityNotFound() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['jo'];
|
|
|
|
$rows = [];
|
2015-01-29 21:23:57 +03:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(DoesNotExistException::class);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper->findOneEntity($sql, $params);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindMultiple() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['jo'];
|
|
|
|
$rows = [
|
|
|
|
['jo'], ['ho']
|
|
|
|
];
|
2015-01-29 21:23:57 +03:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(MultipleObjectsReturnedException::class);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper->find($sql, $params);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindEntityMultiple() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['jo'];
|
|
|
|
$rows = [
|
|
|
|
['jo'], ['ho']
|
|
|
|
];
|
2015-01-29 21:23:57 +03:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(MultipleObjectsReturnedException::class);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper->findOneEntity($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testDelete() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'DELETE FROM `*PREFIX*table` WHERE `id` = ?';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = [2];
|
2014-04-19 16:56:16 +04:00
|
|
|
|
2015-02-24 18:47:38 +03:00
|
|
|
$this->setMapperResult($sql, $params, [], null, null, true);
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setId($params[0]);
|
|
|
|
|
|
|
|
$this->mapper->delete($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testCreate() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->db->expects($this->once())
|
2015-02-24 18:47:38 +03:00
|
|
|
->method('lastInsertId')
|
2014-04-19 16:56:16 +04:00
|
|
|
->with($this->equalTo('*PREFIX*table'))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(3);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper = new ExampleMapper($this->db);
|
|
|
|
|
|
|
|
$sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
|
|
|
|
'VALUES(?,?)';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['john', 'my@email'];
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName($params[0]);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
|
2015-02-24 18:47:38 +03:00
|
|
|
$this->setMapperResult($sql, $params, [], null, null, true);
|
2014-04-19 16:56:16 +04:00
|
|
|
|
|
|
|
$this->mapper->insert($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testCreateShouldReturnItemWithCorrectInsertId() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->db->expects($this->once())
|
2015-02-24 18:47:38 +03:00
|
|
|
->method('lastInsertId')
|
2014-04-19 16:56:16 +04:00
|
|
|
->with($this->equalTo('*PREFIX*table'))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(3);
|
2014-04-19 16:56:16 +04:00
|
|
|
$this->mapper = new ExampleMapper($this->db);
|
|
|
|
|
|
|
|
$sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
|
|
|
|
'VALUES(?,?)';
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['john', 'my@email'];
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName($params[0]);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
|
|
|
|
$this->setMapperResult($sql, $params);
|
|
|
|
|
|
|
|
$result = $this->mapper->insert($entity);
|
|
|
|
|
|
|
|
$this->assertEquals(3, $result->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-19 16:55:21 +03:00
|
|
|
public function testAssocParameters() {
|
|
|
|
$sql = 'test';
|
|
|
|
$params = [':test' => 1, ':a' => 2];
|
|
|
|
|
|
|
|
$this->setMapperResult($sql, $params);
|
|
|
|
$this->mapper->execSql($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testUpdate() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'UPDATE `*PREFIX*table` ' .
|
|
|
|
'SET ' .
|
|
|
|
'`pre_name` = ?,'.
|
|
|
|
'`email` = ? ' .
|
|
|
|
'WHERE `id` = ?';
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['john', 'my@email', 1];
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName($params[0]);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
$entity->setId($params[2]);
|
|
|
|
|
2015-02-24 18:47:38 +03:00
|
|
|
$this->setMapperResult($sql, $params, [], null, null, true);
|
2014-04-19 16:56:16 +04:00
|
|
|
|
|
|
|
$this->mapper->update($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testUpdateNoId() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['john', 'my@email'];
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName($params[0]);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2014-04-19 16:56:16 +04:00
|
|
|
|
|
|
|
$this->mapper->update($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testUpdateNothingChangedNoQuery() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$params = ['john', 'my@email'];
|
2014-04-23 15:43:17 +04:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setId(3);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
$entity->resetUpdatedFields();
|
|
|
|
|
|
|
|
$this->db->expects($this->never())
|
2016-04-08 15:47:23 +03:00
|
|
|
->method('prepare');
|
2014-04-23 15:43:17 +04:00
|
|
|
|
|
|
|
$this->mapper->update($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testMapRowToEntity() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$entity1 = $this->mapper->mapRow(['pre_name' => 'test1', 'email' => 'test2']);
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity2 = new Example();
|
|
|
|
$entity2->setPreName('test1');
|
|
|
|
$entity2->setEmail('test2');
|
|
|
|
$entity2->resetUpdatedFields();
|
|
|
|
$this->assertEquals($entity2, $entity1);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindEntities() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$rows = [
|
|
|
|
['pre_name' => 'hi']
|
|
|
|
];
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName('hi');
|
|
|
|
$entity->resetUpdatedFields();
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->setMapperResult($sql, [], $rows, null, null, true);
|
2014-04-19 16:56:16 +04:00
|
|
|
$result = $this->mapper->findAllEntities($sql);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertEquals([$entity], $result);
|
2014-04-19 16:56:16 +04:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindEntitiesNotFound() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$rows = [];
|
|
|
|
$this->setMapperResult($sql, [], $rows);
|
2014-04-19 16:56:16 +04:00
|
|
|
$result = $this->mapper->findAllEntities($sql);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertEquals([], $result);
|
2014-04-19 16:56:16 +04:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testFindEntitiesMultiple() {
|
2014-04-19 16:56:16 +04:00
|
|
|
$sql = 'hi';
|
2020-03-26 11:30:18 +03:00
|
|
|
$rows = [
|
|
|
|
['pre_name' => 'jo'], ['email' => 'ho']
|
|
|
|
];
|
2014-04-19 16:56:16 +04:00
|
|
|
$entity1 = new Example();
|
|
|
|
$entity1->setPreName('jo');
|
|
|
|
$entity1->resetUpdatedFields();
|
|
|
|
$entity2 = new Example();
|
|
|
|
$entity2->setEmail('ho');
|
|
|
|
$entity2->resetUpdatedFields();
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->setMapperResult($sql, [], $rows);
|
2014-04-19 16:56:16 +04:00
|
|
|
$result = $this->mapper->findAllEntities($sql);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertEquals([$entity1, $entity2], $result);
|
2014-04-19 16:56:16 +04:00
|
|
|
}
|
2014-07-17 18:35:00 +04:00
|
|
|
}
|