*/
public function delete(Entity $entity){
$sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?';
- $this->execute($sql, [$entity->getId()]);
+ $stmt = $this->execute($sql, [$entity->getId()]);
+ $stmt->closeCursor();
return $entity;
}
$values .= ',';
}
- array_push($params, $entity->$getter());
+ $params[] = $entity->$getter();
$i++;
}
$sql = 'INSERT INTO `' . $this->tableName . '`(' .
$columns . ') VALUES(' . $values . ')';
- $this->execute($sql, $params);
+ $stmt = $this->execute($sql, $params);
$entity->setId((int) $this->db->lastInsertId($this->tableName));
+ $stmt->closeCursor();
+
return $entity;
}
$columns .= ',';
}
- array_push($params, $entity->$getter());
+ $params[] = $entity->$getter();
$i++;
}
$sql = 'UPDATE `' . $this->tableName . '` SET ' .
$columns . ' WHERE `id` = ?';
- array_push($params, $id);
+ $params[] = $id;
- $this->execute($sql, $params);
+ $stmt = $this->execute($sql, $params);
+ $stmt->closeCursor();
return $entity;
}
namespace OCP\AppFramework\Db;
-use \OCP\IDb;
+use \OCP\IDBConnection;
use Test\AppFramework\Db\MapperTestUtility;
/**
class ExampleMapper extends Mapper {
- public function __construct(IDb $db){ parent::__construct($db, 'table'); }
+ 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); }
$sql = 'DELETE FROM `*PREFIX*table` WHERE `id` = ?';
$params = array(2);
- $this->setMapperResult($sql, $params);
+ $this->setMapperResult($sql, $params, [], null, null, true);
$entity = new Example();
$entity->setId($params[0]);
public function testCreate(){
$this->db->expects($this->once())
- ->method('getInsertId')
+ ->method('lastInsertId')
->with($this->equalTo('*PREFIX*table'))
->will($this->returnValue(3));
$this->mapper = new ExampleMapper($this->db);
$entity->setPreName($params[0]);
$entity->setEmail($params[1]);
- $this->setMapperResult($sql, $params);
+ $this->setMapperResult($sql, $params, [], null, null, true);
$this->mapper->insert($entity);
}
public function testCreateShouldReturnItemWithCorrectInsertId(){
$this->db->expects($this->once())
- ->method('getInsertId')
+ ->method('lastInsertId')
->with($this->equalTo('*PREFIX*table'))
->will($this->returnValue(3));
$this->mapper = new ExampleMapper($this->db);
$entity->setEmail($params[1]);
$entity->setId($params[2]);
- $this->setMapperResult($sql, $params);
+ $this->setMapperResult($sql, $params, [], null, null, true);
$this->mapper->update($entity);
}
abstract class MapperTestUtility extends \Test\TestCase {
protected $db;
private $query;
- private $pdoResult;
private $queryAt;
private $prepareAt;
private $fetchAt;
parent::setUp();
$this->db = $this->getMockBuilder(
- '\OCP\IDb')
+ '\OCP\IDBConnection')
->disableOriginalConstructor()
->getMock();
- $this->query = $this->getMock('Query', array('execute', 'bindValue'));
- $this->pdoResult = $this->getMock('Result', array('fetch', 'closeCursor'));
+ $this->query = $this->getMock('\PDOStatement');
$this->queryAt = 0;
$this->prepareAt = 0;
- $this->iterators = array();
+ $this->iterators = [];
$this->fetchAt = 0;
}
*/
protected function setMapperResult($sql, $arguments=array(), $returnRows=array(),
$limit=null, $offset=null, $expectClose=false){
+ if($limit === null && $offset === null) {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql))
+ ->will(($this->returnValue($this->query)));
+ } elseif($limit !== null && $offset === null) {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql), $this->equalTo($limit))
+ ->will(($this->returnValue($this->query)));
+ } elseif($limit === null && $offset !== null) {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql),
+ $this->equalTo(null),
+ $this->equalTo($offset))
+ ->will(($this->returnValue($this->query)));
+ } else {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql),
+ $this->equalTo($limit),
+ $this->equalTo($offset))
+ ->will(($this->returnValue($this->query)));
+ }
$this->iterators[] = new ArgumentIterator($returnRows);
$iterators = $this->iterators;
$fetchAt = $this->fetchAt;
- $this->pdoResult->expects($this->any())
+ $this->query->expects($this->any())
->method('fetch')
->will($this->returnCallback(
function() use ($iterators, $fetchAt){
$fetchAt++;
}
+ $this->queryAt++;
+
return $result;
}
));
- if ($expectClose) {
- $closing = $this->once();
- } else {
- $closing = $this->any();
- }
- $this->pdoResult->expects($closing)->method('closeCursor');
$index = 1;
foreach($arguments as $argument) {
}
$this->query->expects($this->at($this->queryAt))
- ->method('execute')
- ->with()
- ->will($this->returnValue($this->pdoResult));
+ ->method('execute');
$this->queryAt++;
- if($limit === null && $offset === null) {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepareQuery')
- ->with($this->equalTo($sql))
- ->will(($this->returnValue($this->query)));
- } elseif($limit !== null && $offset === null) {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepareQuery')
- ->with($this->equalTo($sql), $this->equalTo($limit))
- ->will(($this->returnValue($this->query)));
- } elseif($limit === null && $offset !== null) {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepareQuery')
- ->with($this->equalTo($sql),
- $this->equalTo(null),
- $this->equalTo($offset))
- ->will(($this->returnValue($this->query)));
- } else {
- $this->db->expects($this->at($this->prepareAt))
- ->method('prepareQuery')
- ->with($this->equalTo($sql),
- $this->equalTo($limit),
- $this->equalTo($offset))
- ->will(($this->returnValue($this->query)));
+
+
+ if ($expectClose) {
+ $closing = $this->once();
+ } else {
+ $closing = $this->any();
}
+ $this->query->expects($closing)->method('closeCursor');
+ $this->queryAt++;
+
$this->prepareAt++;
$this->fetchAt++;
-
}