ArticleControllerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\PageNotFoundException;
  8. use n2n\web\http\BadRequestException;
  9. use internship\bo\Article;
  10. use PHPUnit\Util\Json;
  11. use function MongoDB\BSON\toJSON;
  12. class ArticleControllerTest extends TestCase {
  13. private int $article1Id;
  14. private int $article2Id;
  15. private int $article3Id;
  16. function setUp(): void {
  17. GeneralTestEnv::tearDown();
  18. $tx = TestEnv::createTransaction();
  19. $article1 = ArticleTestEnv::setUpArticle('Title 1', 'Loren ipsum 1', 'sport');
  20. $article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'national');
  21. $article3 = ArticleTestEnv::setUpArticle('Title 3', 'Loren ipsum 3', 'national');
  22. $tx->commit();
  23. $this->article1Id = $article1->getId();
  24. $this->article2Id = $article2->getId();
  25. $this->article3Id = $article3->getId();
  26. }
  27. function testGetDoArticles() {
  28. $response = TestEnv::http()->newRequest()
  29. ->get(['api', 'articles'])
  30. ->exec();
  31. $articleStructs = $response->parseJson();
  32. $this->assertCount(3, $articleStructs);
  33. $this->assertEquals('Title 3', $articleStructs[0]['title']);
  34. $this->assertEquals('Title 2', $articleStructs[1]['title']);
  35. $this->assertEquals('Title 1', $articleStructs[2]['title']);
  36. }
  37. function testGetDoArticlesByCategoryName() {
  38. $response = TestEnv::http()->newRequest()
  39. ->get(['api', 'articles', 'sport'])
  40. ->exec();
  41. $articleStructs = $response->parseJson();
  42. $this->assertCount(1, $articleStructs);
  43. $this->assertEquals('Title 1', $articleStructs[0]['title']);
  44. $response = TestEnv::http()->newRequest()
  45. ->get(['api', 'articles', 'national'])
  46. ->exec();
  47. $articleStructs = $response->parseJson();
  48. $this->assertCount(2, $articleStructs);
  49. $this->assertEquals('Title 3', $articleStructs[0]['title']);
  50. $this->assertEquals('Title 2', $articleStructs[1]['title']);
  51. }
  52. function testGetDoArticle() {
  53. $response = TestEnv::http()->newRequest()
  54. ->get(['api', 'article', '1'])
  55. ->exec();
  56. $articleStructs = $response->parseJson();
  57. $this->assertCount(4, $articleStructs);
  58. $this->assertEquals('Title 1', $articleStructs['title']);
  59. $this->assertEquals('Loren ipsum 1', $articleStructs['text']);
  60. $this->assertEquals('sport', $articleStructs['categoryName']);
  61. $this->assertEquals('1', $articleStructs['id']);
  62. }
  63. function testGetDoArticlePageNotFound() {
  64. $this->expectException(PageNotFoundException::class);
  65. TestEnv::http()->newRequest()
  66. ->get(['api', 'article', '4'])
  67. ->exec();
  68. }
  69. function testPostDoNewArticleBadCategory() {
  70. $response = TestEnv::http()->newRequest()
  71. ->post(['api', 'newarticle'])
  72. ->bodyJson(['title' => 'Title 5', 'categoryName' => 'news', 'text' => 'Loren ipsum 5'])
  73. ->exec();
  74. $articleStructs = $response->parseJson();
  75. $this->assertEquals('ERR', $articleStructs['status']);
  76. }
  77. function testPostDoNewArticle() {
  78. $response = TestEnv::http()->newRequest()
  79. ->post(['api', 'newarticle'])
  80. ->bodyJson(['title' => 'Title 4', 'categoryName' => 'international', 'text' => 'Loren ipsum 4'])
  81. ->exec();
  82. $articleStruct = $response->parseJson();
  83. $article = TestEnv::em()->find(Article::getClass(), $articleStruct['id']);
  84. $this->assertEquals('Title 4', $article->getTitle());
  85. $this->assertEquals('Loren ipsum 4', $article->getText());
  86. $this->assertEquals('international', $article->getCategoryName());
  87. $this->assertEquals($articleStruct['id'], $article->getId());
  88. $response = TestEnv::http()->newRequest()
  89. ->get(['api', 'articles'])
  90. ->exec();
  91. $articleStructs = $response->parseJson();
  92. $this->assertCount(4, $articleStructs);
  93. }
  94. function testPutDoArticle() {
  95. TestEnv::http()->newRequest()
  96. ->put(['api', 'article', '1'])
  97. ->bodyJson(['title' => 'Title 4', 'categoryName' => 'international', 'text' => 'Loren ipsum 4'])
  98. ->exec();
  99. $response = TestEnv::http()->newRequest()
  100. ->get(['api', 'article', '1'])
  101. ->exec();
  102. $articleStructs = $response->parseJson();
  103. $this->assertCount(4, $articleStructs);
  104. $this->assertEquals('Title 4', $articleStructs['title']);
  105. $this->assertEquals('Loren ipsum 4', $articleStructs['text']);
  106. $this->assertEquals('international', $articleStructs['categoryName']);
  107. $this->assertEquals('1', $articleStructs['id']);
  108. }
  109. function testDeleteDoArticle() {
  110. TestEnv::http()->newRequest()
  111. ->delete(['api', 'article', '1'])
  112. ->exec();
  113. $response = TestEnv::http()->newRequest()
  114. ->get(['api', 'articles'])
  115. ->exec();
  116. $articleStructs = $response->parseJson();
  117. $this->assertCount(2, $articleStructs);
  118. $this->expectException(PageNotFoundException::class);
  119. TestEnv::http()->newRequest()
  120. ->get(['api', 'article', '1'])
  121. ->exec();
  122. }
  123. }