From 833503241f5be699a5e0af4425a176c1c20142b4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 2 Nov 2021 21:57:09 +0100 Subject: [PATCH] add unit tests --- Controller/ApiController.php | 6 +- Models/KanbanBoard.php | 32 +- Models/KanbanCard.php | 69 +++-- Models/KanbanCardComment.php | 48 ++- Models/KanbanColumn.php | 46 ++- Models/NullKanbanBoard.php | 1 + Models/NullKanbanCard.php | 39 +++ Models/NullKanbanCardComment.php | 39 +++ Models/NullKanbanColumn.php | 38 +++ tests/Controller/ApiControllerTest.php | 273 ++++++++++++++++++ tests/Controller/test.md | 3 + tests/ControllerTest.php | 141 --------- tests/Models/1KanbanBoardMapperTest.php | 19 -- tests/Models/3KanbanColumnMapperTest.php | 23 +- tests/Models/4KanbanCardMapperTest.php | 31 +- tests/Models/5KanbanCardCommentMapperTest.php | 23 +- tests/Models/KanbanBoardTest.php | 97 +++++-- tests/Models/KanbanCardCommentTest.php | 62 ++-- tests/Models/KanbanCardTest.php | 173 ++++++++--- tests/Models/KanbanColumnTest.php | 71 ++++- tests/Models/NullKanbanCardCommentTest.php | 42 +++ tests/Models/NullKanbanCardTest.php | 42 +++ tests/Models/NullKanbanColumnTest.php | 42 +++ tests/phpunit_default.xml | 2 +- 24 files changed, 944 insertions(+), 418 deletions(-) create mode 100644 Models/NullKanbanCard.php create mode 100644 Models/NullKanbanCardComment.php create mode 100644 Models/NullKanbanColumn.php create mode 100644 tests/Controller/ApiControllerTest.php create mode 100644 tests/Controller/test.md delete mode 100755 tests/ControllerTest.php create mode 100644 tests/Models/NullKanbanCardCommentTest.php create mode 100644 tests/Models/NullKanbanCardTest.php create mode 100644 tests/Models/NullKanbanColumnTest.php diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 7b92f1b..a0cb024 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -89,7 +89,7 @@ final class ApiController extends Controller $card->descriptionRaw = (string) ($request->getData('plain') ?? ''); $card->description = Markdown::parse((string) ($request->getData('plain') ?? '')); $card->style = (string) ($request->getData('style') ?? ''); - $card->setColumn((int) $request->getData('column')); + $card->column = (int) $request->getData('column'); $card->order = (int) ($request->getData('order') ?? 1); $card->ref = (int) ($request->getData('ref') ?? 0); $card->setStatus((int) ($request->getData('status') ?? CardStatus::ACTIVE)); @@ -207,7 +207,7 @@ final class ApiController extends Controller $comment = new KanbanCardComment(); $comment->description = Markdown::parse((string) ($request->getData('plain') ?? '')); $comment->descriptionRaw = (string) ($request->getData('plain') ?? ''); - $comment->setCard((int) $request->getData('card')); + $comment->card = (int) $request->getData('card'); $comment->createdBy = new NullAccount($request->header->account); if (!empty($uploadedFiles = $request->getFiles() ?? [])) { @@ -423,7 +423,7 @@ final class ApiController extends Controller { $column = new KanbanColumn(); $column->name = (string) $request->getData('title'); - $column->setBoard((int) $request->getData('board')); + $column->board = (int) $request->getData('board'); $column->order = (int) ($request->getData('order') ?? 1); return $column; diff --git a/Models/KanbanBoard.php b/Models/KanbanBoard.php index 685f263..0363fd3 100755 --- a/Models/KanbanBoard.php +++ b/Models/KanbanBoard.php @@ -17,6 +17,7 @@ namespace Modules\Kanban\Models; use Modules\Admin\Models\Account; use Modules\Admin\Models\NullAccount; use Modules\Tag\Models\Tag; +use Modules\Tag\Models\NullTag; /** * Kanban board class. @@ -191,6 +192,20 @@ class KanbanBoard implements \JsonSerializable $this->tags[] = $tag; } + /** + * Get task elements. + * + * @param int $id Element id + * + * @return Tag + * + * @since 1.0.0 + */ + public function getTag(int $id) : Tag + { + return $this->tags[$id] ?? new NullTag(); + } + /** * Get the columns * @@ -240,8 +255,21 @@ class KanbanBoard implements \JsonSerializable /** * {@inheritdoc} */ - public function jsonSerialize() : array + public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'status' => $this->status, + 'columns' => $this->columns, + 'tags' => $this->tags, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); } } diff --git a/Models/KanbanCard.php b/Models/KanbanCard.php index a848f45..d81beed 100755 --- a/Models/KanbanCard.php +++ b/Models/KanbanCard.php @@ -18,6 +18,7 @@ use Modules\Admin\Models\Account; use Modules\Admin\Models\NullAccount; use Modules\Media\Models\Media; use Modules\Tag\Models\Tag; +use Modules\Tag\Models\NullTag; use Modules\Tasks\Models\Task; /** @@ -108,7 +109,7 @@ class KanbanCard implements \JsonSerializable * @var int * @since 1.0.0 */ - private int $column = 0; + public int $column = 0; /** * Card order/position. @@ -183,32 +184,6 @@ class KanbanCard implements \JsonSerializable return $this->id; } - /** - * Set the column - * - * @param int $id Id - * - * @return void - * - * @since 1.0.0 - */ - public function setColumn(int $id) : void - { - $this->column = $id; - } - - /** - * Get the column - * - * @return int - * - * @since 1.0.0 - */ - public function getColumn() : int - { - return $this->column; - } - /** * Get the status * @@ -355,6 +330,20 @@ class KanbanCard implements \JsonSerializable return $this->tags; } + /** + * Get task elements. + * + * @param int $id Element id + * + * @return Tag + * + * @since 1.0.0 + */ + public function getTag(int $id) : Tag + { + return $this->tags[$id] ?? new NullTag(); + } + /** * Add tag * @@ -372,23 +361,33 @@ class KanbanCard implements \JsonSerializable /** * {@inheritdoc} */ - public function jsonSerialize() : array + public function toArray() : array { return [ + 'id' => $this->id, 'title' => $this->name, 'description' => $this->description, + 'descriptionRaw' => $this->descriptionRaw, 'status' => $this->status, 'type' => $this->type, - 'column' => $this->name, - 'order' => $this->name, - 'ref' => $this->name, - 'createdBy' => $this->name, - 'createdAt' => $this->name, - 'comments' => $this->name, - 'media' => $this->name, + 'column' => $this->column, + 'order' => $this->order, + 'ref' => $this->ref, + 'createdBy' => $this->createdBy, + 'createdAt' => $this->createdAt, + 'comments' => $this->comments, + 'media' => $this->media, ]; } + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); + } + /** * Create a task from a card * diff --git a/Models/KanbanCardComment.php b/Models/KanbanCardComment.php index a0691eb..4a20b5e 100755 --- a/Models/KanbanCardComment.php +++ b/Models/KanbanCardComment.php @@ -57,7 +57,7 @@ class KanbanCardComment implements \JsonSerializable * @var int * @since 1.0.0 */ - private int $card = 0; + public int $card = 0; /** * Created by. @@ -106,32 +106,6 @@ class KanbanCardComment implements \JsonSerializable return $this->id; } - /** - * Set the card - * - * @param int $id Card id - * - * @return void - * - * @since 1.0.0 - */ - public function setCard(int $id) : void - { - $this->card = $id; - } - - /** - * Get the card - * - * @return int - * - * @since 1.0.0 - */ - public function getCard() : int - { - return $this->card; - } - /** * Get the media files * @@ -161,8 +135,24 @@ class KanbanCardComment implements \JsonSerializable /** * {@inheritdoc} */ - public function jsonSerialize() : array + public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'description' => $this->description, + 'descriptionRaw' => $this->descriptionRaw, + 'card' => $this->card, + 'createdBy' => $this->createdBy, + 'createdAt' => $this->createdAt, + 'media' => $this->media, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); } } diff --git a/Models/KanbanColumn.php b/Models/KanbanColumn.php index eb619f3..baa7749 100755 --- a/Models/KanbanColumn.php +++ b/Models/KanbanColumn.php @@ -54,7 +54,7 @@ class KanbanColumn implements \JsonSerializable * @var int * @since 1.0.0 */ - private int $board = 0; + public int $board = 0; /** * Cards. @@ -76,32 +76,6 @@ class KanbanColumn implements \JsonSerializable return $this->id; } - /** - * Get the board this column belongs to - * - * @param int $board Board - * - * @return void - * - * @since 1.0.0 - */ - public function setBoard(int $board) : void - { - $this->board = $board; - } - - /** - * Get the board this column belongs to - * - * @return int - * - * @since 1.0.0 - */ - public function getBoard() : int - { - return $this->board; - } - /** * Get the cards * @@ -151,8 +125,22 @@ class KanbanColumn implements \JsonSerializable /** * {@inheritdoc} */ - public function jsonSerialize() : array + public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'name' => $this->name, + 'order' => $this->order, + 'board' => $this->board, + 'cards' => $this->cards, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); } } diff --git a/Models/NullKanbanBoard.php b/Models/NullKanbanBoard.php index d6d00c6..cf82c87 100755 --- a/Models/NullKanbanBoard.php +++ b/Models/NullKanbanBoard.php @@ -33,6 +33,7 @@ final class NullKanbanBoard extends KanbanBoard */ public function __construct(int $id = 0) { + parent::__construct(); $this->id = $id; } } diff --git a/Models/NullKanbanCard.php b/Models/NullKanbanCard.php new file mode 100644 index 0000000..a4c66f6 --- /dev/null +++ b/Models/NullKanbanCard.php @@ -0,0 +1,39 @@ +id = $id; + } +} diff --git a/Models/NullKanbanCardComment.php b/Models/NullKanbanCardComment.php new file mode 100644 index 0000000..6c0dda5 --- /dev/null +++ b/Models/NullKanbanCardComment.php @@ -0,0 +1,39 @@ +id = $id; + } +} diff --git a/Models/NullKanbanColumn.php b/Models/NullKanbanColumn.php new file mode 100644 index 0000000..3abf92b --- /dev/null +++ b/Models/NullKanbanColumn.php @@ -0,0 +1,38 @@ +id = $id; + } +} diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php new file mode 100644 index 0000000..3f83bdd --- /dev/null +++ b/tests/Controller/ApiControllerTest.php @@ -0,0 +1,273 @@ +app = new class() extends ApplicationAbstract + { + protected string $appName = 'Api'; + }; + + $this->app->dbPool = $GLOBALS['dbpool']; + $this->app->orgId = 1; + $this->app->accountManager = new AccountManager($GLOBALS['session']); + $this->app->appSettings = new CoreSettings(); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../Modules/'); + $this->app->dispatcher = new Dispatcher($this->app); + $this->app->eventManager = new EventManager($this->app->dispatcher); + $this->app->eventManager->importFromFile(__DIR__ . '/../../../Web/Api/Hooks.php'); + + $account = new Account(); + TestUtils::setMember($account, 'id', 1); + + $permission = new AccountPermission(); + $permission->setUnit(1); + $permission->setApp('backend'); + $permission->setPermission( + PermissionType::READ + | PermissionType::CREATE + | PermissionType::MODIFY + | PermissionType::DELETE + | PermissionType::PERMISSION + ); + + $account->addPermission($permission); + + $this->app->accountManager->add($account); + $this->app->router = new WebRouter(); + + $this->module = $this->app->moduleManager->get('Kanban'); + + TestUtils::setMember($this->module, 'app', $this->app); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testApiBoardCU() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', 'Controller Test Board'); + $request->setData('plain', 'Controller Test Description'); + $request->setData('tags', '[{"title": "TestTitle", "color": "#f0f", "language": "en"}, {"id": 1}]'); + + $this->module->apiKanbanBoardCreate($request, $response); + + self::assertEquals('Controller Test Board', $response->get('')['response']->name); + self::assertGreaterThan(0, $bId = $response->get('')['response']->getId()); + + // update + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', $bId); + $request->setData('title', 'New Controller Test Board'); + + $this->module->apiKanbanBoardUpdate($request, $response); + self::assertEquals('New Controller Test Board', $response->get('')['response']->name); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testCreateColumn() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', 'Controller Test Column'); + $request->setData('board', 1); + + $this->module->apiKanbanColumnCreate($request, $response); + + self::assertEquals('Controller Test Column', $response->get('')['response']->name); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testCreateCard() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', 'Controller Test Card'); + $request->setData('plain', 'Controller Test Description'); + $request->setData('column', '1'); + $request->setData('tags', '[{"title": "TestTitle", "color": "#f0f", "language": "en"}, {"id": 1}]'); + + if (!\is_file(__DIR__ . '/test_tmp.md')) { + \copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md'); + } + + TestUtils::setMember($request, 'files', [ + 'file1' => [ + 'name' => 'test.md', + 'type' => MimeType::M_TXT, + 'tmp_name' => __DIR__ . '/test_tmp.md', + 'error' => \UPLOAD_ERR_OK, + 'size' => \filesize(__DIR__ . '/test_tmp.md'), + ], + ]); + + $request->setData('media', \json_encode([1])); + + $this->module->apiKanbanCardCreate($request, $response); + + self::assertEquals('Controller Test Card', $response->get('')['response']->name); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testCreateCommentCard() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('plain', 'Controller Test Description'); + $request->setData('card', '1'); + + if (!\is_file(__DIR__ . '/test_tmp.md')) { + \copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md'); + } + + TestUtils::setMember($request, 'files', [ + 'file1' => [ + 'name' => 'test.md', + 'type' => MimeType::M_TXT, + 'tmp_name' => __DIR__ . '/test_tmp.md', + 'error' => \UPLOAD_ERR_OK, + 'size' => \filesize(__DIR__ . '/test_tmp.md'), + ], + ]); + + $request->setData('media', \json_encode([1])); + + $this->module->apiKanbanCardCommentCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testApiKanbanBoardCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiKanbanBoardCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testApiKanbanColumnCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiKanbanColumnCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testApiKanbanCardCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiKanbanCardCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Kanban\Controller\ApiController + * @group module + */ + public function testApiKanbanCardCommentCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiKanbanCardCommentCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } +} diff --git a/tests/Controller/test.md b/tests/Controller/test.md new file mode 100644 index 0000000..d35f959 --- /dev/null +++ b/tests/Controller/test.md @@ -0,0 +1,3 @@ +# Test Title + +Some **test** text. \ No newline at end of file diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php deleted file mode 100755 index d8ae1c3..0000000 --- a/tests/ControllerTest.php +++ /dev/null @@ -1,141 +0,0 @@ -app = new class() extends ApplicationAbstract - { - protected string $appName = 'Api'; - }; - - $this->app->dbPool = $GLOBALS['dbpool']; - $this->app->orgId = 1; - $this->app->accountManager = new AccountManager($GLOBALS['session']); - $this->app->appSettings = new CoreSettings(); - $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../Modules/'); - $this->app->dispatcher = new Dispatcher($this->app); - $this->app->eventManager = new EventManager($this->app->dispatcher); - $this->app->eventManager->importFromFile(__DIR__ . '/../../../Web/Api/Hooks.php'); - - $account = new Account(); - TestUtils::setMember($account, 'id', 1); - - $permission = new AccountPermission(); - $permission->setUnit(1); - $permission->setApp('backend'); - $permission->setPermission( - PermissionType::READ - | PermissionType::CREATE - | PermissionType::MODIFY - | PermissionType::DELETE - | PermissionType::PERMISSION - ); - - $account->addPermission($permission); - - $this->app->accountManager->add($account); - $this->app->router = new WebRouter(); - - $this->module = $this->app->moduleManager->get('Kanban'); - - TestUtils::setMember($this->module, 'app', $this->app); - } - - /** - * @covers Modules\Kanban\Controller\ApiController - * @group module - */ - public function testCreateBoard() : void - { - $response = new HttpResponse(); - $request = new HttpRequest(new HttpUri('')); - - $request->header->account = 1; - $request->setData('title', 'Controller Test Board'); - $request->setData('plain', 'Controller Test Description'); - - $this->module->apiKanbanBoardCreate($request, $response); - - self::assertEquals('Controller Test Board', $response->get('')['response']->name); - self::assertGreaterThan(0, $response->get('')['response']->getId()); - } - - /** - * @covers Modules\Kanban\Controller\ApiController - * @group module - */ - public function testCreateColumn() : void - { - $response = new HttpResponse(); - $request = new HttpRequest(new HttpUri('')); - - $request->header->account = 1; - $request->setData('title', 'Controller Test Column'); - $request->setData('board', 1); - - $this->module->apiKanbanColumnCreate($request, $response); - - self::assertEquals('Controller Test Column', $response->get('')['response']->name); - self::assertGreaterThan(0, $response->get('')['response']->getId()); - } - - /** - * @covers Modules\Kanban\Controller\ApiController - * @group module - */ - public function testCreateCard() : void - { - $response = new HttpResponse(); - $request = new HttpRequest(new HttpUri('')); - - $request->header->account = 1; - $request->setData('title', 'Controller Test Card'); - $request->setData('plain', 'Controller Test Description'); - $request->setData('column', '1'); - - $this->module->apiKanbanCardCreate($request, $response); - - self::assertEquals('Controller Test Card', $response->get('')['response']->name); - self::assertGreaterThan(0, $response->get('')['response']->getId()); - } -} diff --git a/tests/Models/1KanbanBoardMapperTest.php b/tests/Models/1KanbanBoardMapperTest.php index 5a8f91a..0162e9f 100755 --- a/tests/Models/1KanbanBoardMapperTest.php +++ b/tests/Models/1KanbanBoardMapperTest.php @@ -48,23 +48,4 @@ final class KanbanBoardMapperTest extends \PHPUnit\Framework\TestCase self::assertEquals($board->createdAt->format('Y-m-d'), $boardR->createdAt->format('Y-m-d')); self::assertEquals($board->getColumns(), $boardR->getColumns()); } - - /** - * @group volume - * @group module - * @coversNothing - */ - public function testVolume() : void - { - for ($i = 1; $i < 30; ++$i) { - $text = new Text(); - $board = new KanbanBoard(); - - $board->name = $text->generateText(\mt_rand(3, 7)); - $board->description = $text->generateText(\mt_rand(20, 70)); - $board->createdBy = new NullAccount(1); - - $id = KanbanBoardMapper::create($board); - } - } } diff --git a/tests/Models/3KanbanColumnMapperTest.php b/tests/Models/3KanbanColumnMapperTest.php index 8dd006a..c827f21 100755 --- a/tests/Models/3KanbanColumnMapperTest.php +++ b/tests/Models/3KanbanColumnMapperTest.php @@ -32,7 +32,7 @@ final class KanbanColumnMapperTest extends \PHPUnit\Framework\TestCase $column = new KanbanColumn(); $column->name = 'Some Column'; - $column->setBoard(1); + $column->board = 1; $column->order = 1; $id = KanbanColumnMapper::create($column); @@ -41,26 +41,7 @@ final class KanbanColumnMapperTest extends \PHPUnit\Framework\TestCase $columnR = KanbanColumnMapper::get($column->getId()); self::assertEquals($column->name, $columnR->name); - self::assertEquals($column->getBoard(), $columnR->getBoard()); + self::assertEquals($column->board, $columnR->board); self::assertEquals($column->order, $columnR->order); } - - /** - * @group volume - * @group module - * @coversNothing - */ - public function testVolume() : void - { - for ($i = 1; $i < 4; ++$i) { - $text = new Text(); - $column = new KanbanColumn(); - - $column->name = $text->generateText(\mt_rand(3, 7)); - $column->setBoard(1); - $column->order = $i + 1; - - $id = KanbanColumnMapper::create($column); - } - } } diff --git a/tests/Models/4KanbanCardMapperTest.php b/tests/Models/4KanbanCardMapperTest.php index 708eb86..572bcd5 100755 --- a/tests/Models/4KanbanCardMapperTest.php +++ b/tests/Models/4KanbanCardMapperTest.php @@ -41,7 +41,7 @@ final class KanbanCardMapperTest extends \PHPUnit\Framework\TestCase $card->setStatus(CardStatus::ACTIVE); $card->setType(CardType::TEXT); $card->order = 1; - $card->setColumn(1); + $card->column = 1; $card->createdBy = new NullAccount(1); $card->addTag(new Tag()); $card->addTag(new Tag()); @@ -53,7 +53,7 @@ final class KanbanCardMapperTest extends \PHPUnit\Framework\TestCase $cardR = KanbanCardMapper::get($card->getId()); self::assertEquals($card->name, $cardR->name); self::assertEquals($card->description, $cardR->description); - self::assertEquals($card->getColumn(), $cardR->getColumn()); + self::assertEquals($card->column, $cardR->column); self::assertEquals($card->order, $cardR->order); self::assertEquals($card->getStatus(), $cardR->getStatus()); self::assertEquals($card->getType(), $cardR->getType()); @@ -74,7 +74,7 @@ final class KanbanCardMapperTest extends \PHPUnit\Framework\TestCase $card->setType(CardType::TASK); $card->ref = 1; $card->order = 1; - $card->setColumn(1); + $card->column = 1; $card->createdBy = new NullAccount(1); $card->addTag(new Tag()); $card->addTag(new Tag()); @@ -83,29 +83,4 @@ final class KanbanCardMapperTest extends \PHPUnit\Framework\TestCase self::assertGreaterThan(0, $card->getId()); self::assertEquals($id, $card->getId()); } - - /** - * @group volume - * @group module - * @coversNothing - */ - public function testVolume() : void - { - for ($i = 1; $i < 10; ++$i) { - $text = new Text(); - $card = new KanbanCard(); - - $card->name = $text->generateText(\mt_rand(3, 7)); - $card->description = $text->generateText(\mt_rand(20, 100)); - $card->setStatus(CardStatus::ACTIVE); - $card->setType(CardType::TEXT); - $card->order = \mt_rand(1, 10); - $card->setColumn(\mt_rand(1, 4)); - $card->createdBy = new NullAccount(1); - $card->addTag(new NullTag(1)); - $card->addTag(new NullTag(2)); - - $id = KanbanCardMapper::create($card); - } - } } diff --git a/tests/Models/5KanbanCardCommentMapperTest.php b/tests/Models/5KanbanCardCommentMapperTest.php index c8d2961..0ad6d4d 100755 --- a/tests/Models/5KanbanCardCommentMapperTest.php +++ b/tests/Models/5KanbanCardCommentMapperTest.php @@ -33,7 +33,7 @@ final class KanbanCardCommentMapperTest extends \PHPUnit\Framework\TestCase $comment = new KanbanCardComment(); $comment->description = 'This is some card description'; - $comment->setCard(1); + $comment->card = 1; $comment->createdBy = new NullAccount(1); $id = KanbanCardCommentMapper::create($comment); @@ -42,27 +42,8 @@ final class KanbanCardCommentMapperTest extends \PHPUnit\Framework\TestCase $commentR = KanbanCardCommentMapper::get($comment->getId()); self::assertEquals($comment->description, $commentR->description); - self::assertEquals($comment->getCard(), $commentR->getCard()); + self::assertEquals($comment->card, $commentR->card); self::assertEquals($comment->createdBy->getId(), $commentR->createdBy->getId()); self::assertEquals($comment->createdAt->format('Y-m-d'), $commentR->createdAt->format('Y-m-d')); } - - /** - * @group volume - * @group module - * @coversNothing - */ - public function testVolume() : void - { - for ($i = 1; $i < 10; ++$i) { - $text = new Text(); - $comment = new KanbanCardComment(); - - $comment->description = $text->generateText(\mt_rand(20, 100)); - $comment->setCard(1); - $comment->createdBy = new NullAccount(1); - - $id = KanbanCardCommentMapper::create($comment); - } - } } diff --git a/tests/Models/KanbanBoardTest.php b/tests/Models/KanbanBoardTest.php index 8e10d67..053532c 100755 --- a/tests/Models/KanbanBoardTest.php +++ b/tests/Models/KanbanBoardTest.php @@ -17,47 +17,102 @@ namespace Modules\Kanban\tests\Models; use Modules\Admin\Models\NullAccount; use Modules\Kanban\Models\BoardStatus; use Modules\Kanban\Models\KanbanBoard; +use Modules\Tag\Models\Tag; /** * @internal */ final class KanbanBoardTest extends \PHPUnit\Framework\TestCase { + private KanbanBoard $board; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->board = new KanbanBoard(); + } + /** * @covers Modules\Kanban\Models\KanbanBoard * @group module */ public function testDefault() : void { - $board = new KanbanBoard(); - - self::assertEquals(0, $board->getId()); - self::assertEquals(BoardStatus::ACTIVE, $board->getStatus()); - self::assertEquals('', $board->name); - self::assertEquals('', $board->description); - self::assertEquals(0, $board->createdBy->getId()); - self::assertInstanceOf('\DateTimeImmutable', $board->createdAt); - self::assertEquals([], $board->getColumns()); + self::assertEquals(0, $this->board->getId()); + self::assertEquals(BoardStatus::ACTIVE, $this->board->getStatus()); + self::assertEquals('', $this->board->name); + self::assertEquals('', $this->board->description); + self::assertEquals(0, $this->board->createdBy->getId()); + self::assertInstanceOf('\DateTimeImmutable', $this->board->createdAt); + self::assertEquals([], $this->board->getColumns()); } /** * @covers Modules\Kanban\Models\KanbanBoard * @group module */ - public function testSetGet() : void + public function testStatusInputOutput() : void { - $board = new KanbanBoard(); + $this->board->setStatus(BoardStatus::ARCHIVED); + self::assertEquals(BoardStatus::ARCHIVED, $this->board->getStatus()); + } - $board->name = 'Name'; - $board->description = 'Description'; - $board->setStatus(BoardStatus::ARCHIVED); - $board->createdBy = new NullAccount(1); - $board->addColumn(2); + /** + * @covers Modules\Kanban\Models\KanbanBoard + * @group module + */ + public function testColumnsInputOutput() : void + { + $this->board->addColumn(2); + self::assertEquals([2], $this->board->getColumns()); + } - self::assertEquals(BoardStatus::ARCHIVED, $board->getStatus()); - self::assertEquals('Name', $board->name); - self::assertEquals('Description', $board->description); - self::assertEquals(1, $board->createdBy->getId()); - self::assertEquals([2], $board->getColumns()); + /** + * @covers Modules\Kanban\Models\KanbanBoard + * @group module + */ + public function testColumnRemove() : void + { + $this->board->addColumn(2); + self::assertTrue($this->board->removeColumn(0)); + self::assertCount(0, $this->board->getColumns()); + self::assertFalse($this->board->removeColumn(0)); + } + + /** + * @covers Modules\Kanban\Models\KanbanBoard + * @group module + */ + public function testTagInputOutput() : void + { + $tag = new Tag(); + $tag->setL11n('Tag'); + + $this->board->addTag($tag); + self::assertEquals($tag, $this->board->getTag(0)); + self::assertCount(1, $this->board->getTags()); + } + + /** + * @covers Modules\Kanban\Models\KanbanBoard + * @group module + */ + public function testSerialize() : void + { + $this->board->setStatus(BoardStatus::ARCHIVED); + + $serialized = $this->board->jsonSerialize(); + + self::assertEquals( + [ + 'id' => 0, + 'status' => BoardStatus::ARCHIVED, + 'columns' => [], + 'tags' => [], + ], + $serialized + ); } } diff --git a/tests/Models/KanbanCardCommentTest.php b/tests/Models/KanbanCardCommentTest.php index 060bf45..2bb0443 100755 --- a/tests/Models/KanbanCardCommentTest.php +++ b/tests/Models/KanbanCardCommentTest.php @@ -16,44 +16,70 @@ namespace Modules\Kanban\tests\Models; use Modules\Admin\Models\NullAccount; use Modules\Kanban\Models\KanbanCardComment; +use Modules\Media\Models\NullMedia; /** * @internal */ final class KanbanCardCommentTest extends \PHPUnit\Framework\TestCase { + private KanbanCardComment $comment; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->comment = new KanbanCardComment(); + } + /** * @covers Modules\Kanban\Models\KanbanCardComment * @group module */ public function testDefault() : void { - $comment = new KanbanCardComment(); - - self::assertEquals(0, $comment->getId()); - self::assertEquals(0, $comment->getCard()); - self::assertEquals('', $comment->description); - self::assertEquals(0, $comment->createdBy->getId()); - self::assertInstanceOf('\DateTimeImmutable', $comment->createdAt); - self::assertEquals([], $comment->getMedia()); + self::assertEquals(0, $this->comment->getId()); + self::assertEquals(0, $this->comment->card); + self::assertEquals('', $this->comment->description); + self::assertEquals(0, $this->comment->createdBy->getId()); + self::assertInstanceOf('\DateTimeImmutable', $this->comment->createdAt); + self::assertEquals([], $this->comment->getMedia()); } /** * @covers Modules\Kanban\Models\KanbanCardComment * @group module */ - public function testSetGet() : void + public function testMediaInputOutput() : void { - $comment = new KanbanCardComment(); + $this->comment->addMedia($m = new NullMedia(7)); + self::assertCount(1, $this->comment->getMedia()); + } - $comment->setCard(2); - $comment->description = 'Description'; - $comment->createdBy = new NullAccount(1); - $comment->addMedia(3); + /** + * @covers Modules\Kanban\Models\KanbanCardComment + * @group module + */ + public function testSerialize() : void + { + $this->comment->description = 'Description'; + $this->comment->descriptionRaw = 'DescriptionRaw'; + $this->comment->card = 2; - self::assertEquals(2, $comment->getCard()); - self::assertEquals('Description', $comment->description); - self::assertEquals(1, $comment->createdBy->getId()); - self::assertEquals([3], $comment->getMedia()); + $serialized = $this->comment->jsonSerialize(); + unset($serialized['createdBy']); + unset($serialized['createdAt']); + + self::assertEquals( + [ + 'id' => 0, + 'description' => 'Description', + 'descriptionRaw' => 'DescriptionRaw', + 'card' => 2, + 'media' => [], + ], + $serialized + ); } } diff --git a/tests/Models/KanbanCardTest.php b/tests/Models/KanbanCardTest.php index f5412c0..fcf3afe 100755 --- a/tests/Models/KanbanCardTest.php +++ b/tests/Models/KanbanCardTest.php @@ -19,59 +19,164 @@ use Modules\Kanban\Models\CardStatus; use Modules\Kanban\Models\CardType; use Modules\Kanban\Models\KanbanCard; use Modules\Media\Models\NullMedia; +use Modules\Tasks\Models\Task; +use Modules\Tag\Models\Tag; /** * @internal */ final class KanbanCardTest extends \PHPUnit\Framework\TestCase { + private KanbanCard $card; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->card = new KanbanCard(); + } + /** * @covers Modules\Kanban\Models\KanbanCard * @group module */ public function testDefault() : void { - $card = new KanbanCard(); - - self::assertEquals(0, $card->getId()); - self::assertEquals(CardStatus::ACTIVE, $card->getStatus()); - self::assertEquals(CardType::TEXT, $card->getType()); - self::assertEquals('', $card->name); - self::assertEquals('', $card->description); - self::assertEquals(0, $card->getColumn()); - self::assertEquals(0, $card->order); - self::assertEquals(0, $card->createdBy->getId()); - self::assertInstanceOf('\DateTimeImmutable', $card->createdAt); - self::assertEquals([], $card->getComments()); - self::assertEquals([], $card->getTags()); - self::assertEquals([], $card->getMedia()); + self::assertEquals(0, $this->card->getId()); + self::assertEquals(CardStatus::ACTIVE, $this->card->getStatus()); + self::assertEquals(CardType::TEXT, $this->card->getType()); + self::assertEquals('', $this->card->name); + self::assertEquals('', $this->card->description); + self::assertEquals(0, $this->card->column); + self::assertEquals(0, $this->card->order); + self::assertEquals(0, $this->card->createdBy->getId()); + self::assertInstanceOf('\DateTimeImmutable', $this->card->createdAt); + self::assertEquals([], $this->card->getComments()); + self::assertEquals([], $this->card->getTags()); + self::assertEquals([], $this->card->getMedia()); } /** * @covers Modules\Kanban\Models\KanbanCard * @group module */ - public function testSetGet() : void + public function testStatusInputOutput() : void { - $card = new KanbanCard(); - $card->setStatus(CardStatus::ARCHIVED); - $card->setType(CardType::TASK); - $card->name = 'Name'; - $card->description = 'Description'; - $card->setColumn(1); - $card->order = 2; - $card->createdBy = new NullAccount(1); - $card->addComment(5); - $card->addMedia($m = new NullMedia(7)); + $this->card->setStatus(CardStatus::ARCHIVED); + self::assertEquals(CardStatus::ARCHIVED, $this->card->getStatus()); + } - self::assertEquals(CardStatus::ARCHIVED, $card->getStatus()); - self::assertEquals(CardType::TASK, $card->getType()); - self::assertEquals('Name', $card->name); - self::assertEquals('Description', $card->description); - self::assertEquals(1, $card->getColumn()); - self::assertEquals(2, $card->order); - self::assertEquals(1, $card->createdBy->getId()); - self::assertEquals([5], $card->getComments()); - self::assertEquals([$m], $card->getMedia()); + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testTypeInputOutput() : void + { + $this->card->setType(CardType::TASK); + self::assertEquals(CardType::TASK, $this->card->getType()); + } + + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testColumnInputOutput() : void + { + $this->card->column = 1; + self::assertEquals(1, $this->card->column); + } + + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testMediaInputOutput() : void + { + $this->card->addMedia($m = new NullMedia(7)); + self::assertCount(1, $this->card->getMedia()); + } + + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testCommentInputOutput() : void + { + $this->card->addComment(5); + self::assertEquals([5], $this->card->getComments()); + self::assertEquals(1, $this->card->getCommentCount()); + } + + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testTagInputOutput() : void + { + $tag = new Tag(); + $tag->setL11n('Tag'); + + $this->card->addTag($tag); + self::assertEquals($tag, $this->card->getTag(0)); + self::assertCount(1, $this->card->getTags()); + } + + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testCommentRemove() : void + { + $this->card->addComment(5); + self::assertCount(1, $this->card->getComments()); + self::assertTrue($this->card->removeComment(0)); + self::assertCount(0, $this->card->getComments()); + self::assertFalse($this->card->removeComment(0)); + } + + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testCreateFromTask() : void + { + self::assertInstanceOf('\Modules\Kanban\Models\KanbanCard', $this->card->createFromTask(new Task())); + } + + /** + * @covers Modules\Kanban\Models\KanbanCard + * @group module + */ + public function testSerialize() : void + { + $this->card->name = 'Title'; + $this->card->description = 'Description'; + $this->card->descriptionRaw = 'DescriptionRaw'; + $this->card->order = 3; + $this->card->column = 2; + $this->card->setStatus(CardStatus::ARCHIVED); + $this->card->setType(CardType::TASK); + + $serialized = $this->card->jsonSerialize(); + unset($serialized['createdBy']); + unset($serialized['createdAt']); + + self::assertEquals( + [ + 'id' => 0, + 'title' => 'Title', + 'description' => 'Description', + 'descriptionRaw' => 'DescriptionRaw', + 'status' => CardStatus::ARCHIVED, + 'type' => CardType::TASK, + 'column' => 2, + 'order' => 3, + 'ref' => 0, + 'comments' => [], + 'media' => [], + ], + $serialized + ); } } diff --git a/tests/Models/KanbanColumnTest.php b/tests/Models/KanbanColumnTest.php index 45cdd3b..572a19d 100755 --- a/tests/Models/KanbanColumnTest.php +++ b/tests/Models/KanbanColumnTest.php @@ -22,36 +22,75 @@ use Modules\Kanban\Models\KanbanColumn; */ final class KanbanColumnTest extends \PHPUnit\Framework\TestCase { + private KanbanColumn $column; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->column = new KanbanColumn(); + } + /** * @covers Modules\Kanban\Models\KanbanColumn * @group module */ public function testDefault() : void { - $column = new KanbanColumn(); - - self::assertEquals(0, $column->getId()); - self::assertEquals('', $column->name); - self::assertEquals(0, $column->order); - self::assertEquals(0, $column->getBoard()); - self::assertEquals([], $column->getCards()); + self::assertEquals(0, $this->column->getId()); + self::assertEquals('', $this->column->name); + self::assertEquals(0, $this->column->order); + self::assertEquals(0, $this->column->board); + self::assertEquals([], $this->column->getCards()); } /** * @covers Modules\Kanban\Models\KanbanColumn * @group module */ - public function testSetGet() : void + public function testCardInputOutput() : void { - $column = new KanbanColumn(); + $this->column->addCard(new KanbanCard()); + self::assertCount(1, $this->column->getCards()); + } - $column->name = 'Name'; - $column->order = 2; - $column->setBoard(3); - $column->addCard(new KanbanCard()); + /** + * @covers Modules\Kanban\Models\KanbanColumn + * @group module + */ + public function testCardRemove() : void + { + $this->column->addCard(new KanbanCard()); + self::assertCount(1, $this->column->getCards()); + self::assertTrue($this->column->removeCard(0)); + self::assertCount(0, $this->column->getCards()); + self::assertFalse($this->column->removeCard(0)); + } - self::assertEquals('Name', $column->name); - self::assertEquals(2, $column->order); - self::assertEquals(3, $column->getBoard()); + /** + * @covers Modules\Kanban\Models\KanbanColumn + * @group module + */ + public function testSerialize() : void + { + $this->column->name = 'Name'; + $this->column->board = 2; + $this->column->order = 3; + + $serialized = $this->column->jsonSerialize(); + unset($serialized['createdBy']); + unset($serialized['createdAt']); + + self::assertEquals( + [ + 'id' => 0, + 'name' => 'Name', + 'order' => 3, + 'board' => 2, + 'cards' => [], + ], + $serialized + ); } } diff --git a/tests/Models/NullKanbanCardCommentTest.php b/tests/Models/NullKanbanCardCommentTest.php new file mode 100644 index 0000000..d8f26cd --- /dev/null +++ b/tests/Models/NullKanbanCardCommentTest.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/Models/NullKanbanCardTest.php b/tests/Models/NullKanbanCardTest.php new file mode 100644 index 0000000..b26437d --- /dev/null +++ b/tests/Models/NullKanbanCardTest.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/Models/NullKanbanColumnTest.php b/tests/Models/NullKanbanColumnTest.php new file mode 100644 index 0000000..dacc603 --- /dev/null +++ b/tests/Models/NullKanbanColumnTest.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/phpunit_default.xml b/tests/phpunit_default.xml index 722365c..9208c99 100755 --- a/tests/phpunit_default.xml +++ b/tests/phpunit_default.xml @@ -1,5 +1,5 @@ - + *vendor*