| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- 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\InternshipTestEnv;
- use util\GeneralTestEnv;
- class ArticleControllerTest extends TestCase {
- private int $article1Id;
- private int $article2Id;
- private int $article3Id;
- private int $category1Id;
- /**
- * @return void
- */
- function setUp(): void {
- GeneralTestEnv::tearDown();
- $tx = TestEnv::createTransaction();
- $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();
- }
- /**
- * 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();
- $articleStructs = $response->parseJson();
- $this->assertCount(3, $articleStructs);
- $this->assertEquals('Title 3', $articleStructs[0]['title']);
- $this->assertEquals('Title 2', $articleStructs[1]['title']);
- $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();
- }
- }
|