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