API unit tests
This commit is contained in:
parent
a11b640496
commit
67e25d707e
@ -40,7 +40,7 @@ class Columns extends BaseController {
|
||||
|
||||
$this->apiJson->setSuccess();
|
||||
$this->apiJson->addAlert('success', 'Column ' .
|
||||
$column->name . 'added.');
|
||||
$column->name . ' added.');
|
||||
|
||||
return $this->jsonResponse($response);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ class Comments extends BaseController {
|
||||
public function getComment($request, $response, $args) {
|
||||
$comment = new Comment($this->container, (int)$args['id']);
|
||||
|
||||
if ($comment>id === 0) {
|
||||
if ($comment->id === 0) {
|
||||
$this->logger->addError('Attempt to load comment ' .
|
||||
$args['id'] . ' failed.');
|
||||
$this->apiJson->addAlert('error', 'No column found for ID ' .
|
||||
|
115
test/api/controllers/ColumnsTest.php
Normal file
115
test/api/controllers/ColumnsTest.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../Mocks.php';
|
||||
|
||||
class ColumnsTest extends PHPUnit_Framework_TestCase {
|
||||
private $columns;
|
||||
|
||||
public static function setupBeforeClass() {
|
||||
try {
|
||||
RedBeanPHP\R::setup('sqlite:tests.db');
|
||||
// RedBeanPHP\R::fancyDebug(true);
|
||||
} catch (Exception $ex) { }
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
RedBeanPHP\R::nuke();
|
||||
|
||||
$this->columns = new Columns(new ContainerMock());
|
||||
}
|
||||
|
||||
public function testGetColumn() {
|
||||
$expected = new ApiJson();
|
||||
$expected->addAlert('error', 'No column found for ID 1.');
|
||||
|
||||
$args = [];
|
||||
$args['id'] = 1;
|
||||
|
||||
$actual = $this->columns->getColumn(null,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
$this->createColumn();
|
||||
$actual = $this->columns->getColumn(null,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($actual->status === 'success');
|
||||
$this->assertTrue(count($actual->data) === 1);
|
||||
}
|
||||
|
||||
public function testAddRemoveColumn() {
|
||||
$expected = new ApiJson();
|
||||
|
||||
$actual = $this->createColumn();
|
||||
|
||||
$expected->setSuccess();
|
||||
$expected->addAlert('success', 'Column col1 added.');
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
$expected->addAlert('success', 'Column col1 removed.');
|
||||
|
||||
$args = [];
|
||||
$args['id'] = 1;
|
||||
|
||||
$actual = $this->columns->removeColumn(null,
|
||||
new ResponseMock(), $args);
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testAddBadColumn() {
|
||||
$request = new RequestMock();
|
||||
$request->invalidPayload = true;
|
||||
|
||||
$response = $this->columns->addColumn($request,
|
||||
new ResponseMock(), null);
|
||||
|
||||
$this->assertTrue($response->status === 'failure');
|
||||
$this->assertTrue($response->alerts[0]['type'] === 'error');
|
||||
}
|
||||
|
||||
public function testRemoveBadColumn() {
|
||||
$args = [];
|
||||
$args['id'] = 5; // No such column
|
||||
|
||||
$response = $this->columns->removeColumn(null,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($response->status === 'failure');
|
||||
}
|
||||
|
||||
public function testUpdateColumn() {
|
||||
$this->createColumn();
|
||||
|
||||
$column = DataMock::getColumn();
|
||||
$column->name = 'updated';
|
||||
|
||||
$args = [];
|
||||
$args['id'] = $column->id;
|
||||
|
||||
$request = new RequestMock();
|
||||
$request->payload = $column;
|
||||
|
||||
$response = $this->columns->updateColumn($request,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($response->status === 'success');
|
||||
|
||||
$request->payload = new stdClass();
|
||||
$response = $this->columns->updateColumn($request,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($response->alerts[2]['type'] === 'error');
|
||||
}
|
||||
|
||||
private function createColumn() {
|
||||
$request = new RequestMock();
|
||||
$column = DataMock::getColumn();
|
||||
$column->id = 0;
|
||||
|
||||
$request->payload = $column;
|
||||
|
||||
$response = $this->columns->addColumn($request,
|
||||
new ResponseMock(), null);
|
||||
$this->assertTrue($response->status === 'success');
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
115
test/api/controllers/CommentsTest.php
Normal file
115
test/api/controllers/CommentsTest.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../Mocks.php';
|
||||
|
||||
class CommentsTest extends PHPUnit_Framework_TestCase {
|
||||
private $comments;
|
||||
|
||||
public static function setupBeforeClass() {
|
||||
try {
|
||||
RedBeanPHP\R::setup('sqlite:tests.db');
|
||||
// RedBeanPHP\R::fancyDebug(true);
|
||||
} catch (Exception $ex) { }
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
RedBeanPHP\R::nuke();
|
||||
|
||||
$this->comments = new Comments(new ContainerMock());
|
||||
}
|
||||
|
||||
public function testGetComment() {
|
||||
$expected = new ApiJson();
|
||||
$expected->addAlert('error', 'No column found for ID 1.');
|
||||
|
||||
$args = [];
|
||||
$args['id'] = 1;
|
||||
|
||||
$actual = $this->comments->getComment(null,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
$this->createComment();
|
||||
$actual = $this->comments->getComment(null,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($actual->status === 'success');
|
||||
$this->assertTrue(count($actual->data) === 1);
|
||||
}
|
||||
|
||||
public function testAddRemoveComment() {
|
||||
$expected = new ApiJson();
|
||||
|
||||
$actual = $this->createComment();
|
||||
|
||||
$expected->setSuccess();
|
||||
$expected->addAlert('success', 'Comment added.');
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
$expected->addAlert('success', 'Comment removed.');
|
||||
|
||||
$args = [];
|
||||
$args['id'] = 1;
|
||||
|
||||
$actual = $this->comments->removeComment(null,
|
||||
new ResponseMock(), $args);
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testAddBadComment() {
|
||||
$request = new RequestMock();
|
||||
$request->invalidPayload = true;
|
||||
|
||||
$response = $this->comments->addComment($request,
|
||||
new ResponseMock(), null);
|
||||
|
||||
$this->assertTrue($response->status === 'failure');
|
||||
$this->assertTrue($response->alerts[0]['type'] === 'error');
|
||||
}
|
||||
|
||||
public function testRemoveBadComment() {
|
||||
$args = [];
|
||||
$args['id'] = 5; // No such comment
|
||||
|
||||
$response = $this->comments->removeComment(null,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($response->status === 'failure');
|
||||
}
|
||||
|
||||
public function testUpdateComment() {
|
||||
$this->createComment();
|
||||
|
||||
$comment = DataMock::getComment();
|
||||
$comment->text = 'updated';
|
||||
|
||||
$args = [];
|
||||
$args['id'] = $comment->id;
|
||||
|
||||
$request = new RequestMock();
|
||||
$request->payload = $comment;
|
||||
|
||||
$response = $this->comments->updateComment($request,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($response->status === 'success');
|
||||
|
||||
$request->payload = new stdClass();
|
||||
$response = $this->comments->updateComment($request,
|
||||
new ResponseMock(), $args);
|
||||
$this->assertTrue($response->alerts[2]['type'] === 'error');
|
||||
}
|
||||
|
||||
private function createComment() {
|
||||
$request = new RequestMock();
|
||||
$comment = DataMock::getComment();
|
||||
$comment->id = 0;
|
||||
|
||||
$request->payload = $comment;
|
||||
|
||||
$response = $this->comments->addComment($request,
|
||||
new ResponseMock(), null);
|
||||
$this->assertTrue($response->status === 'success');
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user