|
|
@@ -7,6 +7,9 @@ use n2n\test\TestEnv;
|
|
|
use internship\test\ArticleTestEnv;
|
|
|
use util\GeneralTestEnv;
|
|
|
use n2n\web\http\StatusException;
|
|
|
+use n2n\web\http\PageNotFoundException;
|
|
|
+use internship\bo\Article;
|
|
|
+use n2n\web\http\BadRequestException;
|
|
|
|
|
|
|
|
|
class ArticleControllerTest extends TestCase {
|
|
|
@@ -19,9 +22,11 @@ class ArticleControllerTest extends TestCase {
|
|
|
GeneralTestEnv::tearDown();
|
|
|
|
|
|
$tx = TestEnv::createTransaction();
|
|
|
- $article1 = ArticleTestEnv::setUpArticle('Title 3', 'Loren ipsum 1', 'teaser');
|
|
|
- $article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'news');
|
|
|
- $article3 = ArticleTestEnv::setUpArticle('Title 1', 'Loren ipsum 3', 'news');
|
|
|
+ $article1 = ArticleTestEnv::setUpArticle('Title 3', 'Lorem ipsum 3', 'teaser');
|
|
|
+ $article2 = ArticleTestEnv::setUpArticle('Title 2', categoryName: 'news');
|
|
|
+ $article3 = ArticleTestEnv::setUpArticle('Title 1', categoryName: 'news');
|
|
|
+ //$article4 = ArticleTestEnv::setUpArticle('Title 4');
|
|
|
+ //var_dump($article4);
|
|
|
$tx->commit();
|
|
|
|
|
|
$this->article1Id = $article1->getId();
|
|
|
@@ -45,63 +50,143 @@ class ArticleControllerTest extends TestCase {
|
|
|
$this->assertEquals('Title 3', $articleStructs[2]['title']);
|
|
|
}
|
|
|
|
|
|
- function testGetArticleById() {
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testGetDoArticlesByCategoryName() {
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'articles', 'news'])
|
|
|
+ ->exec();
|
|
|
|
|
|
- // $articlePOST = ArticleTestEnv::getArticleById($this->article3Id);
|
|
|
+ $articleStructs = $response->parseJson();
|
|
|
+ $this->assertCount(2, $articleStructs);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testGetArticle() {
|
|
|
$response = TestEnv::http()->newRequest()
|
|
|
- ->get(['api', 'articles'])
|
|
|
+ ->get(['api', 'article', $this->article3Id])
|
|
|
->exec();
|
|
|
|
|
|
- $articleStructs = $response->parseJson();
|
|
|
+ $articleStructs = $response->parseJson();
|
|
|
|
|
|
- $this->assertEquals('Title 3', $articleStructs[2]['title']);
|
|
|
+ $this->assertEquals('Title 1', $articleStructs['title']);
|
|
|
}
|
|
|
|
|
|
- function testPostDoArticle() {
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testGetArticleNotFound() {
|
|
|
+ $this->expectException(PageNotFoundException::class);
|
|
|
|
|
|
- $tx = TestEnv::createTransaction();
|
|
|
- $articlePOST = ArticleTestEnv::setUpArticle('Title POST', 'Loren ipsum POST', 'teaser POST');
|
|
|
- $tx->commit();
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->get(['api', 'article', 1000])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
|
|
|
- $this->articlePOSTId = $articlePOST->getId();
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPostDoArticle() {
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
|
|
|
+ $tx->commit();
|
|
|
|
|
|
$response = TestEnv::http()->newRequest()
|
|
|
- ->get(['api', 'articles'])
|
|
|
- ->exec();
|
|
|
+ ->post(['api', 'article'])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'Title POST',
|
|
|
+ 'text' => 'Text POST',
|
|
|
+ 'categoryName' => 'sport',
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertEquals('Title POST', $articleStruct['title']);
|
|
|
+ $id = $articleStruct['id'];
|
|
|
+
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $this->assertSame(4, TestEnv::temUtil()->count(Article::class));
|
|
|
+
|
|
|
+ $article = TestEnv::tem()->find(Article::class, $id);
|
|
|
+ $this->assertSame('Title POST', $article->getTitle());
|
|
|
+ $tx->commit();
|
|
|
|
|
|
- $articleStructs = $response->parseJson();
|
|
|
- $this->assertCount(4, $articleStructs);
|
|
|
}
|
|
|
|
|
|
- function testPutDoArticle() {
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testPutDoArticle() {
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
|
|
|
+ $article = TestEnv::tem()->find(Article::class, $this->article1Id);
|
|
|
+ $this->assertSame('Title 3', $article->getTitle());
|
|
|
+ $this->assertSame('Lorem ipsum 3', $article->getText());
|
|
|
+ $tx->commit();
|
|
|
|
|
|
- $tx = TestEnv::createTransaction();
|
|
|
- ArticleTestEnv::updateArticle(1, 'Title PUT', 'Loren ipsum PUT', 'teaser PUT');
|
|
|
- $tx->commit();
|
|
|
+ $response = TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article', $this->article1Id])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'Title PUT',
|
|
|
+ 'text' => 'Text PUT',
|
|
|
+ 'categoryName' => 'sport',
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
|
|
|
- $response = TestEnv::http()->newRequest()
|
|
|
- ->get(['api', 'articles'])
|
|
|
- ->exec();
|
|
|
+ $articleStruct = $response->parseJson();
|
|
|
+ $this->assertEquals('Title PUT', $articleStruct['title']);
|
|
|
|
|
|
- $articleStructs = $response->parseJson();
|
|
|
- $this->assertCount(3, $articleStructs);
|
|
|
- $this->assertEquals('Title PUT', $articleStructs[2]['title']);
|
|
|
- //var_dump($articleStructs);
|
|
|
- }
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
|
|
|
|
|
|
- function testDeleteDoArticle() {
|
|
|
+ $article = TestEnv::tem()->find(Article::class, $this->article1Id);
|
|
|
+ $this->assertSame('Title PUT', $article->getTitle());
|
|
|
|
|
|
- $tx = TestEnv::createTransaction();
|
|
|
- ArticleTestEnv::removeArticle(3);
|
|
|
- $tx->commit();
|
|
|
+ $this->assertSame('Text PUT', $article->getText());
|
|
|
|
|
|
- $response = TestEnv::http()->newRequest()
|
|
|
- ->get(['api', 'articles'])
|
|
|
+ $tx->commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ * check {@link ArticleApiController::isValidArticleCategory} violation
|
|
|
+ */
|
|
|
+ function testPutDoArticleExpectExceptionBecauseOfInvalidCategoryName() {
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $categoryName = TestEnv::tem()->find(Article::class, $this->article1Id)->getCategoryName();
|
|
|
+ $tx->commit();
|
|
|
+
|
|
|
+ // only certain CategoryNames are allowed; in setup category restrictions are not checked.
|
|
|
+ $this->expectException(BadRequestException::class);
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->put(['api', 'article', $this->article1Id])
|
|
|
+ ->bodyJson([
|
|
|
+ 'title' => 'Title PUT',
|
|
|
+ 'text' => 'Text PUT',
|
|
|
+ 'categoryName' => $categoryName,
|
|
|
+ ])
|
|
|
+ ->exec();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws StatusException
|
|
|
+ */
|
|
|
+ function testDeleteDoArticle() {
|
|
|
+
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
|
|
|
+ $tx->commit();
|
|
|
+
|
|
|
+ TestEnv::http()->newRequest()
|
|
|
+ ->delete(['api', 'article', $this->article1Id])
|
|
|
->exec();
|
|
|
|
|
|
- $articleStructs = $response->parseJson();
|
|
|
- $this->assertCount(2, $articleStructs);
|
|
|
+ $tx = TestEnv::createTransaction(true);
|
|
|
+ $this->assertSame(2, TestEnv::temUtil()->count(Article::class));
|
|
|
+ $tx->commit();
|
|
|
}
|
|
|
|
|
|
}
|