Bladeren bron

Tests fertig implementiert & Test Controller erstellt

markus 1 maand geleden
bovenliggende
commit
e5e421f74c

+ 2 - 1
composer.json

@@ -26,7 +26,8 @@
     },
     "autoload-dev" : {
         "psr-4" : {
-            "internship\\" : "src-php/test/internship"
+            "internship\\" : "src-php/test/internship",
+            "util\\" : "src-php/test/util"
         }
     },
     "config": {

+ 10 - 26
composer.lock

@@ -2238,25 +2238,25 @@
         },
         {
             "name": "phpunit/phpunit",
-            "version": "9.6.34",
+            "version": "9.6.35",
             "source": {
                 "type": "git",
                 "url": "https://github.com/sebastianbergmann/phpunit.git",
-                "reference": "b36f02317466907a230d3aa1d34467041271ef4a"
+                "reference": "0edba2f3a0c48df3553cb9b640810b30df60302b"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b36f02317466907a230d3aa1d34467041271ef4a",
-                "reference": "b36f02317466907a230d3aa1d34467041271ef4a",
+                "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/0edba2f3a0c48df3553cb9b640810b30df60302b",
+                "reference": "0edba2f3a0c48df3553cb9b640810b30df60302b",
                 "shasum": ""
             },
             "require": {
                 "doctrine/instantiator": "^1.5.0 || ^2",
                 "ext-dom": "*",
+                "ext-filter": "*",
                 "ext-json": "*",
                 "ext-libxml": "*",
                 "ext-mbstring": "*",
-                "ext-xml": "*",
                 "ext-xmlwriter": "*",
                 "myclabs/deep-copy": "^1.13.4",
                 "phar-io/manifest": "^2.0.4",
@@ -2321,31 +2321,15 @@
             "support": {
                 "issues": "https://github.com/sebastianbergmann/phpunit/issues",
                 "security": "https://github.com/sebastianbergmann/phpunit/security/policy",
-                "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.34"
+                "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.35"
             },
             "funding": [
                 {
-                    "url": "https://phpunit.de/sponsors.html",
-                    "type": "custom"
-                },
-                {
-                    "url": "https://github.com/sebastianbergmann",
-                    "type": "github"
-                },
-                {
-                    "url": "https://liberapay.com/sebastianbergmann",
-                    "type": "liberapay"
-                },
-                {
-                    "url": "https://thanks.dev/u/gh/sebastianbergmann",
-                    "type": "thanks_dev"
-                },
-                {
-                    "url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit",
-                    "type": "tidelift"
+                    "url": "https://phpunit.de/sponsoring.html",
+                    "type": "other"
                 }
             ],
-            "time": "2026-01-27T05:45:00+00:00"
+            "time": "2026-07-06T14:48:07+00:00"
         },
         {
             "name": "sebastian/cli-parser",
@@ -3416,5 +3400,5 @@
     "prefer-lowest": false,
     "platform": {},
     "platform-dev": {},
-    "plugin-api-version": "2.6.0"
+    "plugin-api-version": "2.9.0"
 }

+ 7 - 4
src-php/app/internship/controller/ArticleApiController.php

@@ -94,7 +94,7 @@ class ArticleApiController extends ControllerAdapter {
 	 *    <li>
 	 *        Mache eine neue Entity {@see Article} und befülle sie mit den übergebenen Daten.
 	 *    </li>
-	 *  <li>
+	 *    <li>
 	 *        Implementiere eine neue Methode im {@see ArticleDao} und bennene sie "saveArticle".
 	 *     </li>
 	 * </ul>
@@ -105,7 +105,6 @@ class ArticleApiController extends ControllerAdapter {
 	 * @throws StatusException
 	 */
 	function postDoArticle(ParamBody $body): void {
-
         $httpData = $body->parseJsonToHttpData();
         $title = $httpData->reqString('title');
         $text = $httpData->reqString('text');
@@ -115,11 +114,13 @@ class ArticleApiController extends ControllerAdapter {
 			throw new BadRequestException();
         }
 
-        $articleToAdd = $this->prepareArticle($title, $text, $categoryName);
+        $articleToAdd = $this->createArticle($title, $text, $categoryName);
 
 		$this->beginTransaction();
 		$this->articleDao->saveArticle($articleToAdd);
 		$this->commit();
+
+		$this->sendJson($articleToAdd);
 	}
 
 
@@ -165,6 +166,8 @@ class ArticleApiController extends ControllerAdapter {
         $this->beginTransaction();
         $this->articleDao->saveArticle($article);
         $this->commit();
+
+		$this->sendJson($article);
 	}
 
 	/**
@@ -195,7 +198,7 @@ class ArticleApiController extends ControllerAdapter {
      * Stellt aus den Input Parametern ein Article Objekt zur Verfügung.
      * @return Article
      */
-	function prepareArticle($title, $text, $categoryName): Article {
+	function createArticle($title, $text, $categoryName): Article {
 		$article = new Article();
 		$article->setCategoryName($categoryName);
 		$article->setTitle($title);

+ 46 - 0
src-php/app/internship/controller/UserApiController.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace internship\controller;
+
+use n2n\web\http\controller\ControllerAdapter;
+use n2n\reflection\annotation\AnnoInit;
+use n2n\web\http\annotation\AnnoPath;
+use n2n\web\http\annotation\AnnoExt;
+
+class UserApiController extends ControllerAdapter {
+	private static function _annos(AnnoInit $ai): void {
+		//$ai->m('doDetail', new AnnoExt('txt'));
+		$ai->m('modificationsInt', new AnnoPath('modInt?/paramInts+:#^[0-9]+$#'));
+		$ai->m('modificationsString', new AnnoPath('modString?/params+:#^[a-z]+$#'));
+	}
+
+	/*
+	 * http://localhost/php-storm/internship-playground/src-php/public/user
+	 */
+	public function index() {
+		echo 'Hallo Welt!';
+	}
+
+	/*
+	 * http://localhost/php-storm/internship-playground/src-php/public/user/detail
+	 * http://localhost/php-storm/internship-playground/src-php/public/user/detail/Test
+	 * http://localhost/php-storm/internship-playground/src-php/public/user/detail/a/b/c
+	 */
+	public function doDetail(?string $text1 = null, ?string $text2 = null, ?string $text3 = null) {
+		echo "Hallo Welt! Detail Function " . $text1 . " " . $text2 . " " . $text3;
+	}
+
+	/*
+	 * http://localhost/php-storm/internship-playground/src-php/public/user/modInt/1/2/3
+	 */
+	public function modificationsInt(array $paramInts) {
+		echo 'params mod ints: ' . implode(', ', $paramInts);
+	}
+
+	/*
+	 * http://localhost/php-storm/internship-playground/src-php/public/user/modString/a/b/c
+	 */
+	public function modificationsString(array $params) {
+		echo 'params: ' . implode(', ', $params);
+	}
+}

+ 122 - 37
src-php/test/internship/controller/ArticleControllerTest.php

@@ -7,6 +7,9 @@ use n2n\test\TestEnv;
 use internship\test\ArticleTestEnv;
 use util\GeneralTestEnv;
 use n2n\web\http\StatusException;
+use n2n\web\http\PageNotFoundException;
+use internship\bo\Article;
+use n2n\web\http\BadRequestException;
 
 
 class ArticleControllerTest extends TestCase {
@@ -19,9 +22,11 @@ class ArticleControllerTest extends TestCase {
 		GeneralTestEnv::tearDown();
 
 		$tx = TestEnv::createTransaction();
-		$article1 = ArticleTestEnv::setUpArticle('Title 3', 'Loren ipsum 1', 'teaser');
-		$article2 = ArticleTestEnv::setUpArticle('Title 2', 'Loren ipsum 2', 'news');
-		$article3 = ArticleTestEnv::setUpArticle('Title 1', 'Loren ipsum 3', 'news');
+		$article1 = ArticleTestEnv::setUpArticle('Title 3', 'Lorem ipsum 3', 'teaser');
+		$article2 = ArticleTestEnv::setUpArticle('Title 2', categoryName: 'news');
+		$article3 = ArticleTestEnv::setUpArticle('Title 1', categoryName: 'news');
+		//$article4 = ArticleTestEnv::setUpArticle('Title 4');
+		//var_dump($article4);
 		$tx->commit();
 
 		$this->article1Id = $article1->getId();
@@ -45,63 +50,143 @@ class ArticleControllerTest extends TestCase {
 		$this->assertEquals('Title 3', $articleStructs[2]['title']);
 	}
 
-    function testGetArticleById() {
+	/**
+	 * @throws StatusException
+	 */
+	function testGetDoArticlesByCategoryName() {
+		$response = TestEnv::http()->newRequest()
+				->get(['api', 'articles', 'news'])
+				->exec();
 
-        // $articlePOST = ArticleTestEnv::getArticleById($this->article3Id);
+	   $articleStructs = $response->parseJson();
+	   $this->assertCount(2, $articleStructs);
+    }
 
+	/**
+	 * @throws StatusException
+	 */
+	function testGetArticle() {
         $response = TestEnv::http()->newRequest()
-                    ->get(['api', 'articles'])
+                    ->get(['api', 'article', $this->article3Id])
                     ->exec();
 
-        $articleStructs = $response->parseJson();
+		$articleStructs = $response->parseJson();
 
-        $this->assertEquals('Title 3', $articleStructs[2]['title']);
+        $this->assertEquals('Title 1', $articleStructs['title']);
     }
 
-    function testPostDoArticle() {
+	/**
+	 * @throws StatusException
+	 */
+	function testGetArticleNotFound() {
+		$this->expectException(PageNotFoundException::class);
 
-        $tx = TestEnv::createTransaction();
-        $articlePOST = ArticleTestEnv::setUpArticle('Title POST', 'Loren ipsum POST', 'teaser POST');
-        $tx->commit();
+		$response = TestEnv::http()->newRequest()
+				->get(['api', 'article', 1000])
+				->exec();
+	}
 
-        $this->articlePOSTId = $articlePOST->getId();
+    /**
+     * @throws StatusException
+     */
+    function testPostDoArticle() {
+        $tx = TestEnv::createTransaction(true);
+        $this->assertSame(3, TestEnv::temUtil()->count(Article::class));
+		$tx->commit();
 
         $response = TestEnv::http()->newRequest()
-            ->get(['api', 'articles'])
-            ->exec();
+				->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();
 
-        $articleStructs = $response->parseJson();
-        $this->assertCount(4, $articleStructs);
     }
 
-    function testPutDoArticle() {
+	/**
+	 * @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();
 
-        $tx = TestEnv::createTransaction();
-        ArticleTestEnv::updateArticle(1, 'Title PUT', 'Loren ipsum PUT', 'teaser PUT');
-        $tx->commit();
+		$response = TestEnv::http()->newRequest()
+				->put(['api', 'article', $this->article1Id])
+				->bodyJson([
+						'title' => 'Title PUT',
+						'text' => 'Text PUT',
+						'categoryName' => 'sport',
+				])
+				->exec();
 
-        $response = TestEnv::http()->newRequest()
-            ->get(['api', 'articles'])
-            ->exec();
+		$articleStruct = $response->parseJson();
+		$this->assertEquals('Title PUT', $articleStruct['title']);
 
-        $articleStructs = $response->parseJson();
-        $this->assertCount(3, $articleStructs);
-        $this->assertEquals('Title PUT', $articleStructs[2]['title']);
-        //var_dump($articleStructs);
-    }
+		$tx = TestEnv::createTransaction(true);
+		$this->assertSame(3, TestEnv::temUtil()->count(Article::class));
 
-    function testDeleteDoArticle() {
+		$article = TestEnv::tem()->find(Article::class, $this->article1Id);
+		$this->assertSame('Title PUT', $article->getTitle());
 
-        $tx = TestEnv::createTransaction();
-        ArticleTestEnv::removeArticle(3);
-        $tx->commit();
+		$this->assertSame('Text PUT', $article->getText());
 
-        $response = TestEnv::http()->newRequest()
-            ->get(['api', 'articles'])
+		$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();
 
-        $articleStructs = $response->parseJson();
-        $this->assertCount(2, $articleStructs);
+		$tx = TestEnv::createTransaction(true);
+        $this->assertSame(2, TestEnv::temUtil()->count(Article::class));
+		$tx->commit();
     }
 
 }

+ 10 - 6
src-php/test/internship/test/ArticleTestEnv.php

@@ -3,14 +3,18 @@ namespace internship\test;
 
 use n2n\test\TestEnv;
 use internship\bo\Article;
+use n2n\util\HashUtils;
 
 class ArticleTestEnv  {
 
-    static function setUpArticle(string $title, string $text, string $categoryName): Article {
-        $article = new Article();
-        $article->setTitle($title);
-        $article->setText($text);
-        $article->setCategoryName($categoryName);
+    static function setUpArticle(string $title, ?string $text = null, ?string $categoryName = null): Article {
+
+		$uniqueID = HashUtils::base36Uniqid(false);
+
+		$article = new Article();
+		$article->setTitle($title);
+		$article->setText($text ?? 'Text: ' . $uniqueID);
+		$article->setCategoryName($categoryName ?? 'international');
 
         TestEnv::em()->persist($article);
         TestEnv::em()->flush();
@@ -45,6 +49,6 @@ class ArticleTestEnv  {
         TestEnv::em()->flush();
         //TestEnv::em()->commit();
 
-        // return $article;
+        return $article;
     }
  }

+ 1 - 1
src-php/test/util/GeneralTestEnv.php

@@ -6,7 +6,7 @@ use n2n\persistence\PdoStatementException;
 
 class GeneralTestEnv  {
 	
-	static function teardown() {
+	static function teardown(): void {
 		if (TestEnv::container()->tm()->hasOpenTransaction()) {
 			TestEnv::container()->tm()->getRootTransaction()->rollBack();
 		}

+ 1 - 0
src-php/var/etc/internship/app.ini

@@ -1,5 +1,6 @@
 [routing]
 controllers[/] = "internship\controller\IsRootController"
+controllers[/user] = "internship\\controller\\UserApiController"
 
 [orm]
 entities[] = "internship\bo\Article"