From 196177a0ca3fc0103d359eec44f46173be685413 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 22 Mar 2013 13:20:24 +0100 Subject: removed mapper parent class and use the new mapper methods from the framework --- bl/bl.php | 4 +-- bl/feedbl.php | 3 +- db/feedmapper.php | 6 ++-- db/foldermapper.php | 5 +-- db/imapper.php | 29 +++++++++++++++ db/itemmapper.php | 20 +++++++++-- db/newsmapper.php | 57 ------------------------------ tests/bl/BlTest.php | 25 +++++++------ tests/db/ItemMapperTest.php | 15 ++++++++ tests/db/NewsMapperTest.php | 86 --------------------------------------------- 10 files changed, 86 insertions(+), 164 deletions(-) create mode 100644 db/imapper.php delete mode 100644 db/newsmapper.php delete mode 100644 tests/db/NewsMapperTest.php diff --git a/bl/bl.php b/bl/bl.php index eeabdcb17..ee4ace588 100644 --- a/bl/bl.php +++ b/bl/bl.php @@ -28,14 +28,14 @@ namespace OCA\News\Bl; use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\AppFramework\Db\MultipleObjectsReturnedException; -use \OCA\News\Db\NewsMapper; +use \OCA\News\Db\IMapper; abstract class Bl { protected $mapper; - public function __construct(NewsMapper $mapper){ + public function __construct(IMapper $mapper){ $this->mapper = $mapper; } diff --git a/bl/feedbl.php b/bl/feedbl.php index 5e5ff4fa7..53a2e6061 100644 --- a/bl/feedbl.php +++ b/bl/feedbl.php @@ -55,7 +55,8 @@ class FeedBl extends Bl { public function create($feedUrl, $folderId, $userId){ - // first try if its possible to reach the feed + // first try if the feed exists already + try { list($feed, $items) = $this->feedFetcher->fetch($feedUrl); diff --git a/db/feedmapper.php b/db/feedmapper.php index cf434257c..0fc8d3114 100644 --- a/db/feedmapper.php +++ b/db/feedmapper.php @@ -26,9 +26,9 @@ namespace OCA\News\Db; use \OCA\AppFramework\Core\API; +use \OCA\AppFramework\Db\Mapper; - -class FeedMapper extends NewsMapper { +class FeedMapper extends Mapper implements IMapper { public function __construct(API $api) { @@ -41,7 +41,7 @@ class FeedMapper extends NewsMapper { 'WHERE `id` = ? ' . 'AND `user_id` = ?'; - $row = $this->findRow($sql, $id, $userId); + $row = $this->findQuery($sql, array($id, $userId)); $feed = new Feed(); $feed->fromRow($row); diff --git a/db/foldermapper.php b/db/foldermapper.php index 041b3ada4..ef2f31f2d 100644 --- a/db/foldermapper.php +++ b/db/foldermapper.php @@ -26,9 +26,10 @@ namespace OCA\News\Db; use \OCA\AppFramework\Core\API; +use \OCA\AppFramework\Db\Mapper; -class FolderMapper extends NewsMapper { +class FolderMapper extends Mapper implements IMapper { public function __construct(API $api) { parent::__construct($api, 'news_folders'); @@ -39,7 +40,7 @@ class FolderMapper extends NewsMapper { 'WHERE `id` = ? ' . 'AND `user_id` = ?'; - $row = $this->findRow($sql, $id, $userId); + $row = $this->findQuery($sql, array($id, $userId)); $folder = new Folder(); $folder->fromRow($row); diff --git a/db/imapper.php b/db/imapper.php new file mode 100644 index 000000000..056a4b479 --- /dev/null +++ b/db/imapper.php @@ -0,0 +1,29 @@ +. +* +*/ + +namespace OCA\News\Db; + +interface IMapper { + public function find($id, $userId); +} \ No newline at end of file diff --git a/db/itemmapper.php b/db/itemmapper.php index 04f073c36..2efa9b535 100644 --- a/db/itemmapper.php +++ b/db/itemmapper.php @@ -29,12 +29,28 @@ use \OCA\AppFramework\Db\MultipleObjectsReturnedException; use \OCA\AppFramework\Db\Mapper; use \OCA\AppFramework\Core\API; -class ItemMapper extends NewsMapper { +class ItemMapper extends Mapper implements IMapper { public function __construct(API $api){ parent::__construct($api, 'news_items'); } + + public function findByUrlHash($urlHash, $userId){ + $sql = 'SELECT `*dbprefix*news_items`.* FROM `*dbprefix*news_items` ' . + 'JOIN `*dbprefix*news_feeds` ' . + 'ON `*dbprefix*news_feeds`.`id` = `*dbprefix*news_items`.`feed_id` ' . + 'WHERE `*dbprefix*news_items`.`url_hash` = ? ' . + 'AND `*dbprefix*news_feeds`.`user_id` = ? '; + $params = array($urlHash, $userId); + + $row = $this->findQuery($sql, $params); + $item = new Item(); + $item->fromRow($row); + + return $item; + } + protected function findAllRows($sql, $params, $limit=null, $offset=null) { $result = $this->execute($sql, $params, $limit, $offset); $items = array(); @@ -130,7 +146,7 @@ class ItemMapper extends NewsMapper { 'WHERE `*dbprefix*news_items`.`id` = ? ' . 'AND `*dbprefix*news_feeds`.`user_id` = ? '; - $row = $this->findRow($sql, $id, $userId); + $row = $this->findQuery($sql, array($id, $userId)); $item = new Item(); $item->fromRow($row); diff --git a/db/newsmapper.php b/db/newsmapper.php deleted file mode 100644 index bc2cf5679..000000000 --- a/db/newsmapper.php +++ /dev/null @@ -1,57 +0,0 @@ -. - * - */ - - -namespace OCA\News\Db; - -use \OCA\AppFramework\Db\DoesNotExistException; -use \OCA\AppFramework\Db\MultipleObjectsReturnedException; -use \OCA\AppFramework\Db\Mapper; -use \OCA\AppFramework\Core\API; - - -abstract class NewsMapper extends Mapper { - - - public function __construct(API $api, $tableName) { - parent::__construct($api, $tableName); - } - - - protected function findRow($sql, $id, $userId){ - - $result = $this->execute($sql, array($id, $userId)); - - $row = $result->fetchRow(); - - if($row === false){ - throw new DoesNotExistException('Item does not exist!'); - } elseif($result->fetchRow() !== false) { - throw new MultipleObjectsReturnedException('More than one result for Item with id ' . $id . '!'); - } else { - return $row; - } - } - - -} \ No newline at end of file diff --git a/tests/bl/BlTest.php b/tests/bl/BlTest.php index 5dc16d5a2..0878ddccf 100644 --- a/tests/bl/BlTest.php +++ b/tests/bl/BlTest.php @@ -28,6 +28,8 @@ namespace OCA\News\Bl; require_once(__DIR__ . "/../classloader.php"); +use \OCA\AppFramework\Db\DoesNotExistException; +use \OCA\AppFramework\Db\MultipleObjectsReturnedException; use \OCA\News\Db\Folder; @@ -40,14 +42,15 @@ class TestBl extends BL { class BlTest extends \OCA\AppFramework\Utility\TestUtility { protected $api; - protected $newsMapper; + protected $mapper; protected $newsBl; protected function setUp(){ $this->api = $this->getAPIMock(); - $this->newsMapper = $this->getMock('\OCA\News\Db\NewsMapper', - array('update', 'delete', 'find'), array($this->api, 'test')); - $this->newsBl = new TestBl($this->newsMapper); + $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') + ->disableOriginalConstructor() + ->getMock(); + $this->newsBl = new TestBl($this->mapper); } @@ -57,10 +60,10 @@ class BlTest extends \OCA\AppFramework\Utility\TestUtility { $folder = new Folder(); $folder->setId($id); - $this->newsMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('delete') ->with($this->equalTo($folder)); - $this->newsMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('find') ->with($this->equalTo($id), $this->equalTo($user)) ->will($this->returnValue($folder)); @@ -73,7 +76,7 @@ class BlTest extends \OCA\AppFramework\Utility\TestUtility { $id = 3; $user = 'ken'; - $this->newsMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('find') ->with($this->equalTo($id), $this->equalTo($user)); @@ -82,9 +85,9 @@ class BlTest extends \OCA\AppFramework\Utility\TestUtility { public function testFindDoesNotExist(){ - $ex = new \OCA\AppFramework\Db\DoesNotExistException('hi'); + $ex = new DoesNotExistException('hi'); - $this->newsMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('find') ->will($this->throwException($ex)); @@ -94,9 +97,9 @@ class BlTest extends \OCA\AppFramework\Utility\TestUtility { public function testFindMultiple(){ - $ex = new \OCA\AppFramework\Db\MultipleObjectsReturnedException('hi'); + $ex = new MultipleObjectsReturnedException('hi'); - $this->newsMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('find') ->will($this->throwException($ex)); diff --git a/tests/db/ItemMapperTest.php b/tests/db/ItemMapperTest.php index 82b5b2faf..fb37f9bc0 100644 --- a/tests/db/ItemMapperTest.php +++ b/tests/db/ItemMapperTest.php @@ -76,6 +76,21 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { $this->assertEquals($this->items[0], $result); } + + + public function testFindByUrlHash(){ + $urlHash = md5('hihi'); + $sql = 'SELECT `*dbprefix*news_items`.* FROM `*dbprefix*news_items` ' . + 'JOIN `*dbprefix*news_feeds` ' . + 'ON `*dbprefix*news_feeds`.`id` = `*dbprefix*news_items`.`feed_id` ' . + 'WHERE `*dbprefix*news_items`.`url_hash` = ? ' . + 'AND `*dbprefix*news_feeds`.`user_id` = ? '; + $this->setMapperResult($sql, array($urlHash, $this->userId), $this->row); + + $result = $this->itemMapper->findByUrlHash($urlHash, $this->userId); + $this->assertEquals($this->items[0], $result); + } + // // public function testFindNotFound(){ // $sql = 'SELECT `*dbprefix*news_items`.* FROM `*dbprefix*news_items` ' . diff --git a/tests/db/NewsMapperTest.php b/tests/db/NewsMapperTest.php deleted file mode 100644 index 77459ff27..000000000 --- a/tests/db/NewsMapperTest.php +++ /dev/null @@ -1,86 +0,0 @@ -. -* -*/ - -namespace OCA\News\Db; -use \OCA\AppFramework\Core\API; - -require_once(__DIR__ . "/../classloader.php"); - -class MapperNews extends NewsMapper { - public function __construct(API $api, $tableName){ - parent::__construct($api, $tableName); - } - - public function publicFindRow($sql, $id, $userId){ - return $this->findRow($sql, $id, $userId); - } -} - -class NewsMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { - - private $newsMapper; - - public function setUp() { - $this->beforeEach(); - $this->newsMapper = new MapperNews($this->api, 'news_table'); - - $this->userId = 'john'; - $this->id = 2; - - $this->rows = array( - array('testRow') - ); - } - - public function testFindRow() { - $sql = 'test'; - - $this->setMapperResult($sql, array($this->id, $this->userId), $this->rows); - - $result = $this->newsMapper->publicFindRow($sql, $this->id, $this->userId); - $this->assertEquals($this->rows[0], $result); - - } - - public function testFindRowNoFound() { - $sql = 'test'; - - $this->setMapperResult($sql, array($this->id, $this->userId), array()); - - $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException'); - $this->newsMapper->publicFindRow($sql, $this->id, $this->userId); - - } - - public function testFindRowMultipleRows() { - $sql = 'test'; - array_push($this->rows, array('testRow2')); - $this->setMapperResult($sql, array($this->id, $this->userId), $this->rows); - - $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException'); - $this->newsMapper->publicFindRow($sql, $this->id, $this->userId); - - } -} \ No newline at end of file -- cgit v1.2.3