ArticleControllerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. $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. /**
  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. $articleStruct = $response->parseJson();
  93. $this->assertEquals(4,TestEnv::emUtil()->count(Article::class));
  94. $this->assertCount(4, $articleStruct);
  95. $this->assertEquals('title1', $articleStruct['title']);
  96. $this->assertEquals('text1', $articleStruct['text']);
  97. $this->assertEquals('sport', $articleStruct['categoryName']);
  98. }
  99. /**
  100. * @throws StatusException
  101. */
  102. function testPostDoArticleExpectExceptionBecauseCategoryNotFound() {
  103. $this->expectException(BadRequestException::class);
  104. $this->expectExceptionMessage('The Category must be international, national or sport');
  105. TestEnv::http()->newRequest()
  106. ->post(['api', 'article'])
  107. ->bodyJson([
  108. 'title' => 'title1',
  109. 'categoryName' => 'abc',
  110. 'text' => 'text1'
  111. ])
  112. ->exec();
  113. }
  114. /**
  115. * @throws StatusException
  116. */
  117. function testPostDoArticleExpectExceptionBecauseTitleTooLong() {
  118. $this->expectException(BadRequestException::class);
  119. $this->expectExceptionMessage('Title is too long');
  120. TestEnv::http()->newRequest()
  121. ->post(['api', 'article'])
  122. ->bodyJson([
  123. 'title' => str_repeat('t', 256),
  124. 'categoryName' => 'sport',
  125. 'text' => 'text1'
  126. ])
  127. ->exec();
  128. }
  129. function testPutDoArticle() {
  130. $response = TestEnv::http()->newRequest()
  131. ->put(['api', 'article',$this->article1Id])
  132. ->bodyJson([
  133. 'title' => 'title1',
  134. 'categoryName' => 'sport',
  135. 'text' => 'text1'
  136. ])
  137. ->exec();
  138. $articleStruct = $response->parseJson();
  139. $this->assertCount(4, $articleStruct);
  140. $this->assertEquals('title1', $articleStruct['title']);
  141. $this->assertEquals('text1', $articleStruct['text']);
  142. $this->assertEquals('sport', $articleStruct['categoryName']);
  143. }
  144. function testPutDoArticle1() {
  145. $response = TestEnv::http()->newRequest()
  146. ->put(['api', 'article',$this->article1Id])
  147. ->bodyJson([
  148. 'title' => 'title1',
  149. ])
  150. ->exec();
  151. $articleStruct = $response->parseJson();
  152. $this->assertCount(4, $articleStruct);
  153. $this->assertEquals('title1', $articleStruct['title']);
  154. $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
  155. $this->assertEquals('news', $articleStruct['categoryName']);
  156. }
  157. function testPutDoArticle2() {
  158. $response = TestEnv::http()->newRequest()
  159. ->put(['api', 'article',$this->article1Id])
  160. ->bodyJson([
  161. 'categoryName' => 'sport',
  162. ])
  163. ->exec();
  164. $articleStruct = $response->parseJson();
  165. $this->assertCount(4, $articleStruct);
  166. $this->assertEquals('Title 1', $articleStruct['title']);
  167. $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
  168. $this->assertEquals('sport', $articleStruct['categoryName']);
  169. }
  170. function testPutDoArticle3() {
  171. $response = TestEnv::http()->newRequest()
  172. ->put(['api', 'article',$this->article1Id])
  173. ->bodyJson([
  174. 'text' => 'text1',
  175. ])
  176. ->exec();
  177. $articleStruct = $response->parseJson();
  178. $this->assertCount(4, $articleStruct);
  179. $this->assertEquals('Title 1', $articleStruct['title']);
  180. $this->assertEquals('text1', $articleStruct['text']);
  181. $this->assertEquals('news', $articleStruct['categoryName']);
  182. }
  183. function testPutDoArticleExpectExceptionBecauseCategoryNotFound() {
  184. $this->expectException(BadRequestException::class);
  185. $this->expectExceptionMessage('The Category must be international, national or sport');
  186. TestEnv::http()->newRequest()
  187. ->put(['api', 'article',$this->article1Id])
  188. ->bodyJson([
  189. 'title' => 'title1',
  190. 'categoryName' => 'abc',
  191. 'text' => 'text1'
  192. ])
  193. ->exec();
  194. }
  195. function testPutDoArticleExpectExceptionBecauseArticleNotFound() {
  196. $this->expectException(PageNotFoundException::class);
  197. $this->expectExceptionMessage('Article not found');
  198. TestEnv::http()->newRequest()
  199. ->put(['api', 'article', 25436857])
  200. ->bodyJson([
  201. 'title' => 'title1',
  202. 'categoryName' => 'abc',
  203. 'text' => 'text1'
  204. ])
  205. ->exec();
  206. }
  207. /**
  208. * @throws StatusException
  209. */
  210. function testPutDoArticleExpectExceptionBecauseTitleTooLong() {
  211. $this->expectException(BadRequestException::class);
  212. $this->expectExceptionMessage('Title is too long');
  213. $response = TestEnv::http()->newRequest()
  214. ->put(['api', 'article', $this->article1Id])
  215. ->bodyJson([
  216. 'title' => str_repeat('t', 256),
  217. 'categoryName' => 'sport',
  218. 'text' => 'text1'
  219. ])
  220. ->exec();
  221. }
  222. function testDeleteDoArticle() {
  223. $response = TestEnv::http()->newRequest()
  224. ->delete(['api', 'article',$this->article1Id])
  225. ->exec();
  226. $tx = TestEnv::createTransaction();
  227. $this->assertEquals(2,TestEnv::temUtil()->count(Article::class));
  228. $tx->commit();
  229. }
  230. }