From 03ce3af3a51f51852cd0a3f06872fc36d7f62dfb Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Tue, 24 Feb 2015 17:05:50 +0100 Subject: cleanup db stuff --- db/foldermapper.php | 6 ++- tests/unit/db/FolderMapperTest.php | 8 ++-- tests/unit/db/mappertestutility.php | 87 ++++++++++++++++++------------------- 3 files changed, 51 insertions(+), 50 deletions(-) diff --git a/db/foldermapper.php b/db/foldermapper.php index bce599b45..f2a2ff68d 100644 --- a/db/foldermapper.php +++ b/db/foldermapper.php @@ -58,12 +58,14 @@ class FolderMapper extends NewsMapper { // we needz CASCADE + FKs please $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?'; $params = [$entity->getId()]; - $this->execute($sql, $params); + $stmt = $this->execute($sql, $params); + $stmt->closeCursor(); $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; - $this->execute($sql); + $stmt = $this->execute($sql); + $stmt->closeCursor(); } diff --git a/tests/unit/db/FolderMapperTest.php b/tests/unit/db/FolderMapperTest.php index e9df37195..00ad2127d 100644 --- a/tests/unit/db/FolderMapperTest.php +++ b/tests/unit/db/FolderMapperTest.php @@ -125,14 +125,14 @@ class FolderMapperTest extends \OCA\News\Tests\Unit\Db\MapperTestUtility { $arguments = [$folder->getId()]; $sql2 = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?'; + $arguments2 = [$folder->getId()]; $sql3 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; - $arguments2 = [$folder->getId()]; - $this->setMapperResult($sql, $arguments); - $this->setMapperResult($sql2, $arguments2); - $this->setMapperResult($sql3); + $this->setMapperResult($sql, $arguments, [], null, null, true); + $this->setMapperResult($sql2, $arguments2, [], null, null, true); + $this->setMapperResult($sql3, [], [], null, null, true); $this->folderMapper->delete($folder); } diff --git a/tests/unit/db/mappertestutility.php b/tests/unit/db/mappertestutility.php index 278cbc475..cba2aadd6 100644 --- a/tests/unit/db/mappertestutility.php +++ b/tests/unit/db/mappertestutility.php @@ -31,7 +31,6 @@ namespace OCA\News\Tests\Unit\Db; abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { protected $db; private $query; - private $pdoResult; private $queryAt; private $prepareAt; private $fetchAt; @@ -50,11 +49,10 @@ abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { ->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; } @@ -68,15 +66,40 @@ abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { * of the database query. If not provided, it wont be assumed that fetch * will be called on the result */ - protected function setMapperResult($sql, $arguments=[], $returnRows=[], + 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){ @@ -87,16 +110,11 @@ abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { $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) { @@ -128,42 +146,23 @@ abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { $this->query->expects($this->at($this->queryAt)) ->method('execute') - ->with() - ->will($this->returnValue($this->pdoResult)); + ->will($this->returnCallback(function($sql, $p=null, $o=null, $s=null) { + + })); $this->queryAt++; - if($limit === null && $offset === null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepareQuery') - ->with($this->equalTo($sql), - $this->equalTo(null), - $this->equalTo(null)) - ->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), - $this->equalTo(null)) - ->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->at($this->queryAt); + } else { + $closing = $this->any(); } + $this->query->expects($closing)->method('closeCursor'); + $this->queryAt++; + $this->prepareAt++; $this->fetchAt++; - } -- cgit v1.2.3