From 7171a3dba0f932bffa2b7bba6e84edf02712cea9 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 5 Apr 2013 10:27:28 +0200 Subject: moved tests into unit directory --- tests/unit/db/FeedMapperTest.php | 258 ++++++++++++++++++++++++++++++++ tests/unit/db/FolderMapperTest.php | 176 ++++++++++++++++++++++ tests/unit/db/ItemMapperTest.php | 299 +++++++++++++++++++++++++++++++++++++ tests/unit/db/ItemTest.php | 69 +++++++++ 4 files changed, 802 insertions(+) create mode 100644 tests/unit/db/FeedMapperTest.php create mode 100644 tests/unit/db/FolderMapperTest.php create mode 100644 tests/unit/db/ItemMapperTest.php create mode 100644 tests/unit/db/ItemTest.php (limited to 'tests/unit/db') diff --git a/tests/unit/db/FeedMapperTest.php b/tests/unit/db/FeedMapperTest.php new file mode 100644 index 000000000..58ea8c4ed --- /dev/null +++ b/tests/unit/db/FeedMapperTest.php @@ -0,0 +1,258 @@ +. +* +*/ + +namespace OCA\News\Db; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { + + private $mapper; + private $feeds; + + protected function setUp(){ + $this->beforeEach(); + + $this->mapper = new FeedMapper($this->api); + + // create mock feeds + $feed1 = new Feed(); + $feed2 = new Feed(); + + $this->feeds = array( + $feed1, + $feed2 + ); + $this->user = 'herman'; + } + + + public function testFind(){ + $userId = 'john'; + $id = 3; + $rows = array( + array('id' => $this->feeds[0]->getId()), + ); + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ?) = ? ' . + 'WHERE `feeds`.`id` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + $params = array(StatusFlag::UNREAD, StatusFlag::UNREAD, $id, $userId); + $this->setMapperResult($sql, $params, $rows); + + $result = $this->mapper->find($id, $userId); + $this->assertEquals($this->feeds[0], $result); + + } + + + public function testFindNotFound(){ + $userId = 'john'; + $id = 3; + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ?) = ? ' . + 'WHERE `feeds`.`id` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + $params = array(StatusFlag::UNREAD, StatusFlag::UNREAD, $id, $userId); + $this->setMapperResult($sql, $params); + + $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException'); + $result = $this->mapper->find($id, $userId); + } + + + public function testFindMoreThanOneResultFound(){ + $userId = 'john'; + $id = 3; + $rows = array( + array('id' => $this->feeds[0]->getId()), + array('id' => $this->feeds[1]->getId()) + ); + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ?) = ? ' . + 'WHERE `feeds`.`id` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + $params = array(StatusFlag::UNREAD, StatusFlag::UNREAD, $id, $userId); + $this->setMapperResult($sql, $params, $rows); + + $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException'); + $result = $this->mapper->find($id, $userId); + } + + + public function testFindAll(){ + $userId = 'john'; + $rows = array( + array('id' => $this->feeds[0]->getId()), + array('id' => $this->feeds[1]->getId()) + ); + $sql = 'SELECT * FROM `*PREFIX*news_feeds`'; + + $this->setMapperResult($sql, array(), $rows); + + $result = $this->mapper->findAll(); + $this->assertEquals($this->feeds, $result); + } + + + public function testFindAllFromUser(){ + $userId = 'john'; + $rows = array( + array('id' => $this->feeds[0]->getId()), + array('id' => $this->feeds[1]->getId()) + ); + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ?) = ? ' . + 'WHERE `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + + $this->setMapperResult($sql, + array(StatusFlag::UNREAD, StatusFlag::UNREAD, $userId), $rows); + + $result = $this->mapper->findAllFromUser($userId); + $this->assertEquals($this->feeds, $result); + } + + + public function testFindByUrlHash(){ + $urlHash = md5('hihi'); + $row = array( + array('id' => $this->feeds[0]->getId()), + ); + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ?) = ? ' . + 'WHERE `feeds`.`url_hash` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + $this->setMapperResult($sql, + array(StatusFlag::UNREAD, StatusFlag::UNREAD, $urlHash, $this->user), $row); + + $result = $this->mapper->findByUrlHash($urlHash, $this->user); + $this->assertEquals($this->feeds[0], $result); + } + + + public function testFindByUrlHashNotFound(){ + $urlHash = md5('hihi'); + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ?) = ? ' . + 'WHERE `feeds`.`url_hash` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + $this->setMapperResult($sql, + array(StatusFlag::UNREAD, StatusFlag::UNREAD, $urlHash, $this->user)); + + $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException'); + $result = $this->mapper->findByUrlHash($urlHash, $this->user); + } + + + public function testFindByUrlHashMoreThanOneResultFound(){ + $urlHash = md5('hihi'); + $rows = array( + array('id' => $this->feeds[0]->getId()), + array('id' => $this->feeds[1]->getId()) + ); + $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . + 'FROM `*PREFIX*news_feeds` `feeds` ' . + 'LEFT JOIN `*PREFIX*news_items` `items` ' . + 'ON `feeds`.`id` = `items`.`feed_id` ' . + 'AND (`items`.`status` & ?) = ? ' . + 'WHERE `feeds`.`url_hash` = ? ' . + 'AND `feeds`.`user_id` = ? ' . + 'GROUP BY `feeds`.`id`'; + $this->setMapperResult($sql, + array(StatusFlag::UNREAD, StatusFlag::UNREAD, $urlHash, $this->user), $rows); + + $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException'); + $result = $this->mapper->findByUrlHash($urlHash, $this->user); + } + + + public function testDelete(){ + $feed = new Feed(); + $feed->setId(3); + + $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `id` = ?'; + $arguments = array($feed->getId()); + + $sql2 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` = ?'; + $arguments2 = array($feed->getId()); + + $pdoResult = $this->getMock('Result', + array('fetchRow')); + $pdoResult->expects($this->any()) + ->method('fetchRow'); + + $query = $this->getMock('Query', + array('execute')); + $query->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($arguments)) + ->will($this->returnValue($pdoResult)); + $this->api->expects($this->at(0)) + ->method('prepareQuery') + ->with($this->equalTo($sql)) + ->will(($this->returnValue($query))); + + $query->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($arguments2)) + ->will($this->returnValue($pdoResult)); + $this->api->expects($this->at(1)) + ->method('prepareQuery') + ->with($this->equalTo($sql2)) + ->will(($this->returnValue($query))); + + $this->mapper->delete($feed); + + } + + + + +} \ No newline at end of file diff --git a/tests/unit/db/FolderMapperTest.php b/tests/unit/db/FolderMapperTest.php new file mode 100644 index 000000000..57eff63a4 --- /dev/null +++ b/tests/unit/db/FolderMapperTest.php @@ -0,0 +1,176 @@ +. +* +*/ + +namespace OCA\News\Db; + +require_once(__DIR__ . "/../../classloader.php"); + + +class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { + + private $folderMapper; + private $folders; + + protected function setUp(){ + $this->beforeEach(); + + $this->folderMapper = new FolderMapper($this->api); + + // create mock folders + $folder1 = new Folder(); + $folder2 = new Folder(); + + $this->folders = array( + $folder1, + $folder2 + ); + } + + + public function testFind(){ + $userId = 'john'; + $id = 3; + $rows = array( + array('id' => $this->folders[0]->getId()), + ); + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; + + $this->setMapperResult($sql, array($id, $userId), $rows); + + $result = $this->folderMapper->find($id, $userId); + $this->assertEquals($this->folders[0], $result); + + } + + + public function testFindNotFound(){ + $userId = 'john'; + $id = 3; + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; + + $this->setMapperResult($sql, array($id, $userId)); + + $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException'); + $result = $this->folderMapper->find($id, $userId); + } + + + public function testFindMoreThanOneResultFound(){ + $userId = 'john'; + $id = 3; + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; + + $this->setMapperResult($sql, array($id, $userId), $rows); + + $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException'); + $result = $this->folderMapper->find($id, $userId); + } + + + + public function testFindAllFromUser(){ + $userId = 'john'; + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `user_id` = ?'; + + $this->setMapperResult($sql, array($userId), $rows); + + $result = $this->folderMapper->findAllFromUser($userId); + $this->assertEquals($this->folders, $result); + } + + + public function testFindByName(){ + $userId = 'john'; + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . + 'WHERE `user_id` = ?'; + + $this->setMapperResult($sql, array($userId), $rows); + + $result = $this->folderMapper->findAllFromUser($userId); + $this->assertEquals($this->folders, $result); + } + + + public function testDelete(){ + $folder = new Folder(); + $folder->setId(3); + + $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `id` = ?'; + $arguments = array($folder->getId()); + + $sql2 = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?; '. + 'DELETE `items` FROM `*PREFIX*news_items` `items` '. + 'LEFT JOIN `*PREFIX*news_feeds` `feeds` ON '. + '`items`.`feed_id` = `feed`.`id` WHERE `feeds`.`id` IS NULL;'; + $arguments2 = array($folder->getId()); + + $pdoResult = $this->getMock('Result', + array('fetchRow')); + $pdoResult->expects($this->any()) + ->method('fetchRow'); + + $query = $this->getMock('Query', + array('execute')); + $query->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($arguments)) + ->will($this->returnValue($pdoResult)); + $this->api->expects($this->at(0)) + ->method('prepareQuery') + ->with($this->equalTo($sql)) + ->will(($this->returnValue($query))); + + $query->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($arguments2)) + ->will($this->returnValue($pdoResult)); + $this->api->expects($this->at(1)) + ->method('prepareQuery') + ->with($this->equalTo($sql2)) + ->will(($this->returnValue($query))); + + $this->folderMapper->delete($folder); + } + +} \ No newline at end of file diff --git a/tests/unit/db/ItemMapperTest.php b/tests/unit/db/ItemMapperTest.php new file mode 100644 index 000000000..a4ed6e921 --- /dev/null +++ b/tests/unit/db/ItemMapperTest.php @@ -0,0 +1,299 @@ +. +* +*/ + +namespace OCA\News\Db; + +require_once(__DIR__ . "/../../classloader.php"); + + +class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { + + private $mapper; + private $items; + + public function setUp() + { + $this->beforeEach(); + + $this->mapper = new ItemMapper($this->api); + + // create mock items + $item1 = new Item(); + $item2 = new Item(); + + $this->items = array( + $item1, + $item2 + ); + + $this->userId = 'john'; + $this->id = 3; + $this->folderId = 2; + + $this->row = array( + array('id' => $this->items[0]->getId()), + ); + + $this->rows = array( + array('id' => $this->items[0]->getId()), + array('id' => $this->items[1]->getId()) + ); + + $this->user = 'john'; + $this->limit = 10; + $this->offset = 3; + $this->id = 11; + $this->status = 333; + $this->updatedSince = 323; + + } + + + private function makeSelectQuery($prependTo){ + return 'SELECT `items`.* FROM `*PREFIX*news_items` `items` '. + 'JOIN `*PREFIX*news_feeds` `feeds` ' . + 'ON `feeds`.`id` = `items`.`feed_id` '. + 'AND `feeds`.`user_id` = ? ' . $prependTo; + } + + private function makeSelectQueryStatus($prependTo) { + return $this->makeSelectQuery( + 'AND ((`items`.`status` & ?) = ?) ' . + $prependTo + ); + } + + + public function testFind(){ + $sql = $this->makeSelectQuery('AND `*PREFIX*news_items`.`id` = ? '); + + $this->setMapperResult($sql, array($this->userId, $this->id), $this->row); + + $result = $this->mapper->find($this->id, $this->userId); + $this->assertEquals($this->items[0], $result); + } + + + public function testGetStarredCount(){ + $userId = 'john'; + $row = array( + array('size' => 9) + ); + $sql = 'SELECT COUNT(*) AS size FROM `*PREFIX*news_feeds` `feeds` ' . + 'JOIN `*PREFIX*news_items` `items` ' . + 'ON `items`.`feed_id` = `feeds`.`id` ' . + 'AND `feeds`.`user_id` = ? ' . + 'WHERE ((`items`.`status` & ?) = ?)'; + + $this->setMapperResult($sql, array($userId, StatusFlag::STARRED, + StatusFlag::STARRED), $row); + + $result = $this->mapper->starredCount($userId); + $this->assertEquals($row[0]['size'], $result); + } + + + public function testReadFeed(){ + $sql = 'UPDATE `*PREFIX*news_feeds` `feeds` ' . + 'JOIN `*PREFIX*news_items` `items` ' . + 'ON `items`.`feed_id` = `feeds`.`id` ' . + 'AND `feeds`.`user_id` = ? ' . + 'AND `feeds`.`id` = ? ' . + 'AND `items`.`id` <= ? ' . + 'SET `items`.`status` = (`items`.`status` & ?) '; + $params = array($this->user, 3, 6, ~StatusFlag::UNREAD); + $this->setMapperResult($sql, $params); + $this->mapper->readFeed(3, 6, $this->user); + } + + + public function testFindAllNew(){ + $sql = 'AND `items`.`id` >= ?'; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status, + $this->updatedSince); + + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllNew($this->updatedSince, + $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllNewFeed(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`id` >= ?'; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status, $this->id, + $this->updatedSince); + + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllNewFeed($this->id, $this->updatedSince, + $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllNewFolder(){ + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'AND `items`.`id` >= ?'; + $sql = $this->makeSelectQueryStatus($sql); + + $params = array($this->user, $this->status, $this->status, $this->id, + $this->updatedSince); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllNewFolder($this->id, $this->updatedSince, + $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllFeed(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`id` > ? ' . + 'ORDER BY `items`.`id` DESC '; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status, $this->id, + $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFeed($this->id, $this->limit, + $this->offset, $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllFeedOffsetZero(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'ORDER BY `items`.`id` DESC '; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status, $this->id); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFeed($this->id, $this->limit, + 0, $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllFolder(){ + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'AND `items`.`id` > ? ' . + 'ORDER BY `items`.`id` DESC '; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status, $this->id, + $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFolder($this->id, $this->limit, + $this->offset, $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllFolderOffsetZero(){ + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'ORDER BY `items`.`id` DESC '; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status, $this->id); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAllFolder($this->id, $this->limit, + 0, $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAll(){ + $sql = 'AND `items`.`id` > ? ' . + 'ORDER BY `items`.`id` DESC '; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status, + $this->offset); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAll($this->limit, + $this->offset, $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindAllOffsetZero(){ + $sql = 'ORDER BY `items`.`id` DESC '; + $sql = $this->makeSelectQueryStatus($sql); + $params = array($this->user, $this->status, $this->status); + $this->setMapperResult($sql, $params, $this->rows); + $result = $this->mapper->findAll($this->limit, + 0, $this->status, $this->user); + + $this->assertEquals($this->items, $result); + } + + + public function testFindByGuidHash(){ + $hash = md5('test'); + $feedId = 3; + $sql = $this->makeSelectQuery( + 'AND `items`.`guid_hash` = ? ' . + 'AND `feed`.`id = ? '); + + $this->setMapperResult($sql, array($this->userId, $hash, $feedId), $this->row); + + $result = $this->mapper->findByGuidHash($hash, $feedId, $this->userId); + $this->assertEquals($this->items[0], $result); + } + + + public function testGetReadOlderThanThreshold(){ + $status = StatusFlag::STARRED | StatusFlag::UNREAD; + $sql = 'SELECT * FROM `*PREFIX*news_items` ' . + 'WHERE NOT ((`status` & ?) > 0)'; + $threshold = 10; + $feed = new Feed(); + $feed->setId(30); + $rows = array(array('id' => 30)); + $params = array($status); + + $this->setMapperResult($sql, $params, $rows); + $result = $this->mapper->getReadOlderThanThreshold($threshold); + + $this->assertEquals($feed->getId(), $result[0]->getId()); + } + + + public function testDeleteReadOlderThanId(){ + $id = 10; + $status = StatusFlag::STARRED | StatusFlag::UNREAD; + $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ? ' . + 'AND NOT ((`status` & ?) > 0)'; + $params = array($id, $status); + + $this->setMapperResult($sql, $params); + $this->mapper->deleteReadOlderThanId($id); + } +} \ No newline at end of file diff --git a/tests/unit/db/ItemTest.php b/tests/unit/db/ItemTest.php new file mode 100644 index 000000000..3ce7183fd --- /dev/null +++ b/tests/unit/db/ItemTest.php @@ -0,0 +1,69 @@ +. +* +*/ + +namespace OCA\News\Db; + +require_once(__DIR__ . "/../../classloader.php"); + + +class ItemTest extends \PHPUnit_Framework_TestCase { + + private $item; + + protected function setUp(){ + $this->item = new Item(); + $this->item->setStatus(0); + } + + + public function testSetRead(){ + $this->item->setRead(); + + $this->assertTrue($this->item->isRead()); + } + + + public function testSetUnread(){ + $this->item->setUnread(); + + $this->assertTrue($this->item->isUnread()); + } + + + public function testSetStarred(){ + $this->item->setStarred(); + + $this->assertTrue($this->item->isStarred()); + } + + + public function testSetUnstarred(){ + $this->item->setUnstarred(); + + $this->assertTrue($this->item->isUnstarred()); + } + + +} \ No newline at end of file -- cgit v1.2.3