added the create tests in StackApiControllerTest

Signed-off-by: Ryan Fletcher <ryan.fletcher@codepassion.ca>
This commit is contained in:
Ryan Fletcher
2018-07-15 21:11:49 -04:00
committed by Julius Härtl
parent 5719e9f134
commit 570ac81848
2 changed files with 96 additions and 47 deletions

View File

@@ -85,15 +85,15 @@ class StackApiController extends ApiController {
* Return all of the stacks in the specified board. * Return all of the stacks in the specified board.
*/ */
public function get() { public function get() {
if (is_numeric($this->request->getParam('stackId')) === false) {
return new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST);
}
$boardError = $this->apiHelper->entityHasError($this->request->getParam('boardId'), 'board', $this->boardService); $boardError = $this->apiHelper->entityHasError($this->request->getParam('boardId'), 'board', $this->boardService);
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
} }
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')); $stack = $this->service->find($this->request->getParam('stackId'));
@@ -116,22 +116,21 @@ class StackApiController extends ApiController {
*/ */
public function create($title, $order) { public function create($title, $order) {
$boardError = $this->apiHelper->entityHasError( $this->request->getParam('boardId'), 'board', $this->boardService ); if ($title === false || $title === null) {
return new DataResponse('title must be provided', HTTP::STATUS_BAD_REQUEST);
if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
} }
if (is_numeric($order) === false) { if (is_numeric($order) === false) {
return new DataResponse('order must be a number', HTTP::STATUS_BAD_REQUEST); return new DataResponse('order must be a number', HTTP::STATUS_BAD_REQUEST);
} }
try { $boardError = $this->apiHelper->entityHasError( $this->request->getParam('boardId'), 'board', $this->boardService );
$stack = $this->service->create($title, $this->request->getParam('boardId'), $order);
} catch (StatusException $e) { if ($boardError) {
$errorMessage['error'] = $e->getMessage(); return new DataResponse($boardError['message'], $boardError['status']);
return new DataResponse($errorMessage, HTTP::STATUS_INTERNAL_SERVER_ERROR); }
}
$stack = $this->service->create($title, $this->request->getParam('boardId'), $order);
return new DataResponse($stack, HTTP::STATUS_OK); return new DataResponse($stack, HTTP::STATUS_OK);
} }

View File

@@ -49,7 +49,8 @@ class StackApiControllerTest extends \Test\TestCase {
$this->exampleStack['id'] = 345; $this->exampleStack['id'] = 345;
$this->exampleStack['boardId'] = $this->exampleBoard['boardId']; $this->exampleStack['boardId'] = $this->exampleBoard['boardId'];
$this->exampleStack['order'] = 0; $this->exampleStack['order'] = 0;
$this->exampleStack['title'] = 'Example Stack From API';
$this->exampleBoard['boardId'] = '89'; $this->exampleBoard['boardId'] = '89';
@@ -128,14 +129,14 @@ class StackApiControllerTest extends \Test\TestCase {
$this->request->expects($this->exactly(3)) $this->request->expects($this->exactly(3))
->method('getParam') ->method('getParam')
->withConsecutive( ->withConsecutive(
['boardId'],
['stackId'], ['stackId'],
['boardId'],
['stackId'] ['stackId']
) )
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue($this->exampleStack['id']), $this->returnValue($this->exampleStack['id']),
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue($this->exampleStack['id'])); $this->returnValue($this->exampleStack['id']));
$expected = new DataResponse($stack, HTTP::STATUS_OK); $expected = new DataResponse($stack, HTTP::STATUS_OK);
@@ -145,45 +146,44 @@ class StackApiControllerTest extends \Test\TestCase {
public function testGetBadBoardId() { public function testGetBadBoardId() {
$this->request->expects($this->any()) $this->request->expects($this->exactly(2))
->method('getParam') ->method('getParam')
->with('boardId') ->withConsecutive(
->will($this->returnValue('NOT A BOARD ID')); ['stackId'],
['boardId']
)
->willReturnOnConsecutiveCalls(
$this->returnValue(5),
$this->returnValue('not a board id'));
$expected = new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); $expected = new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST);
$actual = $this->controller->get(); $actual = $this->controller->get();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testGetBoardNotFound() { public function testGetBoardNotFound() {
$this->request->expects($this->any()) $this->request->expects($this->exactly(2))
->method('getParam') ->method('getParam')
->with('boardId') ->withConsecutive(
->will($this->returnValue(9251)); ['stackId'],
['boardId']
)
->willReturnOnConsecutiveCalls(
$this->returnValue(5),
$this->returnValue($this->exampleBoard['boardId']));
$expected = new DataResponse('board not found', HTTP::STATUS_NOT_FOUND); $expected = new DataResponse('board not found', HTTP::STATUS_NOT_FOUND);
$actual = $this->controller->get(); $actual = $this->controller->get();
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testGetStackBadStackId() { public function testGetStackBadStackId() {
$board = new Board(); $this->request->expects($this->any())
$board->setId($this->exampleBoard['boardId']);
$this->boardService->expects($this->once())
->method('find')
->willReturn($board);
$this->request->expects($this->exactly(2))
->method('getParam') ->method('getParam')
->withConsecutive( ->with('stackId')
['boardId'], ->will($this->returnValue('not a stack id'));
['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); $expected = new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST);
$actual = $this->controller->get(); $actual = $this->controller->get();
@@ -196,18 +196,18 @@ class StackApiControllerTest extends \Test\TestCase {
$board->setId($this->exampleBoard['boardId']); $board->setId($this->exampleBoard['boardId']);
$this->boardService->expects($this->once()) $this->boardService->expects($this->once())
->method('find') ->method('find')
->willReturn($board); ->willReturn($board);
$this->request->expects($this->exactly(3)) $this->request->expects($this->exactly(3))
->method('getParam') ->method('getParam')
->withConsecutive( ->withConsecutive(
['boardId'],
['stackId'], ['stackId'],
['boardId'],
['stackId'] ['stackId']
) )
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue(5), $this->returnValue(5),
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue(5)); $this->returnValue(5));
$expected = new DataResponse('stack not found', HTTP::STATUS_NOT_FOUND); $expected = new DataResponse('stack not found', HTTP::STATUS_NOT_FOUND);
@@ -215,4 +215,54 @@ class StackApiControllerTest extends \Test\TestCase {
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testCreate() {
$board = new Board();
$board->setId($this->exampleBoard['boardId']);
$this->boardService->expects($this->once())
->method('find')
->willReturn($board);
$this->request->expects($this->any())
->method('getParam')
->with('boardId')
->will($this->returnValue($this->exampleBoard['boardId']));
$stack = new Stack();
$stack->setId($this->exampleStack['id']);
$stack->setBoardId($this->exampleStack['boardId']);
$stack->setOrder($this->exampleStack['order']);
$stack->setTitle($this->exampleStack['title']);
$this->stackService->expects($this->once())
->method('create')
->willReturn($stack);
$expected = new DataResponse($stack, HTTP::STATUS_OK);
$actual = $this->controller->create($this->exampleStack['title'], $this->exampleStack['order']);
$this->assertEquals($expected, $actual);
}
public function testCreateBadOrder() {
$expected = new DataResponse('order must be a number', HTTP::STATUS_BAD_REQUEST);
$actual = $this->controller->create($this->exampleStack['title'], 'not an order number');
$this->assertEquals($expected, $actual);
}
public function testCreateBadTitle() {
$expected = new DataResponse('title must be provided', HTTP::STATUS_BAD_REQUEST);
$actual = $this->controller->create(null, 999);
$this->assertEquals($expected, $actual);
}
public function testCreateBoardNotFound() {
$this->request->expects($this->once())
->method('getParam')
->will($this->returnValue(453));
$expected = new DataResponse('board not found', HTTP::STATUS_NOT_FOUND);
$actual = $this->controller->create($this->exampleStack['title'], $this->exampleStack['order']);
$this->assertEquals($expected, $actual);
}
} }