CommentControllerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace internship\controller;
  3. use PHPUnit\Framework\TestCase;
  4. use internship\test\CommentTestEnv;
  5. use n2n\test\TestEnv;
  6. use internship\bo\Article;
  7. use internship\test\ArticleTestEnv;
  8. use n2n\web\http\StatusException;
  9. use util\GeneralTestEnv;
  10. use internship\bo\Comment;
  11. use n2n\web\http\PageNotFoundException;
  12. class CommentControllerTest extends TestCase {
  13. private int $article1Id;
  14. private int $comment1Id;
  15. private int $comment2Id;
  16. private int $comment3Id;
  17. function setUp(): void {
  18. GeneralTestEnv::tearDown();
  19. $tx = TestEnv::createTransaction();
  20. $article1 = ArticleTestEnv::setUpArticle('Title 1', 'Lorem ipsum 1', 'sport');
  21. $comment1 = CommentTestEnv::setUpComment($article1, 'Markus Rutz', 'Hallo das ist ein Test (0) :-)');
  22. $comment2 = CommentTestEnv::setUpComment($article1, 'Markus Rutz', 'Hallo das ist ein Test (1) :-)');
  23. $comment3 = CommentTestEnv::setUpComment($article1, 'Markus Rutz', 'Hallo das ist ein Test (2) :-)');
  24. $tx->commit();
  25. $this->article1Id = $article1->id;
  26. $this->comment1Id = $comment1->id;
  27. $this->comment2Id = $comment2->id;
  28. $this->comment3Id = $comment3->id;
  29. }
  30. /**
  31. * @throws StatusException
  32. */
  33. function testGetDoComments() {
  34. $response = TestEnv::http()->newRequest()
  35. ->get(['api', 'comment', 'comments'])
  36. ->exec();
  37. $commentStructs = $response->parseJson();
  38. $this->assertCount(3, $commentStructs);
  39. $this->assertEquals('Hallo das ist ein Test (2) :-)', $commentStructs[0]['text']);
  40. $this->assertEquals('Hallo das ist ein Test (1) :-)', $commentStructs[1]['text']);
  41. $this->assertEquals('Hallo das ist ein Test (0) :-)', $commentStructs[2]['text']);
  42. }
  43. /**
  44. * @throws StatusException
  45. */
  46. function testGetComment() {
  47. $response = TestEnv::http()->newRequest()
  48. ->get(['api', 'comment', 'comment', $this->article1Id])
  49. ->exec();
  50. $commentStructs = $response->parseJson();
  51. $this->assertEquals('Hallo das ist ein Test (0) :-)', $commentStructs['text']);
  52. }
  53. /**
  54. * @throws StatusException
  55. */
  56. function testGetCommentNotFound() {
  57. $this->expectException(PageNotFoundException::class);
  58. $response = TestEnv::http()->newRequest()
  59. ->get(['api', 'comment', 'comment', 1000])
  60. ->exec();
  61. }
  62. /**
  63. * @throws StatusException
  64. */
  65. function testDeleteComment() {
  66. $tx = TestEnv::createTransaction();
  67. $comment1 = TestEnv::tem()->find(Comment::class, 1);
  68. TestEnv::tem()->remove($comment1);
  69. $tx->commit();
  70. $tx = TestEnv::createTransaction(true);
  71. $this->assertSame(2, TestEnv::temUtil()->count(Comment::class));
  72. $tx->commit();
  73. }
  74. /**
  75. * @throws StatusException
  76. */
  77. function testDeleteArticleAndOrphanRemovalOfComments() {
  78. $tx = TestEnv::createTransaction(true);
  79. $this->assertSame(1, TestEnv::temUtil()->count(Article::class));
  80. $this->assertSame(3, TestEnv::temUtil()->count(Comment::class));
  81. $tx->commit();
  82. $tx = TestEnv::createTransaction();
  83. $article = TestEnv::tem()->find(Article::class, 1);
  84. TestEnv::tem()->remove($article);
  85. $tx->commit();
  86. $tx = TestEnv::createTransaction(true);
  87. $this->assertSame(0, TestEnv::temUtil()->count(Article::class));
  88. $this->assertSame(0, TestEnv::temUtil()->count(Comment::class));
  89. $tx->commit();
  90. }
  91. }