. * */ namespace OCA\News\Db; require_once(__DIR__ . "/../classloader.php"); class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { private $feedMapper; private $feeds; protected function setUp(){ $this->beforeEach(); $this->feedMapper = new FeedMapper($this->api); // create mock feeds $feed1 = new Feed(); $feed2 = new Feed(); $this->feeds = array( $feed1, $feed2 ); } public function testFind(){ $userId = 'john'; $id = 3; $rows = array( array('id' => $this->feeds[0]->getId()), ); $sql = 'SELECT * FROM `*dbprefix*news_feeds` ' . 'WHERE `id` = ? ' . 'AND `user_id` = ?'; $this->setMapperResult($sql, array($id, $userId), $rows); $result = $this->feedMapper->find($id, $userId); $this->assertEquals($this->feeds[0], $result); } public function testFindNotFound(){ $userId = 'john'; $id = 3; $sql = 'SELECT * FROM `*dbprefix*news_feeds` ' . 'WHERE `id` = ? ' . 'AND `user_id` = ?'; $this->setMapperResult($sql, array($id, $userId)); $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException'); $result = $this->feedMapper->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 * FROM `*dbprefix*news_feeds` ' . 'WHERE `id` = ? ' . 'AND `user_id` = ?'; $this->setMapperResult($sql, array($id, $userId), $rows); $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException'); $result = $this->feedMapper->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 `*dbprefix*news_feeds`'; $this->setMapperResult($sql, array(), $rows); $result = $this->feedMapper->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 `*dbprefix*news_feeds` `feeds` ' . 'LEFT OUTER JOIN `*dbprefix*news_items` `items` ' . 'ON `feeds`.`id` = `items`.`feed_id` ' . 'WHERE (`items`.`status` & ?) > 0 ' . 'AND `feeds`.`user_id` = ? ' . 'GROUP BY `items`.`feed_id`'; $this->setMapperResult($sql, array($userId), $rows); $result = $this->feedMapper->findAllFromUser($userId); $this->assertEquals($this->feeds, $result); } public function testGetStarredCount(){ $userId = 'john'; $row = array( array('size' => 9) ); $sql = 'SELECT COUNT(*) AS size FROM `*dbprefix*news_feeds` ' . 'AND `user_id` = ? ' . 'AND ((`status` & ?) > 0)'; $this->setMapperResult($sql, array($userId, StatusFlag::STARRED), $row); $result = $this->feedMapper->getStarredCount($userId); $this->assertEquals($row[0]['size'], $result); } }