From 5719e9f134f3079e640bdbcd7f6c47b2d2730eac Mon Sep 17 00:00:00 2001 From: Ryan Fletcher Date: Sun, 15 Jul 2018 20:01:55 -0400 Subject: [PATCH] Wrote unit tests against StackApiController -> get() Signed-off-by: Ryan Fletcher --- lib/Controller/StackApiController.php | 24 ++-- .../controller/StackApiControllerTest.php | 109 +++++++++++++++++- 2 files changed, 121 insertions(+), 12 deletions(-) diff --git a/lib/Controller/StackApiController.php b/lib/Controller/StackApiController.php index 137de1e6d..d605d6499 100644 --- a/lib/Controller/StackApiController.php +++ b/lib/Controller/StackApiController.php @@ -90,11 +90,15 @@ class StackApiController extends ApiController { if ($boardError) { return new DataResponse($boardError['message'], $boardError['status']); } - - $stack = $this->service->find($this->request->params['stackId']); - if ($stack == false || $stack == null) { - return new DataResponse("Stack not found", HTTP::STATUS_NOT_FOUND); + if (is_numeric($this->request->getParam('stackId')) === false) { + return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); + } + + $stack = $this->service->find($this->request->getParam('stackId')); + + if ($stack === false || $stack === null) { + return new DataResponse('stack not found', HTTP::STATUS_NOT_FOUND); } return new DataResponse($stack, HTTP::STATUS_OK); @@ -150,7 +154,7 @@ class StackApiController extends ApiController { return new DataResponse($boardError['message'], $boardError['status']); } - if (is_numeric($this->request->params['stackId']) === false) { + if (is_numeric($this->request->getParam('stackId')) === false) { return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); } @@ -159,10 +163,10 @@ class StackApiController extends ApiController { } try { - $stack = $this->service->update($this->request->params['stackId'], $title, $this->request->getParam('boardId'), $order); + $stack = $this->service->update($this->request->getParam('stackId'), $title, $this->request->getParam('boardId'), $order); if ($stack === false || $stack === null) { - return new DataResponse('Stack not found', HTTP::STATUS_NOT_FOUND); + return new DataResponse('stack not found', HTTP::STATUS_NOT_FOUND); } return new DataResponse($stack, HTTP::STATUS_OK); @@ -176,7 +180,7 @@ class StackApiController extends ApiController { * @CORS * @NoCSRFRequired * - * Delete the stack specified by $this->request->params['stackId']. + * Delete the stack specified by $this->request->getParam('stackId'). */ public function delete() { @@ -186,11 +190,11 @@ class StackApiController extends ApiController { return new DataResponse($boardError['message'], $boardError['status']); } - if (is_numeric($this->request->params['stackId']) === false) { + if (is_numeric($this->request->getParam('stackId')) === false) { return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); } - $stack = $this->service->delete($this->request->params['stackId']); + $stack = $this->service->delete($this->request->getParam('stackId')); if ($stack == false || $stack == null) { return new DataResponse('Stack Not Found', HTTP::STATUS_NOT_FOUND); diff --git a/tests/unit/controller/StackApiControllerTest.php b/tests/unit/controller/StackApiControllerTest.php index 4a8bb93d1..4b02701cc 100644 --- a/tests/unit/controller/StackApiControllerTest.php +++ b/tests/unit/controller/StackApiControllerTest.php @@ -48,7 +48,7 @@ class StackApiControllerTest extends \Test\TestCase { $this->stackService = $this->createMock(StackService::class); $this->exampleStack['id'] = 345; - $this->exampleStack['boardId'] = 245; + $this->exampleStack['boardId'] = $this->exampleBoard['boardId']; $this->exampleStack['order'] = 0; $this->exampleBoard['boardId'] = '89'; @@ -65,7 +65,7 @@ class StackApiControllerTest extends \Test\TestCase { $stack = new Stack(); $stack->setId($this->exampleStack['id']); $stack->setBoardId($this->exampleStack['boardId']); - $stack->setOrder($this->exampleStack['order']); + $stack->setOrder($this->exampleStack['order']); $stacks = [$stack]; $board = new Board(); @@ -110,4 +110,109 @@ class StackApiControllerTest extends \Test\TestCase { $this->assertEquals($expected, $actual); } + public function testGet() { + $stack = new Stack(); + $stack->setId($this->exampleStack['id']); + $stack->setBoardId($this->exampleStack['boardId']); + $stack->setOrder($this->exampleStack['order']); + + $board = new Board(); + $board->setId($this->exampleBoard['boardId']); + $this->boardService->expects($this->once()) + ->method('find') + ->willReturn($board); + + $this->stackService->expects($this->once()) + ->method('find') + ->willReturn($stack); + + $this->request->expects($this->exactly(3)) + ->method('getParam') + ->withConsecutive( + ['boardId'], + ['stackId'], + ['stackId'] + ) + ->willReturnOnConsecutiveCalls( + $this->returnValue($this->exampleBoard['boardId']), + $this->returnValue($this->exampleStack['id']), + $this->returnValue($this->exampleStack['id'])); + + $expected = new DataResponse($stack, HTTP::STATUS_OK); + $actual = $this->controller->get(); + $this->assertEquals($expected, $actual); + } + + public function testGetBadBoardId() { + + $this->request->expects($this->any()) + ->method('getParam') + ->with('boardId') + ->will($this->returnValue('NOT A BOARD ID')); + + $expected = new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); + $actual = $this->controller->get(); + $this->assertEquals($expected, $actual); + } + + public function testGetBoardNotFound() { + $this->request->expects($this->any()) + ->method('getParam') + ->with('boardId') + ->will($this->returnValue(9251)); + + $expected = new DataResponse('board not found', HTTP::STATUS_NOT_FOUND); + $actual = $this->controller->get(); + $this->assertEquals($expected, $actual); + } + + public function testGetStackBadStackId() { + + $board = new Board(); + $board->setId($this->exampleBoard['boardId']); + $this->boardService->expects($this->once()) + ->method('find') + ->willReturn($board); + + $this->request->expects($this->exactly(2)) + ->method('getParam') + ->withConsecutive( + ['boardId'], + ['stackId'] + ) + ->willReturnOnConsecutiveCalls( + $this->returnValue($this->exampleBoard['boardId']), + $this->returnValue('not a stack id'), + $this->returnValue('not a stack id')); + + $expected = new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST); + $actual = $this->controller->get(); + $this->assertEquals($expected, $actual); + } + + public function testGetStackNotFound() { + + $board = new Board(); + $board->setId($this->exampleBoard['boardId']); + $this->boardService->expects($this->once()) + ->method('find') + ->willReturn($board); + + $this->request->expects($this->exactly(3)) + ->method('getParam') + ->withConsecutive( + ['boardId'], + ['stackId'], + ['stackId'] + ) + ->willReturnOnConsecutiveCalls( + $this->returnValue($this->exampleBoard['boardId']), + $this->returnValue(5), + $this->returnValue(5)); + + $expected = new DataResponse('stack not found', HTTP::STATUS_NOT_FOUND); + $actual = $this->controller->get(); + $this->assertEquals($expected, $actual); + } + } \ No newline at end of file