| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace internship\controller;
- use PHPUnit\Framework\TestCase;
- use n2n\test\TestEnv;
- use internship\test\ArticleTestEnv;
- use util\GeneralTestEnv;
- use n2n\web\http\PageNotFoundException;
- use n2n\web\http\BadRequestException;
- use internship\bo\Article;
- use PHPUnit\Util\Json;
- use function MongoDB\BSON\toJSON;
- class ArticleControllerTest extends TestCase {
- private int $article1Id;
- private int $article2Id;
- private int $article3Id;
- function setUp(): void {
- GeneralTestEnv::tearDown();
- $tx = TestEnv::createTransaction();
- $article1 = ArticleTestEnv::setUpArticle('Title 1', 'Loren ipsum 1', 'sport');
- $article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'national');
- $article3 = ArticleTestEnv::setUpArticle('Title 3', 'Loren ipsum 3', 'national');
- $tx->commit();
- $this->article1Id = $article1->getId();
- $this->article2Id = $article2->getId();
- $this->article3Id = $article3->getId();
- }
- function testGetDoArticles() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'articles'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(3, $articleStructs);
- $this->assertEquals('Title 3', $articleStructs[0]['title']);
- $this->assertEquals('Title 2', $articleStructs[1]['title']);
- $this->assertEquals('Title 1', $articleStructs[2]['title']);
- }
- function testGetDoArticlesByCategoryName() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'articles', 'sport'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(1, $articleStructs);
- $this->assertEquals('Title 1', $articleStructs[0]['title']);
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'articles', 'national'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(2, $articleStructs);
- $this->assertEquals('Title 3', $articleStructs[0]['title']);
- $this->assertEquals('Title 2', $articleStructs[1]['title']);
- }
- function testGetDoArticle() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'article', '1'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(4, $articleStructs);
- $this->assertEquals('Title 1', $articleStructs['title']);
- $this->assertEquals('Loren ipsum 1', $articleStructs['text']);
- $this->assertEquals('sport', $articleStructs['categoryName']);
- $this->assertEquals('1', $articleStructs['id']);
- }
- function testGetDoArticlePageNotFound() {
- $this->expectException(PageNotFoundException::class);
- TestEnv::http()->newRequest()
- ->get(['api', 'article', '4'])
- ->exec();
- }
- function testPostDoNewArticleBadCategory() {
- $response = TestEnv::http()->newRequest()
- ->post(['api', 'newarticle'])
- ->bodyJson(['title' => 'Title 5', 'categoryName' => 'news', 'text' => 'Loren ipsum 5'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertEquals('ERR', $articleStructs['status']);
- }
- function testPostDoNewArticle() {
- $response = TestEnv::http()->newRequest()
- ->post(['api', 'newarticle'])
- ->bodyJson(['title' => 'Title 4', 'categoryName' => 'international', 'text' => 'Loren ipsum 4'])
- ->exec();
- $articleStruct = $response->parseJson();
- $article = TestEnv::em()->find(Article::getClass(), $articleStruct['id']);
- $this->assertEquals('Title 4', $article->getTitle());
- $this->assertEquals('Loren ipsum 4', $article->getText());
- $this->assertEquals('international', $article->getCategoryName());
- $this->assertEquals($articleStruct['id'], $article->getId());
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'articles'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(4, $articleStructs);
- }
- function testPutDoArticle() {
- TestEnv::http()->newRequest()
- ->put(['api', 'article', '1'])
- ->bodyJson(['title' => 'Title 4', 'categoryName' => 'international', 'text' => 'Loren ipsum 4'])
- ->exec();
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'article', '1'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(4, $articleStructs);
- $this->assertEquals('Title 4', $articleStructs['title']);
- $this->assertEquals('Loren ipsum 4', $articleStructs['text']);
- $this->assertEquals('international', $articleStructs['categoryName']);
- $this->assertEquals('1', $articleStructs['id']);
- }
- function testDeleteDoArticle() {
- TestEnv::http()->newRequest()
- ->delete(['api', 'article', '1'])
- ->exec();
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'articles'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(2, $articleStructs);
- $this->expectException(PageNotFoundException::class);
- TestEnv::http()->newRequest()
- ->get(['api', 'article', '1'])
- ->exec();
- }
- }
|