|
|
@@ -6,7 +6,9 @@ use PHPUnit\Framework\TestCase;
|
|
|
use n2n\test\TestEnv;
|
|
|
use internship\test\ArticleTestEnv;
|
|
|
use util\GeneralTestEnv;
|
|
|
-
|
|
|
+use n2n\web\http\PageNotFoundException;
|
|
|
+use internship\bo\Article;
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
|
|
|
class ArticleControllerTest extends TestCase {
|
|
|
|
|
|
@@ -18,8 +20,8 @@ class ArticleControllerTest extends TestCase {
|
|
|
GeneralTestEnv::tearDown();
|
|
|
|
|
|
$tx = TestEnv::createTransaction();
|
|
|
- $article1 = ArticleTestEnv::setUpArticle('Title 1', 'Loren ipsum 1', 'teaser');
|
|
|
- $article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'news');
|
|
|
+ $article1 = ArticleTestEnv::setUpArticle('Title 1', 'Loren ipsum 1', 'news');
|
|
|
+ $article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'teaser');
|
|
|
$article3 = ArticleTestEnv::setUpArticle('Title 3', 'Loren ipsum 3', 'news');
|
|
|
$tx->commit();
|
|
|
|
|
|
@@ -33,12 +35,201 @@ class ArticleControllerTest extends TestCase {
|
|
|
->get(['api', 'articles'])
|
|
|
->exec();
|
|
|
|
|
|
- $articleStructs = $response->parseJson();
|
|
|
- $this->assertCount(3, $articleStructs);
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(3, $articleStruct);
|
|
|
+
|
|
|
+ $this->assertEquals('Title 3', $articleStruct[0]['title']);
|
|
|
+ $this->assertEquals('Title 2', $articleStruct[1]['title']);
|
|
|
+ $this->assertEquals('Title 1', $articleStruct[2]['title']);
|
|
|
+ }
|
|
|
+
|
|
|
+ function testGetDoArticlesWithCategory() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'articles', 'teaser'])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(1, $articleStruct);
|
|
|
+
|
|
|
+ $this->assertEquals('Title 2', $articleStruct[0]['title']);
|
|
|
+ }
|
|
|
+
|
|
|
+ function testGetDoArticlesWithCategory2() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'articles', 'news'])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(2, $articleStruct);
|
|
|
+
|
|
|
+ $this->assertEquals('Title 3', $articleStruct[0]['title']);
|
|
|
+ $this->assertEquals('Title 1', $articleStruct[1]['title']);
|
|
|
+ }
|
|
|
+
|
|
|
+ function testGetDoArticlesExpectExceptionBecauseCategoryNotFound() {
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+ $this->expectExceptionMessage('Category abc not found');
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'articles', 'abc'])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function testGetDoArticle() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'article', $this->article1Id])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(4, $articleStruct);
|
|
|
|
|
|
- $this->assertEquals('Title 3', $articleStructs[0]['title']);
|
|
|
- $this->assertEquals('Title 2', $articleStructs[1]['title']);
|
|
|
- $this->assertEquals('Title 1', $articleStructs[2]['title']);
|
|
|
+ $this->assertEquals('Title 1', $articleStruct['title']);
|
|
|
+ $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
|
|
|
+ $this->assertEquals('news', $articleStruct['categoryName']);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function testGetDoArticleExpectExceptionBecauseArticleNotFound() {
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+ $this->expectExceptionMessage('Article not found with ID:25436857');
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'article', 25436857])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPostDoArticle() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->post(['api', 'article'])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'title1',
|
|
|
+ 'categoryName' => 'sport',
|
|
|
+ 'text' => 'text1'
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertEquals(4,TestEnv::emUtil()->count(Article::class));
|
|
|
+ $this->assertCount(4, $articleStruct);
|
|
|
+
|
|
|
+ $this->assertEquals('title1', $articleStruct['title']);
|
|
|
+ $this->assertEquals('text1', $articleStruct['text']);
|
|
|
+ $this->assertEquals('sport', $articleStruct['categoryName']);
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPostDoArticleExpectExceptionBecauseCategoryNotFound() {
|
|
|
+ $this->expectException(BadRequestException::class);
|
|
|
+ $this->expectExceptionMessage('The Category must be international, national or sport');
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->post(['api', 'article'])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'title1',
|
|
|
+ 'categoryName' => 'abc',
|
|
|
+ 'text' => 'text1'
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPutDoArticle() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article',$this->article1Id])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'title1',
|
|
|
+ 'categoryName' => 'sport',
|
|
|
+ 'text' => 'text1'
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(4, $articleStruct);
|
|
|
+ $this->assertEquals('title1', $articleStruct['title']);
|
|
|
+ $this->assertEquals('text1', $articleStruct['text']);
|
|
|
+ $this->assertEquals('sport', $articleStruct['categoryName']);
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPutDoArticle1() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article',$this->article1Id])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'title1',
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(4, $articleStruct);
|
|
|
+ $this->assertEquals('title1', $articleStruct['title']);
|
|
|
+ $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
|
|
|
+ $this->assertEquals('news', $articleStruct['categoryName']);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPutDoArticle2() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article',$this->article1Id])
|
|
|
+ ->bodyJson([
|
|
|
+ 'categoryName' => 'sport',
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(4, $articleStruct);
|
|
|
+ $this->assertEquals('Title 1', $articleStruct['title']);
|
|
|
+ $this->assertEquals('Loren ipsum 1', $articleStruct['text']);
|
|
|
+ $this->assertEquals('sport', $articleStruct['categoryName']);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPutDoArticle3() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article',$this->article1Id])
|
|
|
+ ->bodyJson([
|
|
|
+ 'text' => 'text1',
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertCount(4, $articleStruct);
|
|
|
+ $this->assertEquals('Title 1', $articleStruct['title']);
|
|
|
+ $this->assertEquals('text1', $articleStruct['text']);
|
|
|
+ $this->assertEquals('news', $articleStruct['categoryName']);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPutDoArticleExpectExceptionBecauseCategoryNotFound() {
|
|
|
+ $this->expectException(BadRequestException::class);
|
|
|
+ $this->expectExceptionMessage('The Category must be international, national or sport');
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article',$this->article1Id])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'title1',
|
|
|
+ 'categoryName' => 'abc',
|
|
|
+ 'text' => 'text1'
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ function testPutDoArticleExpectExceptionBecauseArticleNotFound() {
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+ $this->expectExceptionMessage('Article not found');
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article', 25436857])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'title1',
|
|
|
+ 'categoryName' => 'abc',
|
|
|
+ 'text' => 'text1'
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function testDeleteDoArticle() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->delete(['api', 'article',$this->article1Id])
|
|
|
+ ->exec();
|
|
|
+ $tx = TestEnv::createTransaction();
|
|
|
+ $this->assertEquals(2,TestEnv::temUtil()->count(Article::class));
|
|
|
+ $tx->commit();
|
|
|
}
|
|
|
|
|
|
}
|