|
|
@@ -2,9 +2,13 @@
|
|
|
|
|
|
namespace internship\controller;
|
|
|
|
|
|
+use internship\bo\Article;
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
+use n2n\web\http\PageNotFoundException;
|
|
|
+use n2n\web\http\StatusException;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
use n2n\test\TestEnv;
|
|
|
-use internship\test\ArticleTestEnv;
|
|
|
+use internship\test\InternshipTestEnv;
|
|
|
use util\GeneralTestEnv;
|
|
|
|
|
|
|
|
|
@@ -14,21 +18,65 @@ class ArticleControllerTest extends TestCase {
|
|
|
private int $article2Id;
|
|
|
private int $article3Id;
|
|
|
|
|
|
+ private int $category1Id;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
function setUp(): void {
|
|
|
GeneralTestEnv::tearDown();
|
|
|
|
|
|
$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');
|
|
|
+ $category1 = InternshipTestEnv::setUpCategory('category1');
|
|
|
+ $category2 = InternshipTestEnv::setUpCategory('category2');
|
|
|
+ $article1 = InternshipTestEnv::setUpArticle('Title 1', 'Loren ipsum 1', $category1);
|
|
|
+ $article2 = InternshipTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', $category1);
|
|
|
+ $article3 = InternshipTestEnv::setUpArticle('Title 3', 'Loren ipsum 3', $category2);
|
|
|
$tx->commit();
|
|
|
|
|
|
$this->article1Id = $article1->getId();
|
|
|
$this->article2Id = $article2->getId();
|
|
|
$this->article3Id = $article3->getId();
|
|
|
+
|
|
|
+ $this->category1Id = $category1->getId();
|
|
|
}
|
|
|
|
|
|
- function testGetDoArticles() {
|
|
|
+ /**
|
|
|
+ * Get one article by id.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testGetDoArticle(): void {
|
|
|
+ $responseJson = TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'article','1'])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ $response = $responseJson->parseJson();
|
|
|
+ $this->assertEquals('Title 1',$response['title']);
|
|
|
+ $this->assertEquals('Loren ipsum 1',$response['text']);
|
|
|
+ $this->assertEquals('category1',$response['categoryName']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testGetDoArticleExpectExceptionBecauseWrongArticleID(): void {
|
|
|
+
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+ $responseJson = TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'article','15'])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get all articles.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testGetDoArticles(): void {
|
|
|
$response = TestEnv::http()->newRequest()
|
|
|
->get(['api', 'articles'])
|
|
|
->exec();
|
|
|
@@ -41,4 +89,163 @@ class ArticleControllerTest extends TestCase {
|
|
|
$this->assertEquals('Title 1', $articleStructs[2]['title']);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Exception because category does not exist
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testGetDoArticlesExpectExceptionBecauseWrongCategoryName(): void {
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'articles','category3'])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPostDoArticle(): void {
|
|
|
+ $responseJson = TestEnv::http()->newRequest()
|
|
|
+ ->post(['api', 'article'])
|
|
|
+ ->bodyJson(['categoryId' => 1, 'title' => 'All the Travel',
|
|
|
+ 'text' => 'hello world'])
|
|
|
+ ->exec();
|
|
|
+ $article = $responseJson->parseJson();
|
|
|
+ $this->assertEquals('All the Travel', $article['title']);
|
|
|
+
|
|
|
+ $transaction = TestEnv::createTransaction(true);
|
|
|
+ $correctArticle = TestEnv::tem()->find(Article::class,4);
|
|
|
+ $this->assertEquals('All the Travel', $correctArticle->getTitle());
|
|
|
+ $transaction->commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * When osting an {@see Article} without text, the text should be set to ''.
|
|
|
+ *
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPostDoArticleWithoutText() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->post(['api','article'])
|
|
|
+ ->body('{"categoryId": 1, "title": "All the Travel"}')
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+
|
|
|
+ $this->assertEquals('200',$response->getStatus());
|
|
|
+ $transaction = TestEnv::createTransaction(true);
|
|
|
+ $article = TestEnv::tem()->find(Article::class,4);
|
|
|
+ $transaction->commit();
|
|
|
+ $this->assertEquals('',$article->getText());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPostDoArticleExpectExceptionBecauseCategoryDoesNotExist() {
|
|
|
+ $this->expectException(BadRequestException::class);
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->post(['api','article'])
|
|
|
+ ->body('{"categoryId": 3, "title": "All the Travel", "text": "hello world"}')
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test: update the name of $article3. Then check if the name no longer matches the old one.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPutDoArticle(): void {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article','3'])
|
|
|
+ ->bodyJson(['categoryId' => $this->category1Id , 'title'=> 'New Title','text'=>'Hello'])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ $articleJson = $response->parseJson();
|
|
|
+ $this->assertEquals('New Title', $articleJson['title']);
|
|
|
+ $this->assertEquals('Hello', $articleJson['text']);
|
|
|
+ $this->assertEquals('category1', $articleJson['categoryName']);
|
|
|
+
|
|
|
+
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $article = TestEnv::tem()->find(Article::class, 3);
|
|
|
+ $this->assertEquals(3, TestEnv::temUtil()->count(Article::class));
|
|
|
+ $this->assertEquals('New Title', $article->getTitle());
|
|
|
+ $this->assertEquals(3, $article->getId());
|
|
|
+ $tx->commit();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPutDoArticleExpectExceptionBecauseWrongArticleId(): void {
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article','15'])
|
|
|
+ ->bodyJson(['categoryId'=> $this->category1Id , 'title'=> 'New Title','text'=>'Hello'])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPutDoArticleExpectExceptionBecauseWrongCategoryId(): void {
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article','3'])
|
|
|
+ ->bodyJson(['categoryId'=> 15 , 'title'=> 'New Title','text'=>'Hello'])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPutDoArticleExpectExceptionBecauseNoCategoryId(): void {
|
|
|
+ $this->expectException(StatusException::class);
|
|
|
+// $this->expectExceptionCode('0');
|
|
|
+ $this->expectExceptionMessage('Missing property: categoryId');
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article','3'])
|
|
|
+ ->bodyJson(['title'=> 'New Title','text'=>'Hello'])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Delete $article3 then check if article count is 2.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testDeleteDoArticles () {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->delete(['api','article','3'])
|
|
|
+ ->exec();
|
|
|
+ $this->assertEquals(200,$response->getStatus());
|
|
|
+ $transaction = TestEnv::createTransaction(true);
|
|
|
+ $this->assertEquals(2, TestEnv::temUtil()->count(Article::class));
|
|
|
+ $this->assertEquals(null, TestEnv::tem()->find(Article::class,3));
|
|
|
+ $transaction->commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Delete request with an articleId that does not exist
|
|
|
+ *
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testDeleteDoArticlesExpectExceptionBecauseWrongArticleId () {
|
|
|
+
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->delete(['api','article','15'])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|