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(); } }