ArticleControllerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace internship\controller;
  3. use PHPUnit\Framework\TestCase;
  4. use n2n\test\TestEnv;
  5. use internship\test\ArticleTestEnv;
  6. use util\GeneralTestEnv;
  7. use n2n\web\http\StatusException;
  8. use n2n\web\http\PageNotFoundException;
  9. use internship\bo\Article;
  10. use n2n\web\http\BadRequestException;
  11. class ArticleControllerTest extends TestCase {
  12. private int $article1Id;
  13. private int $article2Id;
  14. private int $article3Id;
  15. function setUp(): void {
  16. GeneralTestEnv::tearDown();
  17. $tx = TestEnv::createTransaction();
  18. $article1 = ArticleTestEnv::setUpArticle('Title 3', 'Lorem ipsum 3', 'teaser');
  19. $article2 = ArticleTestEnv::setUpArticle('Title 2', categoryName: 'news');
  20. $article3 = ArticleTestEnv::setUpArticle('Title 1', categoryName: 'news');
  21. //$article4 = ArticleTestEnv::setUpArticle('Title 4');
  22. //var_dump($article4);
  23. $tx->commit();
  24. $this->article1Id = $article1->getId();
  25. $this->article2Id = $article2->getId();
  26. $this->article3Id = $article3->getId();
  27. }
  28. /**
  29. * @throws StatusException
  30. */
  31. function testGetDoArticles() {
  32. $response = TestEnv::http()->newRequest()
  33. ->get(['api', 'articles'])
  34. ->exec();
  35. $articleStructs = $response->parseJson();
  36. $this->assertCount(3, $articleStructs);
  37. $this->assertEquals('Title 1', $articleStructs[0]['title']);
  38. $this->assertEquals('Title 2', $articleStructs[1]['title']);
  39. $this->assertEquals('Title 3', $articleStructs[2]['title']);
  40. }
  41. /**
  42. * @throws StatusException
  43. */
  44. function testGetDoArticlesByCategoryName() {
  45. $response = TestEnv::http()->newRequest()
  46. ->get(['api', 'articles', 'news'])
  47. ->exec();
  48. $articleStructs = $response->parseJson();
  49. $this->assertCount(2, $articleStructs);
  50. }
  51. /**
  52. * @throws StatusException
  53. */
  54. function testGetArticle() {
  55. $response = TestEnv::http()->newRequest()
  56. ->get(['api', 'article', $this->article3Id])
  57. ->exec();
  58. $articleStructs = $response->parseJson();
  59. $this->assertEquals('Title 1', $articleStructs['title']);
  60. }
  61. /**
  62. * @throws StatusException
  63. */
  64. function testGetArticleNotFound() {
  65. $this->expectException(PageNotFoundException::class);
  66. $response = TestEnv::http()->newRequest()
  67. ->get(['api', 'article', 1000])
  68. ->exec();
  69. }
  70. /**
  71. * @throws StatusException
  72. */
  73. function testPostDoArticle() {
  74. $tx = TestEnv::createTransaction(true);
  75. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  76. $tx->commit();
  77. $response = TestEnv::http()->newRequest()
  78. ->post(['api', 'article'])
  79. ->bodyJson([
  80. 'title' => 'Title POST',
  81. 'text' => 'Text POST',
  82. 'categoryName' => 'sport',
  83. ])
  84. ->exec();
  85. $articleStruct = $response->parseJson();
  86. $this->assertEquals('Title POST', $articleStruct['title']);
  87. $id = $articleStruct['id'];
  88. $tx = TestEnv::createTransaction(true);
  89. $this->assertSame(4, TestEnv::temUtil()->count(Article::class));
  90. $article = TestEnv::tem()->find(Article::class, $id);
  91. $this->assertSame('Title POST', $article->title);
  92. $tx->commit();
  93. }
  94. /**
  95. * @throws StatusException
  96. */
  97. function testPutDoArticle() {
  98. $tx = TestEnv::createTransaction(true);
  99. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  100. $article = TestEnv::tem()->find(Article::class, $this->article1Id);
  101. $this->assertSame('Title 3', $article->title);
  102. $this->assertSame('Lorem ipsum 3', $article->text);
  103. $tx->commit();
  104. $response = TestEnv::http()->newRequest()
  105. ->put(['api', 'article', $this->article1Id])
  106. ->bodyJson([
  107. 'title' => 'Title PUT',
  108. 'text' => 'Text PUT',
  109. 'categoryName' => 'sport',
  110. ])
  111. ->exec();
  112. $articleStruct = $response->parseJson();
  113. $this->assertEquals('Title PUT', $articleStruct['title']);
  114. $tx = TestEnv::createTransaction(true);
  115. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  116. $article = TestEnv::tem()->find(Article::class, $this->article1Id);
  117. $this->assertSame('Title PUT', $article->title);
  118. $this->assertSame('Text PUT', $article->text);
  119. $tx->commit();
  120. }
  121. /**
  122. * @throws StatusException
  123. * check {@link ArticleApiController::isValidArticleCategory} violation
  124. */
  125. function testPutDoArticleExpectExceptionBecauseOfInvalidCategoryName() {
  126. $tx = TestEnv::createTransaction(true);
  127. $categoryName = TestEnv::tem()->find(Article::class, $this->article1Id)->categoryName;
  128. $tx->commit();
  129. // only certain CategoryNames are allowed; in setup category restrictions are not checked.
  130. $this->expectException(BadRequestException::class);
  131. TestEnv::http()->newRequest()
  132. ->put(['api', 'article', $this->article1Id])
  133. ->bodyJson([
  134. 'title' => 'Title PUT',
  135. 'text' => 'Text PUT',
  136. 'categoryName' => $categoryName,
  137. ])
  138. ->exec();
  139. }
  140. /**
  141. * @throws StatusException
  142. */
  143. function testDeleteDoArticle() {
  144. $tx = TestEnv::createTransaction(true);
  145. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  146. $tx->commit();
  147. TestEnv::http()->newRequest()
  148. ->delete(['api', 'article', $this->article1Id])
  149. ->exec();
  150. $tx = TestEnv::createTransaction(true);
  151. $this->assertSame(2, TestEnv::temUtil()->count(Article::class));
  152. $tx->commit();
  153. }
  154. }