Wrote initial unit tests for LabelApiControllerTest.php

Signed-off-by: Ryan Fletcher <ryan.fletcher@codepassion.ca>
This commit is contained in:
Ryan Fletcher
2018-07-16 22:29:51 -04:00
committed by Julius Härtl
parent 9c81584b02
commit 184cd00a8b
2 changed files with 136 additions and 82 deletions

View File

@@ -47,12 +47,10 @@ class LabelApiController extends ApiController {
* @param LabelService $service
* @param $userId
*/
public function __construct($appName, IRequest $request, LabelService $labelService, BoardService $boardService, $userId) {
public function __construct($appName, IRequest $request, LabelService $labelService, $userId) {
parent::__construct($appName, $request);
$this->labelService = $labelService;
$this->boardService = $boardService;
$this->userId = $userId;
$this->apiHelper = new ApiHelper();
$this->labelService = $labelService;
$this->userId = $userId;
}
/**
@@ -63,22 +61,7 @@ class LabelApiController extends ApiController {
* Get a specific label.
*/
public function get() {
$boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
}
if (is_numeric($this->request->params['labelId']) === false) {
return new DataResponse('label id must be a number', HTTP::STATUS_BAD_REQUEST);
}
$label = $this->labelService->find($this->request->params['labelId']);
if ($label === false || $label === null) {
return new DataResponse('Label not found', HTTP::STATUS_NOT_FOUND);
}
$label = $this->labelService->find($this->request->getParam('labelId'));
return new DataResponse($label, HTTP::STATUS_OK);
}
@@ -92,26 +75,7 @@ class LabelApiController extends ApiController {
* Create a new label
*/
public function create($title, $color) {
$boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
}
if ($title === false || $title === null) {
return new DataResponse('title must be provided', HTTP::STATUS_BAD_REQUEST);
}
if ($color === false || $color === null) {
return new DataResponse('color must be provided', HTTP::STATUS_BAD_REQUEST);
}
try {
$label = $this->labelService->create($title, $color, $this->request->params['boardId']);
} catch (Exception $e) {
return new DataResponse($e->getMessage(), HTTP::STATUS_INTERNAL_SERVER_ERROR);
}
$label = $this->labelService->create($title, $color, $this->request->getParam('boardId'));
return new DataResponse($label, HTTP::STATUS_OK);
}
@@ -124,31 +88,8 @@ class LabelApiController extends ApiController {
* @params $color
* Update a specific label
*/
public function update($title, $color) {
$boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
}
if (is_numeric($this->request->params['labelId']) === false) {
return new DataResponse('label id must be a number', HTTP::STATUS_BAD_REQUEST);
}
if ($title === false || $title === null) {
return new DataResponse('title must be provided', HTTP::STATUS_BAD_REQUEST);
}
if ($color === false || $color === null) {
return new DataResponse('color must be provided', HTTP::STATUS_BAD_REQUEST);
}
try {
$label = $this->labelService->update($this->request->params['labelId'], $title, $color);
} catch (Exception $e) {
return new DataResponse($e->getMessage(), HTTP::STATUS_INTERNAL_SERVER_ERROR);
}
public function update($title, $color) {
$label = $this->labelService->update($this->request->getParam('labelId'), $title, $color);
return new DataResponse($label, HTTP::STATUS_OK);
}
@@ -160,22 +101,7 @@ class LabelApiController extends ApiController {
* Delete a specific label
*/
public function delete() {
$boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
}
if (is_numeric($this->request->params['labelId']) === false) {
return new DataResponse('label id must be a number', HTTP::STATUS_BAD_REQUEST);
}
try {
$label = $this->labelService->delete($this->request->params['labelId']);
} catch (Exception $e) {
return new DataResponse($e->getMessage(), HTTP::STATUS_INTERNAL_SERVER_ERROR);
}
$label = $this->labelService->delete($this->request->getParam('labelId'));
return new DataResponse($label, HTTP::STATUS_OK);
}

View File

@@ -0,0 +1,128 @@
<?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\Label;
use OCA\Deck\Service\LabelService;
class LabelApiControllerTest extends \Test\TestCase {
private $controller;
private $request;
private $labelService;
private $userId = 'admin';
private $exampleLabel;
public function setUp() {
parent::setUp();
$this->request = $this->createMock(IRequest::class);
$this->labelService = $this->createMock(LabelService::class);
$this->exampleLabel['id'];
$this->controller = new LabelApiController(
'deck',
$this->request,
$this->labelService,
$this->userId
);
}
public function testGet() {
$label = new Label();
$label->setId($this->exampleLabel['id']);
$this->request->expects($this->once())
->method('getParam')
->with('labelId')
->will($this->returnValue($this->exampleLabel['id']));
$this->labelService->expects($this->once())
->method('find')
->willReturn($label);
$expected = new DataResponse($label, HTTP::STATUS_OK);
$actual = $this->controller->get();
$this->assertEquals($expected, $actual);
}
public function testCreate() {
$label = new Label();
$label->setId($this->exampleLabel['id']);
$this->request->expects($this->once())
->method('getParam')
->with('boardId')
->will($this->returnValue(1));
$this->labelService->expects($this->once())
->method('create')
->willReturn($label);
$expected = new DataResponse($label, HTTP::STATUS_OK);
$actual = $this->controller->create('title', '000000');
$this->assertEquals($expected, $actual);
}
public function testUpdate() {
$label = new Label();
$label->setId($this->exampleLabel['id']);
$this->request->expects($this->once())
->method('getParam')
->with('labelId')
->will($this->returnValue($this->exampleLabel['id']));
$this->labelService->expects($this->once())
->method('update')
->will($this->returnValue($label));
$expected = new DataResponse($label, HTTP::STATUS_OK);
$actual = $this->controller->update('title', '000000');
$this->assertEquals($expected, $actual);
}
public function testDelete() {
$label = new Label();
$label->setId($this->exampleLabel['id']);
$this->request->expects($this->once())
->method('getParam')
->with('labelId')
->will($this->returnValue($this->exampleLabel['id']));
$this->labelService->expects($this->once())
->method('delete')
->willReturn($label);
$expected = new DataResponse($label, HTTP::STATUS_OK);
$actual = $this->controller->delete();
$this->assertEquals($expected, $actual);
}
}