ArticleControllerTest.php 6.2 KB

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