|
@@ -6,6 +6,11 @@ use PHPUnit\Framework\TestCase;
|
|
|
use n2n\test\TestEnv;
|
|
use n2n\test\TestEnv;
|
|
|
use internship\test\ArticleTestEnv;
|
|
use internship\test\ArticleTestEnv;
|
|
|
use util\GeneralTestEnv;
|
|
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 {
|
|
class ArticleControllerTest extends TestCase {
|
|
@@ -18,9 +23,9 @@ class ArticleControllerTest extends TestCase {
|
|
|
GeneralTestEnv::tearDown();
|
|
GeneralTestEnv::tearDown();
|
|
|
|
|
|
|
|
$tx = TestEnv::createTransaction();
|
|
$tx = TestEnv::createTransaction();
|
|
|
- $article1 = ArticleTestEnv::setUpArticle('Title 1', 'Loren ipsum 1', 'teaser');
|
|
|
|
|
- $article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'news');
|
|
|
|
|
- $article3 = ArticleTestEnv::setUpArticle('Title 3', 'Loren ipsum 3', 'news');
|
|
|
|
|
|
|
+ $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();
|
|
$tx->commit();
|
|
|
|
|
|
|
|
$this->article1Id = $article1->getId();
|
|
$this->article1Id = $article1->getId();
|
|
@@ -40,5 +45,105 @@ class ArticleControllerTest extends TestCase {
|
|
|
$this->assertEquals('Title 2', $articleStructs[1]['title']);
|
|
$this->assertEquals('Title 2', $articleStructs[1]['title']);
|
|
|
$this->assertEquals('Title 1', $articleStructs[2]['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() {
|
|
|
|
|
+ $this->expectException(BadRequestException::class);
|
|
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
|
|
+ ->post(['api', 'newarticle'])
|
|
|
|
|
+ ->bodyJson(['title' => 'Title 5', 'categoryName' => 'news', 'text' => 'Loren ipsum 5'])
|
|
|
|
|
+ ->exec();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ 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();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
}
|
|
}
|