ArticleControllerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace internship\controller;
  3. use internship\bo\Article;
  4. use n2n\web\http\BadRequestException;
  5. use n2n\web\http\PageNotFoundException;
  6. use n2n\web\http\StatusException;
  7. use PHPUnit\Framework\TestCase;
  8. use n2n\test\TestEnv;
  9. use internship\test\InternshipTestEnv;
  10. use util\GeneralTestEnv;
  11. class ArticleControllerTest extends TestCase {
  12. private int $article1Id;
  13. private int $article2Id;
  14. private int $article3Id;
  15. private int $category1Id;
  16. /**
  17. * @return void
  18. */
  19. function setUp(): void {
  20. GeneralTestEnv::tearDown();
  21. $tx = TestEnv::createTransaction();
  22. $category1 = InternshipTestEnv::setUpCategory('category1');
  23. $category2 = InternshipTestEnv::setUpCategory('category2');
  24. $article1 = InternshipTestEnv::setUpArticle('Title 1', 'Loren ipsum 1', $category1);
  25. $article2 = InternshipTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', $category1);
  26. $article3 = InternshipTestEnv::setUpArticle('Title 3', 'Loren ipsum 3', $category2);
  27. $tx->commit();
  28. $this->article1Id = $article1->getId();
  29. $this->article2Id = $article2->getId();
  30. $this->article3Id = $article3->getId();
  31. $this->category1Id = $category1->getId();
  32. }
  33. /**
  34. * Get one article by id.
  35. *
  36. * @return void
  37. * @throws StatusException
  38. */
  39. function testGetDoArticle(): void {
  40. $responseJson = TestEnv::http()->newRequest()
  41. ->get(['api', 'article','1'])
  42. ->exec();
  43. $response = $responseJson->parseJson();
  44. $this->assertEquals('Title 1',$response['title']);
  45. $this->assertEquals('Loren ipsum 1',$response['text']);
  46. $this->assertEquals('category1',$response['categoryName']);
  47. }
  48. /**
  49. * @return void
  50. * @throws StatusException
  51. */
  52. function testGetDoArticleExpectExceptionBecauseWrongArticleID(): void {
  53. $this->expectException(PageNotFoundException::class);
  54. $responseJson = TestEnv::http()->newRequest()
  55. ->get(['api', 'article','15'])
  56. ->exec();
  57. }
  58. /**
  59. * Get all articles.
  60. *
  61. * @return void
  62. * @throws StatusException
  63. */
  64. function testGetDoArticles(): void {
  65. $response = TestEnv::http()->newRequest()
  66. ->get(['api', 'articles'])
  67. ->exec();
  68. $articleStructs = $response->parseJson();
  69. $this->assertCount(3, $articleStructs);
  70. $this->assertEquals('Title 3', $articleStructs[0]['title']);
  71. $this->assertEquals('Title 2', $articleStructs[1]['title']);
  72. $this->assertEquals('Title 1', $articleStructs[2]['title']);
  73. }
  74. /**
  75. * Exception because category does not exist
  76. *
  77. * @return void
  78. * @throws StatusException
  79. */
  80. function testGetDoArticlesExpectExceptionBecauseWrongCategoryName(): void {
  81. $this->expectException(PageNotFoundException::class);
  82. TestEnv::http()->newRequest()
  83. ->get(['api', 'articles','category3'])
  84. ->exec();
  85. }
  86. /**
  87. * @return void
  88. * @throws StatusException
  89. */
  90. function testPostDoArticle(): void {
  91. $responseJson = TestEnv::http()->newRequest()
  92. ->post(['api', 'article'])
  93. ->bodyJson(['categoryId' => 1, 'title' => 'All the Travel',
  94. 'text' => 'hello world'])
  95. ->exec();
  96. $article = $responseJson->parseJson();
  97. $this->assertEquals('All the Travel', $article['title']);
  98. $transaction = TestEnv::createTransaction(true);
  99. $correctArticle = TestEnv::tem()->find(Article::class,4);
  100. $this->assertEquals('All the Travel', $correctArticle->getTitle());
  101. $transaction->commit();
  102. }
  103. /**
  104. * When osting an {@see Article} without text, the text should be set to ''.
  105. *
  106. * @throws StatusException
  107. */
  108. function testPostDoArticleWithoutText() {
  109. $response = TestEnv::http()->newRequest()
  110. ->post(['api','article'])
  111. ->body('{"categoryId": 1, "title": "All the Travel"}')
  112. ->exec();
  113. $this->assertEquals('200',$response->getStatus());
  114. $transaction = TestEnv::createTransaction(true);
  115. $article = TestEnv::tem()->find(Article::class,4);
  116. $transaction->commit();
  117. $this->assertEquals('',$article->getText());
  118. }
  119. /**
  120. * @throws StatusException
  121. */
  122. function testPostDoArticleExpectExceptionBecauseCategoryDoesNotExist() {
  123. $this->expectException(BadRequestException::class);
  124. TestEnv::http()->newRequest()
  125. ->post(['api','article'])
  126. ->body('{"categoryId": 3, "title": "All the Travel", "text": "hello world"}')
  127. ->exec();
  128. }
  129. /**
  130. * Test: update the name of $article3. Then check if the name no longer matches the old one.
  131. *
  132. * @return void
  133. * @throws StatusException
  134. */
  135. function testPutDoArticle(): void {
  136. $response = TestEnv::http()->newRequest()
  137. ->put(['api', 'article','3'])
  138. ->bodyJson(['categoryId' => $this->category1Id , 'title'=> 'New Title','text'=>'Hello'])
  139. ->exec();
  140. $articleJson = $response->parseJson();
  141. $this->assertEquals('New Title', $articleJson['title']);
  142. $this->assertEquals('Hello', $articleJson['text']);
  143. $this->assertEquals('category1', $articleJson['categoryName']);
  144. $tx = TestEnv::createTransaction(true);
  145. $article = TestEnv::tem()->find(Article::class, 3);
  146. $this->assertEquals(3, TestEnv::temUtil()->count(Article::class));
  147. $this->assertEquals('New Title', $article->getTitle());
  148. $this->assertEquals(3, $article->getId());
  149. $tx->commit();
  150. }
  151. /**
  152. * @return void
  153. * @throws StatusException
  154. */
  155. function testPutDoArticleExpectExceptionBecauseWrongArticleId(): void {
  156. $this->expectException(PageNotFoundException::class);
  157. $response = TestEnv::http()->newRequest()
  158. ->put(['api', 'article','15'])
  159. ->bodyJson(['categoryId'=> $this->category1Id , 'title'=> 'New Title','text'=>'Hello'])
  160. ->exec();
  161. }
  162. /**
  163. * @return void
  164. * @throws StatusException
  165. */
  166. function testPutDoArticleExpectExceptionBecauseWrongCategoryId(): void {
  167. $this->expectException(PageNotFoundException::class);
  168. $response = TestEnv::http()->newRequest()
  169. ->put(['api', 'article','3'])
  170. ->bodyJson(['categoryId'=> 15 , 'title'=> 'New Title','text'=>'Hello'])
  171. ->exec();
  172. }
  173. /**
  174. * @return void
  175. * @throws StatusException
  176. */
  177. function testPutDoArticleExpectExceptionBecauseNoCategoryId(): void {
  178. $this->expectException(StatusException::class);
  179. // $this->expectExceptionCode('0');
  180. $this->expectExceptionMessage('Missing property: categoryId');
  181. $response = TestEnv::http()->newRequest()
  182. ->put(['api', 'article','3'])
  183. ->bodyJson(['title'=> 'New Title','text'=>'Hello'])
  184. ->exec();
  185. }
  186. /**
  187. * Delete $article3 then check if article count is 2.
  188. *
  189. * @return void
  190. * @throws StatusException
  191. */
  192. function testDeleteDoArticles () {
  193. $response = TestEnv::http()->newRequest()
  194. ->delete(['api','article','3'])
  195. ->exec();
  196. $this->assertEquals(200,$response->getStatus());
  197. $transaction = TestEnv::createTransaction(true);
  198. $this->assertEquals(2, TestEnv::temUtil()->count(Article::class));
  199. $this->assertEquals(null, TestEnv::tem()->find(Article::class,3));
  200. $transaction->commit();
  201. }
  202. /**
  203. * Delete request with an articleId that does not exist
  204. *
  205. * @throws StatusException
  206. */
  207. function testDeleteDoArticlesExpectExceptionBecauseWrongArticleId () {
  208. $this->expectException(PageNotFoundException::class);
  209. TestEnv::http()->newRequest()
  210. ->delete(['api','article','15'])
  211. ->exec();
  212. }
  213. }