ArticleControllerTest.php 7.1 KB

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