commit(); $this->article1Id = $article1->getId(); $this->article2Id = $article2->getId(); $this->article3Id = $article3->getId(); } /** * @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->getTitle()); $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->getTitle()); $this->assertSame('Lorem ipsum 3', $article->getText()); $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->getTitle()); $this->assertSame('Text PUT', $article->getText()); $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(); $tx = TestEnv::createTransaction(true); $this->assertSame(2, TestEnv::temUtil()->count(Article::class)); $tx->commit(); } }