ArticleControllerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. */
  80. function testPostDoArticle() {
  81. $tx = TestEnv::createTransaction(true);
  82. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  83. $tx->commit();
  84. $response = TestEnv::http()->newRequest()
  85. ->post(['api', 'article'])
  86. ->bodyJson([
  87. 'title' => 'Title POST',
  88. 'text' => 'Text POST',
  89. 'categoryName' => 'sport'
  90. ])
  91. ->exec();
  92. $articleStruct = $response->parseJson();
  93. $this->assertEquals('Title POST', $articleStruct['title']);
  94. $id = $articleStruct['id'];
  95. $tx = TestEnv::createTransaction(true);
  96. $this->assertSame(4, TestEnv::temUtil()->count(Article::class));
  97. $article = TestEnv::tem()->find(Article::class, $id);
  98. $this->assertSame('Title POST', $article->title);
  99. $tx->commit();
  100. }
  101. /**
  102. * @throws StatusException
  103. */
  104. function testPutDoArticle() {
  105. $tx = TestEnv::createTransaction(true);
  106. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  107. $article = TestEnv::tem()->find(Article::class, $this->article1Id);
  108. $this->assertSame('Title 3', $article->title);
  109. $this->assertSame('Lorem ipsum 3', $article->text);
  110. $tx->commit();
  111. $response = TestEnv::http()->newRequest()
  112. ->put(['api', 'article', $this->article1Id])
  113. ->bodyJson([
  114. 'title' => 'Title PUT',
  115. 'text' => 'Text PUT',
  116. 'categoryName' => 'sport',
  117. ])
  118. ->exec();
  119. $articleStruct = $response->parseJson();
  120. $this->assertEquals('Title PUT', $articleStruct['title']);
  121. $tx = TestEnv::createTransaction(true);
  122. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  123. $article = TestEnv::tem()->find(Article::class, $this->article1Id);
  124. $this->assertSame('Title PUT', $article->title);
  125. $this->assertSame('Text PUT', $article->text);
  126. $tx->commit();
  127. }
  128. /**
  129. * @throws StatusException
  130. * check {@link ArticleApiController::isValidArticleCategory} violation
  131. */
  132. function testPutDoArticleExpectExceptionBecauseOfInvalidCategoryName() {
  133. $tx = TestEnv::createTransaction(true);
  134. $categoryName = TestEnv::tem()->find(Article::class, $this->article1Id)->categoryName;
  135. $tx->commit();
  136. // only certain CategoryNames are allowed; in setup category restrictions are not checked.
  137. $this->expectException(BadRequestException::class);
  138. TestEnv::http()->newRequest()
  139. ->put(['api', 'article', $this->article1Id])
  140. ->bodyJson([
  141. 'title' => 'Title PUT',
  142. 'text' => 'Text PUT',
  143. 'categoryName' => $categoryName,
  144. ])
  145. ->exec();
  146. }
  147. /**
  148. * @throws StatusException
  149. */
  150. function testDeleteDoArticle() {
  151. $tx = TestEnv::createTransaction(true);
  152. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  153. $this->assertSame(1, TestEnv::temUtil()->count(Category::class));
  154. $tx->commit();
  155. TestEnv::http()->newRequest()
  156. ->delete(['api', 'article', $this->article1Id])
  157. ->exec();
  158. $tx = TestEnv::createTransaction(true);
  159. $this->assertSame(2, TestEnv::temUtil()->count(Article::class));
  160. $this->assertSame(1, TestEnv::temUtil()->count(Category::class));
  161. $tx->commit();
  162. }
  163. function testDeleteCat() {
  164. $tx = TestEnv::createTransaction(true);
  165. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  166. $this->assertSame(1, TestEnv::temUtil()->count(Category::class));
  167. $tx->commit();
  168. $tx = TestEnv::createTransaction();
  169. $cat = TestEnv::tem()->find(Category::class, 1);
  170. TestEnv::tem()->remove($cat);
  171. $tx->commit();
  172. $tx = TestEnv::createTransaction(true);
  173. $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
  174. $this->assertSame(0, TestEnv::temUtil()->count(Category::class));
  175. $tx->commit();
  176. }
  177. }