ArticleControllerTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 1', 'Loren ipsum 1', 'news');
  19. $article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'teaser');
  20. $article3 = ArticleTestEnv::setUpArticle('Title 3', 'Loren ipsum 3', 'news');
  21. $tx->commit();
  22. $this->article1Id = $article1->getId();
  23. $this->article2Id = $article2->getId();
  24. $this->article3Id = $article3->getId();
  25. }
  26. /**
  27. * @throws StatusException
  28. */
  29. function testGetDoArticles() {
  30. $response = TestEnv::http()->newRequest()
  31. ->get(['api', 'articles'])
  32. ->exec();
  33. $articleStruct = $response->parseJson();
  34. $this->assertCount(3, $articleStruct);
  35. $this->assertEquals('Title 3', $articleStruct[0]['title']);
  36. $this->assertEquals('Title 2', $articleStruct[1]['title']);
  37. $this->assertEquals('Title 1', $articleStruct[2]['title']);
  38. }
  39. function testGetDoArticlesWithCategory() {
  40. $response = TestEnv::http()->newRequest()
  41. ->get(['api', 'articles', 'teaser'])
  42. ->exec();
  43. $articleStruct = $response->parseJson();
  44. $this->assertCount(1, $articleStruct);
  45. $this->assertEquals('Title 2', $articleStruct[0]['title']);
  46. }
  47. function testGetDoArticlesWithCategory2() {
  48. $response = TestEnv::http()->newRequest()
  49. ->get(['api', 'articles', 'news'])
  50. ->exec();
  51. $articleStruct = $response->parseJson();
  52. $this->assertCount(2, $articleStruct);
  53. $this->assertEquals('Title 3', $articleStruct[0]['title']);
  54. $this->assertEquals('Title 1', $articleStruct[1]['title']);
  55. }
  56. function testGetDoArticlesExpectExceptionBecauseCategoryNotFound() {
  57. $this->expectException(PageNotFoundException::class);
  58. $this->expectExceptionMessage('Category abc not found');
  59. TestEnv::http()->newRequest()
  60. ->get(['api', 'articles', 'abc'])
  61. ->exec();
  62. }
  63. function testGetDoArticle() {
  64. $response = TestEnv::http()->newRequest()
  65. ->get(['api', 'article', $this->article1Id])
  66. ->exec();
  67. $articleStruct = $response->parseJson();
  68. $this->assertCount(4, $articleStruct);
  69. $this->assertEquals('Title 1', $articleStruct['title']);
  70. $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
  71. $this->assertEquals('news', $articleStruct['categoryName']);
  72. }
  73. function testGetDoArticleExpectExceptionBecauseArticleNotFound() {
  74. $this->expectException(PageNotFoundException::class);
  75. $this->expectExceptionMessage('Article not found with ID:25436857');
  76. TestEnv::http()->newRequest()
  77. ->get(['api', 'article', 25436857])
  78. ->exec();
  79. }
  80. function testPostDoArticle() {
  81. $response = TestEnv::http()->newRequest()
  82. ->post(['api', 'article'])
  83. ->bodyJson([
  84. 'title' => 'title1',
  85. 'categoryName' => 'sport',
  86. 'text' => 'text1'
  87. ])
  88. ->exec();
  89. $articleStruct = $response->parseJson();
  90. $this->assertEquals(4,TestEnv::emUtil()->count(Article::class));
  91. $this->assertCount(4, $articleStruct);
  92. $this->assertEquals('title1', $articleStruct['title']);
  93. $this->assertEquals('text1', $articleStruct['text']);
  94. $this->assertEquals('sport', $articleStruct['categoryName']);
  95. }
  96. function testPostDoArticleExpectExceptionBecauseCategoryNotFound() {
  97. $this->expectException(BadRequestException::class);
  98. $this->expectExceptionMessage('The Category must be international, national or sport');
  99. TestEnv::http()->newRequest()
  100. ->post(['api', 'article'])
  101. ->bodyJson([
  102. 'title' => 'title1',
  103. 'categoryName' => 'abc',
  104. 'text' => 'text1'
  105. ])
  106. ->exec();
  107. }
  108. function testPutDoArticle() {
  109. $response = TestEnv::http()->newRequest()
  110. ->put(['api', 'article',$this->article1Id])
  111. ->bodyJson([
  112. 'title' => 'title1',
  113. 'categoryName' => 'sport',
  114. 'text' => 'text1'
  115. ])
  116. ->exec();
  117. $articleStruct = $response->parseJson();
  118. $this->assertCount(4, $articleStruct);
  119. $this->assertEquals('title1', $articleStruct['title']);
  120. $this->assertEquals('text1', $articleStruct['text']);
  121. $this->assertEquals('sport', $articleStruct['categoryName']);
  122. }
  123. function testPutDoArticle1() {
  124. $response = TestEnv::http()->newRequest()
  125. ->put(['api', 'article',$this->article1Id])
  126. ->bodyJson([
  127. 'title' => 'title1',
  128. ])
  129. ->exec();
  130. $articleStruct = $response->parseJson();
  131. $this->assertCount(4, $articleStruct);
  132. $this->assertEquals('title1', $articleStruct['title']);
  133. $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
  134. $this->assertEquals('news', $articleStruct['categoryName']);
  135. }
  136. function testPutDoArticle2() {
  137. $response = TestEnv::http()->newRequest()
  138. ->put(['api', 'article',$this->article1Id])
  139. ->bodyJson([
  140. 'categoryName' => 'sport',
  141. ])
  142. ->exec();
  143. $articleStruct = $response->parseJson();
  144. $this->assertCount(4, $articleStruct);
  145. $this->assertEquals('Title 1', $articleStruct['title']);
  146. $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
  147. $this->assertEquals('sport', $articleStruct['categoryName']);
  148. }
  149. function testPutDoArticle3() {
  150. $response = TestEnv::http()->newRequest()
  151. ->put(['api', 'article',$this->article1Id])
  152. ->bodyJson([
  153. 'text' => 'text1',
  154. ])
  155. ->exec();
  156. $articleStruct = $response->parseJson();
  157. $this->assertCount(4, $articleStruct);
  158. $this->assertEquals('Title 1', $articleStruct['title']);
  159. $this->assertEquals('text1', $articleStruct['text']);
  160. $this->assertEquals('news', $articleStruct['categoryName']);
  161. }
  162. function testPutDoArticleExpectExceptionBecauseCategoryNotFound() {
  163. $this->expectException(BadRequestException::class);
  164. $this->expectExceptionMessage('The Category must be international, national or sport');
  165. TestEnv::http()->newRequest()
  166. ->put(['api', 'article',$this->article1Id])
  167. ->bodyJson([
  168. 'title' => 'title1',
  169. 'categoryName' => 'abc',
  170. 'text' => 'text1'
  171. ])
  172. ->exec();
  173. }
  174. function testPutDoArticleExpectExceptionBecauseArticleNotFound() {
  175. $this->expectException(PageNotFoundException::class);
  176. $this->expectExceptionMessage('Article not found');
  177. TestEnv::http()->newRequest()
  178. ->put(['api', 'article', 25436857])
  179. ->bodyJson([
  180. 'title' => 'title1',
  181. 'categoryName' => 'abc',
  182. 'text' => 'text1'
  183. ])
  184. ->exec();
  185. }
  186. function testDeleteDoArticle() {
  187. $response = TestEnv::http()->newRequest()
  188. ->delete(['api', 'article',$this->article1Id])
  189. ->exec();
  190. $tx = TestEnv::createTransaction();
  191. $this->assertEquals(2,TestEnv::temUtil()->count(Article::class));
  192. $tx->commit();
  193. }
  194. /**
  195. * @throws StatusException
  196. */
  197. function testGetDoArticlesByCategoryName() {
  198. $response = TestEnv::http()->newRequest()
  199. ->get(['api', 'articles', 'news'])
  200. ->exec();
  201. }
  202. }