2014-04-19 12:56:16 +00: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 12:47:23 +00:00
|
|
|
namespace Test\AppFramework\Db;
|
2014-04-19 12:56:16 +00:00
|
|
|
|
2018-01-24 17:10:16 +00:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
2015-02-24 15:47:38 +00:00
|
|
|
use \OCP\IDBConnection;
|
2016-04-08 12:47:23 +00:00
|
|
|
use \OCP\AppFramework\Db\Entity;
|
|
|
|
use \OCP\AppFramework\Db\Mapper;
|
2014-04-19 12:56:16 +00:00
|
|
|
|
2014-04-20 00:55:21 +00: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 12:56:16 +00:00
|
|
|
class Example extends Entity {
|
2014-08-27 11:28:04 +00:00
|
|
|
protected $preName;
|
|
|
|
protected $email;
|
2014-04-19 12:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ExampleMapper extends Mapper {
|
2015-02-24 15:47:38 +00:00
|
|
|
public function __construct(IDBConnection $db){ parent::__construct($db, 'table'); }
|
2014-04-19 12:56:16 +00:00
|
|
|
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); }
|
2015-03-19 13:55:21 +00:00
|
|
|
public function execSql($sql, $params){ return $this->execute($sql, $params); }
|
2014-04-19 12:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MapperTest extends MapperTestUtility {
|
|
|
|
|
2014-07-17 14:35:00 +00:00
|
|
|
/**
|
|
|
|
* @var Mapper
|
|
|
|
*/
|
2014-04-19 12:56:16 +00:00
|
|
|
private $mapper;
|
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
protected function setUp(){
|
2014-04-19 12:56:16 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->mapper = new ExampleMapper($this->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testMapperShouldSetTableName(){
|
|
|
|
$this->assertEquals('*PREFIX*table', $this->mapper->getTableName());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testFindQuery(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$params = array('jo');
|
|
|
|
$rows = array(
|
|
|
|
array('hi')
|
|
|
|
);
|
2015-01-29 18:23:57 +00:00
|
|
|
$this->setMapperResult($sql, $params, $rows);
|
2014-04-19 12:56:16 +00:00
|
|
|
$this->mapper->find($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindEntity(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$params = array('jo');
|
|
|
|
$rows = array(
|
|
|
|
array('pre_name' => 'hi')
|
|
|
|
);
|
2015-01-29 18:23:57 +00:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2014-04-19 12:56:16 +00:00
|
|
|
$this->mapper->findOneEntity($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindNotFound(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$params = array('jo');
|
|
|
|
$rows = array();
|
2015-01-29 18:23:57 +00:00
|
|
|
$this->setMapperResult($sql, $params, $rows);
|
2018-01-24 17:10:16 +00:00
|
|
|
$this->expectException(DoesNotExistException::class);
|
2014-04-19 12:56:16 +00:00
|
|
|
$this->mapper->find($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindEntityNotFound(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$params = array('jo');
|
|
|
|
$rows = array();
|
2015-01-29 18:23:57 +00:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2018-01-24 17:10:16 +00:00
|
|
|
$this->expectException(DoesNotExistException::class);
|
2014-04-19 12:56:16 +00:00
|
|
|
$this->mapper->findOneEntity($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindMultiple(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$params = array('jo');
|
|
|
|
$rows = array(
|
|
|
|
array('jo'), array('ho')
|
|
|
|
);
|
2015-01-29 18:23:57 +00:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2018-01-24 17:10:16 +00:00
|
|
|
$this->expectException(MultipleObjectsReturnedException::class);
|
2014-04-19 12:56:16 +00:00
|
|
|
$this->mapper->find($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindEntityMultiple(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$params = array('jo');
|
|
|
|
$rows = array(
|
|
|
|
array('jo'), array('ho')
|
|
|
|
);
|
2015-01-29 18:23:57 +00:00
|
|
|
$this->setMapperResult($sql, $params, $rows, null, null, true);
|
2018-01-24 17:10:16 +00:00
|
|
|
$this->expectException(MultipleObjectsReturnedException::class);
|
2014-04-19 12:56:16 +00:00
|
|
|
$this->mapper->findOneEntity($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testDelete(){
|
|
|
|
$sql = 'DELETE FROM `*PREFIX*table` WHERE `id` = ?';
|
|
|
|
$params = array(2);
|
|
|
|
|
2015-02-24 15:47:38 +00:00
|
|
|
$this->setMapperResult($sql, $params, [], null, null, true);
|
2014-04-19 12:56:16 +00:00
|
|
|
$entity = new Example();
|
|
|
|
$entity->setId($params[0]);
|
|
|
|
|
|
|
|
$this->mapper->delete($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testCreate(){
|
|
|
|
$this->db->expects($this->once())
|
2015-02-24 15:47:38 +00:00
|
|
|
->method('lastInsertId')
|
2014-04-19 12:56:16 +00:00
|
|
|
->with($this->equalTo('*PREFIX*table'))
|
|
|
|
->will($this->returnValue(3));
|
|
|
|
$this->mapper = new ExampleMapper($this->db);
|
|
|
|
|
|
|
|
$sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
|
|
|
|
'VALUES(?,?)';
|
|
|
|
$params = array('john', 'my@email');
|
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName($params[0]);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
|
2015-02-24 15:47:38 +00:00
|
|
|
$this->setMapperResult($sql, $params, [], null, null, true);
|
2014-04-19 12:56:16 +00:00
|
|
|
|
|
|
|
$this->mapper->insert($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testCreateShouldReturnItemWithCorrectInsertId(){
|
|
|
|
$this->db->expects($this->once())
|
2015-02-24 15:47:38 +00:00
|
|
|
->method('lastInsertId')
|
2014-04-19 12:56:16 +00:00
|
|
|
->with($this->equalTo('*PREFIX*table'))
|
|
|
|
->will($this->returnValue(3));
|
|
|
|
$this->mapper = new ExampleMapper($this->db);
|
|
|
|
|
|
|
|
$sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
|
|
|
|
'VALUES(?,?)';
|
|
|
|
$params = array('john', 'my@email');
|
|
|
|
$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 13:55:21 +00:00
|
|
|
public function testAssocParameters() {
|
|
|
|
$sql = 'test';
|
|
|
|
$params = [':test' => 1, ':a' => 2];
|
|
|
|
|
|
|
|
$this->setMapperResult($sql, $params);
|
|
|
|
$this->mapper->execSql($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-19 12:56:16 +00:00
|
|
|
public function testUpdate(){
|
|
|
|
$sql = 'UPDATE `*PREFIX*table` ' .
|
|
|
|
'SET ' .
|
|
|
|
'`pre_name` = ?,'.
|
|
|
|
'`email` = ? ' .
|
|
|
|
'WHERE `id` = ?';
|
|
|
|
|
|
|
|
$params = array('john', 'my@email', 1);
|
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName($params[0]);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
$entity->setId($params[2]);
|
|
|
|
|
2015-02-24 15:47:38 +00:00
|
|
|
$this->setMapperResult($sql, $params, [], null, null, true);
|
2014-04-19 12:56:16 +00:00
|
|
|
|
|
|
|
$this->mapper->update($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testUpdateNoId(){
|
|
|
|
$params = array('john', 'my@email');
|
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName($params[0]);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
|
2018-01-24 17:10:16 +00:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
2014-04-19 12:56:16 +00:00
|
|
|
|
|
|
|
$this->mapper->update($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-23 11:43:17 +00:00
|
|
|
public function testUpdateNothingChangedNoQuery(){
|
|
|
|
$params = array('john', 'my@email');
|
|
|
|
$entity = new Example();
|
|
|
|
$entity->setId(3);
|
|
|
|
$entity->setEmail($params[1]);
|
|
|
|
$entity->resetUpdatedFields();
|
|
|
|
|
|
|
|
$this->db->expects($this->never())
|
2016-04-08 12:47:23 +00:00
|
|
|
->method('prepare');
|
2014-04-23 11:43:17 +00:00
|
|
|
|
|
|
|
$this->mapper->update($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-19 12:56:16 +00:00
|
|
|
public function testMapRowToEntity(){
|
|
|
|
$entity1 = $this->mapper->mapRow(array('pre_name' => 'test1', 'email' => 'test2'));
|
|
|
|
$entity2 = new Example();
|
|
|
|
$entity2->setPreName('test1');
|
|
|
|
$entity2->setEmail('test2');
|
|
|
|
$entity2->resetUpdatedFields();
|
|
|
|
$this->assertEquals($entity2, $entity1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindEntities(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$rows = array(
|
|
|
|
array('pre_name' => 'hi')
|
|
|
|
);
|
|
|
|
$entity = new Example();
|
|
|
|
$entity->setPreName('hi');
|
|
|
|
$entity->resetUpdatedFields();
|
2015-01-29 18:23:57 +00:00
|
|
|
$this->setMapperResult($sql, array(), $rows, null, null, true);
|
2014-04-19 12:56:16 +00:00
|
|
|
$result = $this->mapper->findAllEntities($sql);
|
|
|
|
$this->assertEquals(array($entity), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindEntitiesNotFound(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$rows = array();
|
2014-04-19 13:25:36 +00:00
|
|
|
$this->setMapperResult($sql, array(), $rows);
|
2014-04-19 12:56:16 +00:00
|
|
|
$result = $this->mapper->findAllEntities($sql);
|
|
|
|
$this->assertEquals(array(), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindEntitiesMultiple(){
|
|
|
|
$sql = 'hi';
|
|
|
|
$rows = array(
|
|
|
|
array('pre_name' => 'jo'), array('email' => 'ho')
|
|
|
|
);
|
|
|
|
$entity1 = new Example();
|
|
|
|
$entity1->setPreName('jo');
|
|
|
|
$entity1->resetUpdatedFields();
|
|
|
|
$entity2 = new Example();
|
|
|
|
$entity2->setEmail('ho');
|
|
|
|
$entity2->resetUpdatedFields();
|
2014-04-19 13:25:36 +00:00
|
|
|
$this->setMapperResult($sql, array(), $rows);
|
2014-04-19 12:56:16 +00:00
|
|
|
$result = $this->mapper->findAllEntities($sql);
|
|
|
|
$this->assertEquals(array($entity1, $entity2), $result);
|
|
|
|
}
|
2014-07-17 14:35:00 +00:00
|
|
|
}
|