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

View File

@@ -50,6 +50,7 @@ class StackApiControllerTest extends \Test\TestCase {
$this->exampleStack['id'] = 345;
$this->exampleStack['boardId'] = $this->exampleBoard['boardId'];
$this->exampleStack['order'] = 0;
$this->exampleStack['title'] = 'Example Stack From API';
$this->exampleBoard['boardId'] = '89';
@@ -129,13 +130,13 @@ class StackApiControllerTest extends \Test\TestCase {
$this->request->expects($this->exactly(3))
->method('getParam')
->withConsecutive(
['boardId'],
['stackId'],
['boardId'],
['stackId']
)
->willReturnOnConsecutiveCalls(
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue($this->exampleStack['id']),
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue($this->exampleStack['id']));
$expected = new DataResponse($stack, HTTP::STATUS_OK);
@@ -145,10 +146,15 @@ class StackApiControllerTest extends \Test\TestCase {
public function testGetBadBoardId() {
$this->request->expects($this->any())
$this->request->expects($this->exactly(2))
->method('getParam')
->with('boardId')
->will($this->returnValue('NOT A BOARD ID'));
->withConsecutive(
['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);
$actual = $this->controller->get();
@@ -156,10 +162,16 @@ class StackApiControllerTest extends \Test\TestCase {
}
public function testGetBoardNotFound() {
$this->request->expects($this->any())
$this->request->expects($this->exactly(2))
->method('getParam')
->with('boardId')
->will($this->returnValue(9251));
->withConsecutive(
['stackId'],
['boardId']
)
->willReturnOnConsecutiveCalls(
$this->returnValue(5),
$this->returnValue($this->exampleBoard['boardId']));
$expected = new DataResponse('board not found', HTTP::STATUS_NOT_FOUND);
$actual = $this->controller->get();
@@ -168,22 +180,10 @@ class StackApiControllerTest extends \Test\TestCase {
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))
$this->request->expects($this->any())
->method('getParam')
->withConsecutive(
['boardId'],
['stackId']
)
->willReturnOnConsecutiveCalls(
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue('not a stack id'),
$this->returnValue('not a stack id'));
->with('stackId')
->will($this->returnValue('not a stack id'));
$expected = new DataResponse('stack id must be a number', HTTP::STATUS_BAD_REQUEST);
$actual = $this->controller->get();
@@ -201,13 +201,13 @@ class StackApiControllerTest extends \Test\TestCase {
$this->request->expects($this->exactly(3))
->method('getParam')
->withConsecutive(
['boardId'],
['stackId'],
['boardId'],
['stackId']
)
->willReturnOnConsecutiveCalls(
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue(5),
$this->returnValue($this->exampleBoard['boardId']),
$this->returnValue(5));
$expected = new DataResponse('stack not found', HTTP::STATUS_NOT_FOUND);
@@ -215,4 +215,54 @@ class StackApiControllerTest extends \Test\TestCase {
$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);
}
}