| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace internship\controller;
- use PHPUnit\Framework\TestCase;
- use internship\test\CommentTestEnv;
- use n2n\test\TestEnv;
- use internship\bo\Article;
- use internship\test\ArticleTestEnv;
- use n2n\web\http\StatusException;
- use util\GeneralTestEnv;
- use internship\bo\Comment;
- use n2n\web\http\PageNotFoundException;
- class CommentControllerTest extends TestCase {
- private int $article1Id;
- private int $comment1Id;
- private int $comment2Id;
- private int $comment3Id;
- function setUp(): void {
- GeneralTestEnv::tearDown();
- $tx = TestEnv::createTransaction();
- $article1 = ArticleTestEnv::setUpArticle('Title 1', 'Lorem ipsum 1', 'sport');
- $comment1 = CommentTestEnv::setUpComment($article1, 'Markus Rutz', 'Hallo das ist ein Test (0) :-)');
- $comment2 = CommentTestEnv::setUpComment($article1, 'Markus Rutz', 'Hallo das ist ein Test (1) :-)');
- $comment3 = CommentTestEnv::setUpComment($article1, 'Markus Rutz', 'Hallo das ist ein Test (2) :-)');
- $tx->commit();
- $this->article1Id = $article1->id;
- $this->comment1Id = $comment1->id;
- $this->comment2Id = $comment2->id;
- $this->comment3Id = $comment3->id;
- }
- /**
- * @throws StatusException
- */
- function testGetDoComments() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'comment', 'comments'])
- ->exec();
- $commentStructs = $response->parseJson();
- $this->assertCount(3, $commentStructs);
- $this->assertEquals('Hallo das ist ein Test (2) :-)', $commentStructs[0]['text']);
- $this->assertEquals('Hallo das ist ein Test (1) :-)', $commentStructs[1]['text']);
- $this->assertEquals('Hallo das ist ein Test (0) :-)', $commentStructs[2]['text']);
- }
- /**
- * @throws StatusException
- */
- function testGetComment() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'comment', 'comment', $this->article1Id])
- ->exec();
- $commentStructs = $response->parseJson();
- $this->assertEquals('Hallo das ist ein Test (0) :-)', $commentStructs['text']);
- }
- /**
- * @throws StatusException
- */
- function testGetCommentNotFound() {
- $this->expectException(PageNotFoundException::class);
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'comment', 'comment', 1000])
- ->exec();
- }
- /**
- * @throws StatusException
- */
- function testDeleteComment() {
- $tx = TestEnv::createTransaction();
- $comment1 = TestEnv::tem()->find(Comment::class, 1);
- TestEnv::tem()->remove($comment1);
- $tx->commit();
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(2, TestEnv::temUtil()->count(Comment::class));
- $tx->commit();
- }
- /**
- * @throws StatusException
- */
- function testDeleteArticleAndOrphanRemovalOfComments() {
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(1, TestEnv::temUtil()->count(Article::class));
- $this->assertSame(3, TestEnv::temUtil()->count(Comment::class));
- $tx->commit();
- $tx = TestEnv::createTransaction();
- $article = TestEnv::tem()->find(Article::class, 1);
- TestEnv::tem()->remove($article);
- $tx->commit();
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(0, TestEnv::temUtil()->count(Article::class));
- $this->assertSame(0, TestEnv::temUtil()->count(Comment::class));
- $tx->commit();
- }
- }
|