ArticleControllerTest.php 7.3 KB

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