diff --git a/Models/Comment.php b/Models/Comment.php index 14efc9c..f674790 100755 --- a/Models/Comment.php +++ b/Models/Comment.php @@ -58,7 +58,7 @@ class Comment * @var int|CommentList * @since 1.0.0 */ - private $list = 0; + public $list = 0; /** * Title @@ -98,7 +98,7 @@ class Comment * @var null|int|self * @since 1.0.0 */ - private $ref = null; + public $ref = null; /** * Media files @@ -131,32 +131,6 @@ class Comment return $this->id; } - /** - * Reference id - * - * @param mixed $ref Reference - * - * @return void - * - * @since 1.0.0 - */ - public function setRef($ref) : void - { - $this->ref = $ref; - } - - /** - * Get the reference - * - * @return mixed - * - * @since 1.0.0 - */ - public function getRef() - { - return $this->ref; - } - /** * Set the status * @@ -183,38 +157,22 @@ class Comment return $this->status; } - /** - * Set the list this comment belongs to - * - * @param int|CommentList $list List - * - * @return void - * - * @since 1.0.0 - */ - public function setList($list) : void - { - $this->list = $list; - } - - /** - * Get the list this comment belongs to - * - * @return int|CommentList - * - * @since 1.0.0 - */ - public function getList() - { - return $this->list; - } /** * {@inheritdoc} */ public function jsonSerialize() : array { - return []; + return [ + 'id' => $this->id, + 'title' => $this->title, + 'content' => $this->content, + 'list' => $this->list, + 'ref' => $this->ref, + 'status' => $this->status, + 'createdAt' => $this->createdAt, + 'createdBy' => $this->createdBy, + ]; } /** diff --git a/Models/CommentVote.php b/Models/CommentVote.php index cfc0f1e..14fd618 100755 --- a/Models/CommentVote.php +++ b/Models/CommentVote.php @@ -38,10 +38,10 @@ class CommentVote /** * Account. * - * @var Account + * @var int * @since 1.0.0 */ - public Account $createdBy; + public int $createdBy = 0; /** * Created at @@ -74,7 +74,18 @@ class CommentVote */ public function __construct() { - $this->createdBy = new NullAccount(); $this->createdAt = new \DateTimeImmutable(); } + + /** + * Get id. + * + * @return int Model id + * + * @since 1.0.0 + */ + public function getId() : int + { + return $this->id; + } } diff --git a/tests/Models/CommentListTest.php b/tests/Models/CommentListTest.php index c77796f..94b5fe6 100755 --- a/tests/Models/CommentListTest.php +++ b/tests/Models/CommentListTest.php @@ -22,15 +22,24 @@ use Modules\Comments\Models\CommentList; */ class CommentListTest extends \PHPUnit\Framework\TestCase { + private CommentList $list; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->list = new CommentList(); + } + /** * @covers Modules\Comments\Models\CommentList * @group module */ public function testDefault() : void { - $list = new CommentList(); - self::assertEquals(0, $list->getId()); - self::assertEquals([], $list->getComments()); + self::assertEquals(0, $this->list->getId()); + self::assertEquals([], $this->list->getComments()); } /** @@ -39,15 +48,14 @@ class CommentListTest extends \PHPUnit\Framework\TestCase */ public function testGetSet() : void { - $list = new CommentList(); $comment = new Comment(); $comment->title = 'Test Title'; $comment->contentRaw = 'TestRaw'; $comment->content = 'Test Content'; - $list->addComment($comment); - self::assertEquals('Test Title', $list->getComments()[0]->title); - self::assertEquals('TestRaw', $list->getComments()[0]->contentRaw); - self::assertEquals('Test Content', $list->getComments()[0]->content); + $this->list->addComment($comment); + self::assertEquals('Test Title', $this->list->getComments()[0]->title); + self::assertEquals('TestRaw', $this->list->getComments()[0]->contentRaw); + self::assertEquals('Test Content', $this->list->getComments()[0]->content); } } diff --git a/tests/Models/CommentMapperTest.php b/tests/Models/CommentMapperTest.php index 7858454..25bfd47 100755 --- a/tests/Models/CommentMapperTest.php +++ b/tests/Models/CommentMapperTest.php @@ -34,8 +34,8 @@ class CommentMapperTest extends \PHPUnit\Framework\TestCase $comment->createdBy = new NullAccount(1); $comment->title = 'Test Title'; $comment->content = 'Test Content'; - $comment->setRef(null); - $comment->setList(new CommentList()); + $comment->ref = null; + $comment->list = new CommentList(); $id = CommentMapper::create($comment); self::assertGreaterThan(0, $comment->getId()); @@ -46,7 +46,7 @@ class CommentMapperTest extends \PHPUnit\Framework\TestCase self::assertEquals($comment->createdBy->getId(), $commentR->createdBy->getId()); self::assertEquals($comment->title, $commentR->title); self::assertEquals($comment->content, $commentR->content); - self::assertEquals($comment->getRef(), $commentR->getRef()); - self::assertEquals($comment->getList()->getId(), $commentR->getList()->getId()); + self::assertEquals($comment->ref, $commentR->ref); + self::assertEquals($comment->list->getId(), $commentR->list->getId()); } } diff --git a/tests/Models/CommentTest.php b/tests/Models/CommentTest.php index 94f43fb..0b1e8d5 100755 --- a/tests/Models/CommentTest.php +++ b/tests/Models/CommentTest.php @@ -15,7 +15,9 @@ declare(strict_types=1); namespace Modules\Comments\tests\Models; use Modules\Admin\Models\NullAccount; +use Modules\Media\Models\Media; use Modules\Comments\Models\Comment; +use Modules\Comments\Models\CommentStatus; use Modules\Comments\Models\NullComment; /** @@ -23,45 +25,133 @@ use Modules\Comments\Models\NullComment; */ class CommentTest extends \PHPUnit\Framework\TestCase { + private Comment $comment; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->comment = new Comment(); + } + /** * @covers Modules\Comments\Models\Comment * @group module */ public function testDefault() : void { - $comment = new Comment(); - self::assertEquals(0, $comment->getId()); + self::assertEquals(0, $this->comment->getId()); $date = new \DateTime('now'); - self::assertEquals($date->format('Y-m-d'), $comment->createdAt->format('Y-m-d')); - self::assertEquals(0, $comment->createdBy->getId()); - self::assertEquals(0, $comment->getList()); - self::assertEquals(0, $comment->getRef()); - self::assertEquals('', $comment->title); - self::assertEquals('', $comment->content); + self::assertEquals($date->format('Y-m-d'), $this->comment->createdAt->format('Y-m-d')); + self::assertEquals(0, $this->comment->createdBy->getId()); + self::assertEquals(0, $this->comment->list); + self::assertEquals(0, $this->comment->ref); + self::assertEquals('', $this->comment->title); + self::assertEquals('', $this->comment->content); + self::assertEquals([], $this->comment->getMedia()); } /** * @covers Modules\Comments\Models\Comment * @group module */ - public function testGetSet() : void + public function testCreatedByInputOutput() : void { - $comment = new Comment(); + $this->comment->createdBy = new NullAccount(1); + self::assertEquals(1, $this->comment->createdBy->getId()); + } - $comment->createdBy = new NullAccount(1); - self::assertEquals(1, $comment->createdBy->getId()); + /** + * @covers Modules\Comments\Models\Comment + * @group module + */ + public function testListInputOutput() : void + { + $this->comment->list = 3; + self::assertEquals(3, $this->comment->list); + } - $comment->setList(2); - self::assertEquals(2, $comment->getList()); + /** + * @covers Modules\Comments\Models\Comment + * @group module + */ + public function testRefInputOutput() : void + { + $this->comment->ref = 2; + self::assertEquals(2, $this->comment->ref); - $comment->setRef(new NullComment(3)); - self::assertEquals(3, $comment->getRef()->getId()); + $this->comment->ref = new NullComment(3); + self::assertEquals(3, $this->comment->ref->getId()); + } - $comment->title = 'Test Title'; - self::assertEquals('Test Title', $comment->title); + /** + * @covers Modules\Comments\Models\Comment + * @group module + */ + public function testTitleInputOutput() : void + { + $this->comment->title = 'Test Title'; + self::assertEquals('Test Title', $this->comment->title); + } - $comment->content = 'Test Content'; - self::assertEquals('Test Content', $comment->content); + /** + * @covers Modules\Comments\Models\Comment + * @group module + */ + public function testContentInputOutput() : void + { + $this->comment->content = 'Test Content'; + self::assertEquals('Test Content', $this->comment->content); + } + + /** + * @covers Modules\Comments\Models\Comment + * @group module + */ + public function testStatusInputOutput() : void + { + $this->comment->setStatus(CommentStatus::INVISIBLE); + self::assertEquals(CommentStatus::INVISIBLE, $this->comment->getStatus()); + } + + /** + * @covers Modules\Comments\Models\Comment + * @group module + */ + public function testMediaInputOutput() : void + { + $this->comment->addMedia(new Media()); + self::assertCount(1, $this->comment->getMedia()); + } + + /** + * @covers Modules\CMS\Models\PageL11n + * @group module + */ + public function testSerialize() : void + { + $this->comment->title = 'Title'; + $this->comment->content = 'Content'; + $this->comment->list = 2; + $this->comment->ref = 1; + $this->comment->createdBy = new NullAccount(2); + + $serialized = $this->comment->jsonSerialize(); + unset($serialized['createdAt']); + unset($serialized['createdBy']); + + self::assertEquals( + [ + 'id' => 0, + 'title' => 'Title', + 'content' => 'Content', + 'list' => 2, + 'ref' => 1, + 'status' => CommentStatus::VISIBLE, + ], + $serialized + ); } } diff --git a/tests/Models/CommentVoteMapperTest.php b/tests/Models/CommentVoteMapperTest.php new file mode 100644 index 0000000..73f5b98 --- /dev/null +++ b/tests/Models/CommentVoteMapperTest.php @@ -0,0 +1,57 @@ +title = 'TestComment'; + $comment->createdBy = new NullAccount(1); + $comment->list = $lId; + + $cId = CommentMapper::create($comment); + + $vote = new CommentVote(); + $vote->comment = $cId; + $vote->score = 1; + $vote->createdBy = 1; + + CommentVoteMapper::create($vote); + + $voteR = CommentvoteMapper::findVote($cId, 1); + self::assertEquals($vote->comment, $voteR->comment); + self::assertEquals($vote->createdBy, $voteR->createdBy); + } +} diff --git a/tests/Models/CommentVoteTest.php b/tests/Models/CommentVoteTest.php new file mode 100644 index 0000000..054ae1b --- /dev/null +++ b/tests/Models/CommentVoteTest.php @@ -0,0 +1,66 @@ +vote = new CommentVote(); + } + + /** + * @covers Modules\Comments\Models\CommentVote + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->vote->getId()); + self::assertEquals(0, $this->vote->score); + self::assertEquals(0, $this->vote->comment); + self::assertEquals(0, $this->vote->createdBy); + self::assertInstanceOf('\DateTimeImmutable', $this->vote->createdAt); + } + + /** + * @covers Modules\Comments\Models\CommentVote + * @group module + */ + public function testScoreInputOutput() : void + { + $this->vote->score = 1; + self::assertEquals(1, $this->vote->score); + } + + /** + * @covers Modules\Comments\Models\CommentVote + * @group module + */ + public function testCommentInputOutput() : void + { + $this->vote->comment = 1; + self::assertEquals(1, $this->vote->comment); + } +} diff --git a/tests/Models/NullComment.php b/tests/Models/NullComment.php new file mode 100644 index 0000000..c5ad7d9 --- /dev/null +++ b/tests/Models/NullComment.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/Models/NullCommentList.php b/tests/Models/NullCommentList.php new file mode 100644 index 0000000..96f2f8c --- /dev/null +++ b/tests/Models/NullCommentList.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/Models/NullCommentVote.php b/tests/Models/NullCommentVote.php new file mode 100644 index 0000000..d242a05 --- /dev/null +++ b/tests/Models/NullCommentVote.php @@ -0,0 +1,42 @@ +getId()); + } +}