summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-22 13:20:24 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-22 13:20:24 +0100
commit196177a0ca3fc0103d359eec44f46173be685413 (patch)
treef721b656d64e240050980420202873e94f6e9edc
parent7a579b42fa731b65db26bd0c026bc68f2339c451 (diff)
removed mapper parent class and use the new mapper methods from the framework
-rw-r--r--bl/bl.php4
-rw-r--r--bl/feedbl.php3
-rw-r--r--db/feedmapper.php6
-rw-r--r--db/foldermapper.php5
-rw-r--r--db/imapper.php29
-rw-r--r--db/itemmapper.php20
-rw-r--r--db/newsmapper.php57
-rw-r--r--tests/bl/BlTest.php25
-rw-r--r--tests/db/ItemMapperTest.php15
-rw-r--r--tests/db/NewsMapperTest.php86
10 files changed, 86 insertions, 164 deletions
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 @@
+<?php
+/**
+* ownCloud - News app
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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/>.
+*
+*/
+
+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 @@
-<?php
-
-/**
- * ownCloud - App Framework
- *
- * @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.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/>.
- *
- */
-
-
-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 @@
-<?php
-
-/**
-* ownCloud - News
-*
-* @author Alessandro Cosentino
-* @author Bernhard Posselt
-* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
-* @copyright 2012 Bernhard Posselt nukeawhale@gmail.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/>.
-*
-*/
-
-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