Explorar el Código

.phpunit.result.cache ignored

Andreas von Burg hace 1 mes
padre
commit
37cd1d096b

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
src-php/.phpunit.result.cache


+ 71 - 63
src-php/app/internship/controller/ArticleApiController.php

@@ -9,6 +9,7 @@ use internship\bo\Article;
 use n2n\validation\plan\ErrorMap;
 use n2n\web\http\BadRequestException;
 use n2n\web\http\PageNotFoundException;
+use n2n\web\http\StatusException;
 
 
 /**
@@ -102,92 +103,99 @@ class ArticleApiController extends ControllerAdapter {
 	 * Speichere einen Artikel.
 	 *
 	 * <ul>
-	 * 	<li>
-	 * 		Der Kategoriename darf nur 'international','national' oder 'sport' sein.
-	 * 		Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
-	 * 	</li>
-	 * 	<li>
-	 *		Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
-	 * 	</li>
+	 *    <li>
+	 *        Der Kategoriename darf nur 'international','national' oder 'sport' sein.
+	 *        Validiere dies und schmeisse im Fehlerfall eine: {@see BadRequestException}
+	 *    </li>
+	 *    <li>
+	 *        Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
+	 *    </li>
 	 *  <li>
-	 *		Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
-	 * 	 </li>
+	 *        Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
+	 *     </li>
 	 * </ul>
 	 *
 	 * Nenne die Methode {@see saveArticle(Aŋ@rticle $article)}
 	 *
 	 * @return void
+	 * @throws StatusException
 	 */
-	function badRequestExceptionIfs(ParamBody $body, $articleId = null) { 
-		$this->beginTransaction();
-		$article = $this->articleDao->getArticleById($articleId);
+	function badRequestExceptionIfs(ParamBody $body, ?Article $article = null): Article {
 		$bodyData = $body->parseJson();
 		if($body->getValue() === "") {
-				throw new BadRequestException('Body cant be empty');
-			}
-			$pattern = '/^{/';
-			$pattern2 = '/}$/';
-
-			if(preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
-				throw new BadRequestException('Json is invalid');
-			}
-
-			if(!isset($bodyData['title'])) {
-				throw new BadRequestException('Title must be set');
-			}
-
-			if (strlen($bodyData['title']) > 255){
-				throw new BadRequestException('Title is too long');
-			}
+			throw new BadRequestException('Body cant be empty');
+		}
+		$pattern = '/^{/';
+		$pattern2 = '/}$/';
 
-			if (!isset($bodyData['categoryName'])) {
-				throw new BadRequestException('The Category must be international, national or sport');
-			}
+		if (preg_match($pattern2, $body) == 0 || preg_match($pattern, $body) == 0) {
+			throw new BadRequestException('Json is invalid');
+		}
 
-			if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
-				throw new BadRequestException('The Category must be international, national or sport');
-			}
+		if (!isset($bodyData['title'])) {
+			throw new BadRequestException('Title must be set');
+		}
 
-			if(!isset($bodyData['text'])) {
-				throw new BadRequestException('Article must contain a text');
-			}
+		if (strlen($bodyData['title']) > 255) {
+			throw new BadRequestException('Title is too long');
+		}
 
-			if (strlen($bodyData['text']) > 12000){
-				throw new BadRequestException('Text is too long');
-			}
+		if (!isset($bodyData['categoryName'])) {
+			throw new BadRequestException('The Category must be international, national or sport');
+		}
 
+		if(!in_array($bodyData['categoryName'], ['sport','international','national'])) {
+			throw new BadRequestException('The Category must be international, national or sport');
+		}
 
+		if(!isset($bodyData['text'])) {
+			throw new BadRequestException('Article must contain a text');
+		}
 
-		
-		if (isset($articleId)) {
+		if (strlen($bodyData['text']) > 12000){
+			throw new BadRequestException('Text is too long');
+		}
 
-			if($article === null) {
-				throw new PageNotFoundException('Article not found');
-			}
-			$article->setCategoryName($bodyData['categoryName']);
-			$article->setText($bodyData['text']);
-			$article->setTitle($bodyData['title']);
-			$this->articleDao->saveArticle($article);
-			$this->commit();
-			$this->sendJson($article);
-			
-		} else {
+		if ($article === null) {
 			$article = new Article();
-			$article->setTitle($bodyData['title']);
-			$article->setText($bodyData['text']);
-			$article->setCategoryName($bodyData['categoryName']);
-			$this->articleDao->saveArticle($article);
-			$this->commit();
-			$this->sendJson($article);
 		}
+
+		$article->setCategoryName($bodyData['categoryName']);
+		$article->setText($bodyData['text']);
+		$article->setTitle($bodyData['title']);
+
+		return $article;
 	}
 
-	function postDoArticle(Parambody $body) {
-		$this->badRequestExceptionIfs($body);
+	/**
+	 * @throws StatusException
+	 */
+	function postDoArticle(Parambody $body): void {
+		$this->beginTransaction();
+
+		$article = $this->badRequestExceptionIfs($body, null);
+		$this->articleDao->saveArticle($article);
+		$this->commit();
+
+		$this->sendJson($article);
+
 	}
 
-	function putDoArticle(Parambody $body, int $articleId) {
-		$this->badRequestExceptionIfs($body, $articleId);
+	/**
+	 * @throws PageNotFoundException
+	 * @throws StatusException
+	 */
+	function putDoArticle(int $articleId, Parambody $body): void {
+		$this->beginTransaction();
+		$article = $this->articleDao->getArticleById($articleId);
+		if ($article === null) {
+			throw new PageNotFoundException('Article not found');
+		}
+
+		$this->badRequestExceptionIfs($body, $article);
+		$this->commit();
+
+		$this->sendJson($article);
 	}
 
 	/**

+ 13 - 1
src-php/test/internship/controller/ArticleControllerTest.php

@@ -106,6 +106,9 @@ class ArticleControllerTest extends TestCase {
 
 	}
 
+	/**
+	 * @throws StatusException
+	 */
 	function testPostDoArticle() {
 		$response = TestEnv::http()->newRequest()
 				->post(['api', 'article'])
@@ -151,7 +154,10 @@ class ArticleControllerTest extends TestCase {
 		$this->assertEquals('text1', $articleStruct['text']);
 		$this->assertEquals('sport', $articleStruct['categoryName']);
 	}
-	
+
+	/**
+	 * @throws StatusException
+	 */
 	function testPutDoArticle1() {
 		$response = TestEnv::http()->newRequest()
 				->put(['api', 'article',$this->article1Id])
@@ -167,6 +173,9 @@ class ArticleControllerTest extends TestCase {
 
 	}
 
+	/**
+	 * @throws StatusException
+	 */
 	function testPutDoArticle2() {
 		$response = TestEnv::http()->newRequest()
 				->put(['api', 'article',$this->article1Id])
@@ -182,6 +191,9 @@ class ArticleControllerTest extends TestCase {
 
 	}
 
+	/**
+	 * @throws StatusException
+	 */
 	function testPutDoArticle3() {
 		$response = TestEnv::http()->newRequest()
 				->put(['api', 'article',$this->article1Id])

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio