From 9c81584b026426caad7c94e092ef55769539068a Mon Sep 17 00:00:00 2001 From: Ryan Fletcher Date: Mon, 16 Jul 2018 19:25:01 -0400 Subject: [PATCH] Wrote tests for CardApiControllerTest.php Signed-off-by: Ryan Fletcher --- lib/Controller/CardApiController.php | 37 ++--- lib/Controller/StackApiController.php | 7 +- .../unit/controller/CardApiControllerTest.php | 138 ++++++++++++++++++ 3 files changed, 152 insertions(+), 30 deletions(-) create mode 100644 tests/unit/controller/CardApiControllerTest.php diff --git a/lib/Controller/CardApiController.php b/lib/Controller/CardApiController.php index 82363d62f..64d48ec71 100644 --- a/lib/Controller/CardApiController.php +++ b/lib/Controller/CardApiController.php @@ -42,8 +42,7 @@ class CardApiController extends ApiController { private $cardService; private $boardService; private $stackService; - private $userId; - private $apiHelper; + private $userId; /** * @param string $appName @@ -51,13 +50,10 @@ class CardApiController extends ApiController { * @param CardService $service * @param $userId */ - public function __construct($appName, IRequest $request, CardService $cardService, BoardService $boardService, StackService $stackService, $userId) { - parent::__construct($appName, $request); - $this->boardService = $boardService; - $this->cardService = $cardService; - $this->stackService = $stackService; - $this->userId = $userId; - $this->apiHelper = new ApiHelper(); + public function __construct($appName, IRequest $request, CardService $cardService, $userId) { + parent::__construct($appName, $request); + $this->cardService = $cardService; + $this->userId = $userId; } /** @@ -67,8 +63,8 @@ class CardApiController extends ApiController { * * Get a specific card. */ - public function get() { - $card = $this->cardService->find($this->request->params['cardId']); + public function get() { + $card = $this->cardService->find($this->request->getParam('cardId')); return new DataResponse($card, HTTP::STATUS_OK); } @@ -83,8 +79,8 @@ class CardApiController extends ApiController { * * Get a specific card. */ - public function create($title, $type = 'plain', $order = 999) { - $card = $this->cardService->create($title, $this->request->params['stackId'], $type, $order, $this->userId); + public function create($title, $type = 'plain', $order = 999) { + $card = $this->cardService->create($title, $this->request->getParam('stackId'), $type, $order, $this->userId); return new DataResponse($card, HTTP::STATUS_OK); } @@ -96,19 +92,10 @@ class CardApiController extends ApiController { * * Update a card */ - public function update($cardId, $title, $stackId, $type, $order = 0, $description = '', $owner, $duedate = null) { + public function update($title, $type, $order = 0, $description = '', $owner, $duedate = null) { $card = $this->cardService->update($this->request->getParam('cardId'), $title, $this->request->getParam('stackId'), $type, $order, $description, $owner, $duedate); return new DataResponse($card, HTTP::STATUS_OK); - } - - - public function assignUser() { - throw new Exception('Not Implemented'); - } - - public function unassignUser() { - throw new Exception('Not Implemented'); - } + } /** * @NoAdminRequired @@ -118,7 +105,7 @@ class CardApiController extends ApiController { * Delete a specific card. */ public function delete() { - $card = $this->cardService->delete($this->request->params['cardId']); + $card = $this->cardService->delete($this->request->getParam('cardId')); return new DataResponse($card, HTTP::STATUS_OK); } } \ No newline at end of file diff --git a/lib/Controller/StackApiController.php b/lib/Controller/StackApiController.php index 4cc6a2f6e..f5565633a 100644 --- a/lib/Controller/StackApiController.php +++ b/lib/Controller/StackApiController.php @@ -32,7 +32,6 @@ use OCP\IRequest; use OCA\Deck\StatusException; use OCA\Deck\Service\StackService; use OCA\Deck\Service\BoardService; -use OCA\Deck\Controller\Helper\ApiHelper; /** * Class StackApiController @@ -42,8 +41,7 @@ use OCA\Deck\Controller\Helper\ApiHelper; class StackApiController extends ApiController { private $boardService; - private $stackService; - private $apiHelper; + private $stackService; /** * @param string $appName @@ -53,8 +51,7 @@ class StackApiController extends ApiController { public function __construct($appName, IRequest $request, StackService $stackService, BoardService $boardService) { parent::__construct($appName, $request); $this->stackService = $stackService; - $this->boardService = $boardService; - $this->apiHelper = new ApiHelper(); + $this->boardService = $boardService; } /** diff --git a/tests/unit/controller/CardApiControllerTest.php b/tests/unit/controller/CardApiControllerTest.php new file mode 100644 index 000000000..82ad0329b --- /dev/null +++ b/tests/unit/controller/CardApiControllerTest.php @@ -0,0 +1,138 @@ + + * + * @author Ryan Fletcher + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see . + * + */ +namespace OCA\Deck\Controller; + +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\IRequest; + +use OCA\Deck\Db\Card; +use OCA\Deck\Service\CardService; + +class CardApiControllerTest extends \Test\TestCase { + + private $controller; + private $request; + private $cardService; + private $userId = 'admin'; + private $cardExample; + private $stackExample; + + public function setUp() { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->cardService = $this->createMock(CardService::class); + + $this->cardExample['id'] = 1; + $this->stackExample['id'] = 1; + + $this->controller = new CardApiController ( + $appName = 'deck', + $this->request, + $this->cardService, + $this->userId + ); + } + + public function testGet() { + $card = new Card(); + $card->setId($this->cardExample['id']); + + $this->request->expects($this->once()) + ->method('getParam') + ->with('cardId') + ->will($this->returnValue($this->cardExample['id'])); + + $this->cardService->expects($this->once()) + ->method('find') + ->willReturn($card); + + $expected = new DataResponse($card, HTTP::STATUS_OK); + $actual = $this->controller->get(); + $this->assertEquals($expected, $actual); + } + + public function testCreate() { + + $card = new Card(); + $card->setId($this->cardExample['id']); + $card->setStackId($this->stackExample['id']); + + $this->request->expects($this->once()) + ->method('getParam') + ->with('stackId') + ->willReturn($this->stackExample['id']); + + $this->cardService->expects($this->once()) + ->method('create') + ->willReturn($card); + + $expected = new DataResponse($card, HTTP::STATUS_OK); + $actual = $this->controller->create('title'); + $this->assertEquals($expected, $actual); + } + + public function testUpdate() { + $card = new Card(); + $card->setId($this->cardExample['id']); + $card->setStackId($this->stackExample['id']); + + $this->request->expects($this->exactly(2)) + ->method('getParam') + ->withConsecutive( + ['cardId'], + ['stackId'] + )->willReturnonConsecutiveCalls( + $this->cardExample['id'], + $this->stackExample['id']); + + $this->cardService->expects($this->once()) + ->method('update') + ->willReturn($card); + + $expected = new DataResponse($card, HTTP::STATUS_OK); + $actual = $this->controller->update('title', 'plain', 0, 'description', $this->userId, null); + $this->assertEquals($expected, $actual); + } + + public function testDelete() { + + $card = new Card(); + $card->setId($this->cardExample['id']); + + $this->request->expects($this->once()) + ->method('getParam') + ->with('cardId') + ->will($this->returnValue($this->cardExample['id'])); + + $this->cardService->expects($this->once()) + ->method('delete') + ->willReturn($card); + + $expected = new DataResponse($card, HTTP::STATUS_OK); + $actual = $this->controller->delete(); + $this->assertEquals($expected, $actual); + } + +} \ No newline at end of file