Wrote tests for CardApiControllerTest.php

Signed-off-by: Ryan Fletcher <ryan.fletcher@codepassion.ca>
This commit is contained in:
Ryan Fletcher
2018-07-16 19:25:01 -04:00
committed by Julius Härtl
parent 5bc8a363b9
commit 9c81584b02
3 changed files with 152 additions and 30 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
/**

View File

@@ -0,0 +1,138 @@
<?php
/**
* @copyright Copyright (c) 2018 Ryan Fletcher <ryan.fletcher@codepassion.ca>
*
* @author Ryan Fletcher <ryan.fletcher@codepassion.ca>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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);
}
}