| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace internship\controller;
- use PHPUnit\Framework\TestCase;
- 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;
- use ArrayObject;
- use internship\bo\Category;
- 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 3', 'Lorem ipsum 3', 'teaser');
- $article2 = ArticleTestEnv::setUpArticle('Title 2', categoryName: 'news');
- $article3 = ArticleTestEnv::setUpArticle('Title 1', categoryName: 'news');
- $cat = new Category();
- $cat->name = 'Category 1';
- $cat->articles = new ArrayObject([$article1, $article2]);
- $article1->categories = new ArrayObject([$cat]);
- $article3->categories = new ArrayObject([$cat]);
- //$article4 = ArticleTestEnv::setUpArticle('Title 4');
- //var_dump($article4);
- $tx->commit();
- $this->article1Id = $article1->id;
- $this->article2Id = $article2->id;
- $this->article3Id = $article3->id;
- }
- /**
- * @throws StatusException
- */
- function testGetDoArticles() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'articles'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(3, $articleStructs);
- $this->assertEquals('Title 1', $articleStructs[0]['title']);
- $this->assertEquals('Title 2', $articleStructs[1]['title']);
- $this->assertEquals('Title 3', $articleStructs[2]['title']);
- }
- /**
- * @throws StatusException
- */
- function testGetDoArticlesByCategoryName() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'articles', 'news'])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertCount(2, $articleStructs);
- }
- /**
- * @throws StatusException
- */
- function testGetArticle() {
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'article', $this->article3Id])
- ->exec();
- $articleStructs = $response->parseJson();
- $this->assertEquals('Title 1', $articleStructs['title']);
- }
- /**
- * @throws StatusException
- */
- function testGetArticleNotFound() {
- $this->expectException(PageNotFoundException::class);
- $response = TestEnv::http()->newRequest()
- ->get(['api', 'article', 1000])
- ->exec();
- }
- /**
- * @throws StatusException
- */
- function testPostDoArticle() {
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
- $tx->commit();
- $response = TestEnv::http()->newRequest()
- ->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->title);
- $tx->commit();
- }
- /**
- * @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->title);
- $this->assertSame('Lorem ipsum 3', $article->text);
- $tx->commit();
- $response = TestEnv::http()->newRequest()
- ->put(['api', 'article', $this->article1Id])
- ->bodyJson([
- 'title' => 'Title PUT',
- 'text' => 'Text PUT',
- 'categoryName' => 'sport',
- ])
- ->exec();
- $articleStruct = $response->parseJson();
- $this->assertEquals('Title PUT', $articleStruct['title']);
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
- $article = TestEnv::tem()->find(Article::class, $this->article1Id);
- $this->assertSame('Title PUT', $article->title);
- $this->assertSame('Text PUT', $article->text);
- $tx->commit();
- }
- /**
- * @throws StatusException
- * check {@link ArticleApiController::isValidArticleCategory} violation
- */
- function testPutDoArticleExpectExceptionBecauseOfInvalidCategoryName() {
- $tx = TestEnv::createTransaction(true);
- $categoryName = TestEnv::tem()->find(Article::class, $this->article1Id)->categoryName;
- $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));
- $this->assertSame(1, TestEnv::temUtil()->count(Category::class));
- $tx->commit();
- TestEnv::http()->newRequest()
- ->delete(['api', 'article', $this->article1Id])
- ->exec();
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(2, TestEnv::temUtil()->count(Article::class));
- $this->assertSame(1, TestEnv::temUtil()->count(Category::class));
- $tx->commit();
- }
- function testDeleteCat() {
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
- $this->assertSame(1, TestEnv::temUtil()->count(Category::class));
- $tx->commit();
- $tx = TestEnv::createTransaction();
- $cat = TestEnv::tem()->find(Category::class, 1);
- TestEnv::tem()->remove($cat);
- $tx->commit();
- $tx = TestEnv::createTransaction(true);
- $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
- $this->assertSame(0, TestEnv::temUtil()->count(Category::class));
- $tx->commit();
- }
- }
|