diff options
-rw-r--r-- | external/itemapi.php | 31 | ||||
-rw-r--r-- | tests/unit/external/ItemAPITest.php | 174 |
2 files changed, 199 insertions, 6 deletions
diff --git a/external/itemapi.php b/external/itemapi.php index b1b75ca35..99dc8ddef 100644 --- a/external/itemapi.php +++ b/external/itemapi.php @@ -103,28 +103,47 @@ class ItemAPI extends Controller { private function setRead($isRead) { $userId = $this->api->getUserId(); $itemId = $this->params('itemId'); - $this->itemBusinessLayer->read($itemId, $isRead, $userId); + try { + $this->itemBusinessLayer->read($itemId, $isRead, $userId); + return new NewsAPIResult(); + } catch(BusinessLayerException $ex){ + return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, + $ex->getMessage()); + } + } + + + private function setStarred($isStarred) { + $userId = $this->api->getUserId(); + $feedId = $this->params('feedId'); + $guidHash = $this->params('guidHash'); + try { + $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); + return new NewsAPIResult(); + } catch(BusinessLayerException $ex){ + return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, + $ex->getMessage()); + } } public function read() { - $this->setRead(true); - return new NewsAPIResult(); + return $this->setRead(true); } public function unread() { - + return $this->setRead(false); } public function star() { - + return $this->setStarred(true); } public function unstar() { - + return $this->setStarred(false); } diff --git a/tests/unit/external/ItemAPITest.php b/tests/unit/external/ItemAPITest.php index c91b06db7..a6cd96018 100644 --- a/tests/unit/external/ItemAPITest.php +++ b/tests/unit/external/ItemAPITest.php @@ -40,6 +40,7 @@ class ItemAPITest extends \PHPUnit_Framework_TestCase { private $api; private $user; private $request; + private $msg; protected function setUp() { $this->api = $this->getMockBuilder( @@ -63,6 +64,7 @@ class ItemAPITest extends \PHPUnit_Framework_TestCase { $this->request, $this->itemBusinessLayer ); + $this->msg = 'hi'; } @@ -162,4 +164,176 @@ class ItemAPITest extends \PHPUnit_Framework_TestCase { $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); } + + public function testReadDoesNotExist() { + $request = new Request(array('urlParams' => array( + 'itemId' => 2 + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('read') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->itemAPI->read(); + + $this->assertNull($response->getData()); + $this->assertEquals($this->msg, $response->getMessage()); + $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + } + + + public function testUnread() { + $request = new Request(array('urlParams' => array( + 'itemId' => 2 + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('read') + ->with( + $this->equalTo(2), + $this->equalTo(false), + $this->equalTo($this->user) + ); + + $response = $this->itemAPI->unread(); + + $this->assertNull($response->getData()); + $this->assertNull($response->getMessage()); + $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + } + + + public function testUnreadDoesNotExist() { + $request = new Request(array('urlParams' => array( + 'itemId' => 2 + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('read') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->itemAPI->unread(); + + $this->assertNull($response->getData()); + $this->assertEquals($this->msg, $response->getMessage()); + $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + } + + + public function testStar() { + $request = new Request(array('urlParams' => array( + 'feedId' => 2, + 'guidHash' => 'hash' + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('star') + ->with( + $this->equalTo(2), + $this->equalTo('hash'), + $this->equalTo(true), + $this->equalTo($this->user) + ); + + $response = $this->itemAPI->star(); + + $this->assertNull($response->getData()); + $this->assertNull($response->getMessage()); + $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + } + + + public function testStarDoesNotExist() { + $request = new Request(array('urlParams' => array( + 'feedId' => 2, + 'guidHash' => 'hash' + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('star') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->itemAPI->star(); + + $this->assertNull($response->getData()); + $this->assertEquals($this->msg, $response->getMessage()); + $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + } + + + public function testUnstar() { + $request = new Request(array('urlParams' => array( + 'feedId' => 2, + 'guidHash' => 'hash' + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('star') + ->with( + $this->equalTo(2), + $this->equalTo('hash'), + $this->equalTo(false), + $this->equalTo($this->user) + ); + + $response = $this->itemAPI->unstar(); + + $this->assertNull($response->getData()); + $this->assertNull($response->getMessage()); + $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + } + + + public function testUnstarDoesNotExist() { + $request = new Request(array('urlParams' => array( + 'feedId' => 2, + 'guidHash' => 'hash' + ))); + $this->itemAPI = new ItemAPI( + $this->api, + $request, + $this->itemBusinessLayer + ); + + $this->itemBusinessLayer->expects($this->once()) + ->method('star') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->itemAPI->unstar(); + + $this->assertNull($response->getData()); + $this->assertEquals($this->msg, $response->getMessage()); + $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + } + }
\ No newline at end of file |