Преглед на файлове

added tests for Comment.php

markus преди 2 месеца
родител
ревизия
bb4803f306

+ 3 - 1
src-php/app/internship/bo/Article.php

@@ -6,6 +6,8 @@ use n2n\persistence\orm\attribute\ManyToMany;
 use n2n\persistence\orm\attribute\OneToMany;
 use n2n\persistence\orm\CascadeType;
 use n2n\persistence\orm\attribute\OrderBy;
+use n2n\persistence\orm\attribute\JoinColumn;
+use n2n\persistence\orm\FetchType;
 
 class Article extends ObjectAdapter implements \JsonSerializable {
 	public int $id;
@@ -20,7 +22,7 @@ class Article extends ObjectAdapter implements \JsonSerializable {
 	#[ManyToMany(Category::class, 'articles', cascade: CascadeType::PERSIST)]
 	public \ArrayObject $categories;
 
-	#[OneToMany(Comment::class, 'article', cascade: CascadeType::PERSIST, orphanRemoval: true)]
+	#[OneToMany(Comment::class, 'article', cascade: CascadeType::PERSIST, fetch: FetchType::LAZY , orphanRemoval: true)]
 	public \ArrayObject $comments;
 
 	function __construct() {

+ 2 - 1
src-php/app/internship/bo/Comment.php

@@ -4,6 +4,7 @@ namespace internship\bo;
 
 use n2n\persistence\orm\attribute\ManyToOne;
 use n2n\reflection\ObjectAdapter;
+use n2n\persistence\orm\FetchType;
 
 class Comment extends ObjectAdapter implements \JsonSerializable {
 
@@ -13,7 +14,7 @@ class Comment extends ObjectAdapter implements \JsonSerializable {
 
 	public string $text;
 
-	#[ManyToOne(Article::class)]
+	#[ManyToOne(Article::class, fetch: FetchType::LAZY)]
 	public ?Article $article = null;
 
 //	function __construct(Article $article) {

+ 16 - 0
src-php/app/internship/bo/MainNews.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace internship\bo;
+
+use n2n\persistence\orm\attribute\DiscriminatorValue;
+
+#[DiscriminatorValue('mainNews')]
+class MainNews extends News {
+	public string $intro;
+
+	public function jsonSerialize(): mixed {
+		$parentJSON = parent::jsonSerialize();
+		$parentJSON['intro'] = $this->intro;
+		return $parentJSON;
+	}
+}

+ 25 - 11
src-php/app/internship/bo/News.php

@@ -4,6 +4,8 @@ namespace internship\bo;
 
 use n2n\reflection\ObjectAdapter;
 use n2n\persistence\orm\attribute\Table;
+use nql\bo\Member;
+use n2n\io\managed\FileManager;
 use n2n\persistence\orm\attribute\Inheritance;
 use n2n\persistence\orm\InheritanceType;
 use n2n\persistence\orm\attribute\DiscriminatorValue;
@@ -12,12 +14,10 @@ use n2n\persistence\orm\attribute\EntityListeners;
 use n2n\persistence\orm\attribute\AttributeOverrides;
 use n2n\persistence\orm\attribute\MappedSuperclass;
 use n2n\persistence\orm\attribute\ManagedFile;
-use nql\bo\Member;
 use n2n\persistence\orm\attribute\ManyToOne;
 use n2n\persistence\orm\attribute\Embedded;
 use n2n\persistence\orm\attribute\Column;
 use n2n\persistence\orm\attribute\Id;
-use n2n\io\managed\FileManager;
 use n2n\persistence\orm\attribute\AssociationOverrides;
 use n2n\persistence\orm\attribute\JoinColumn;
 use n2n\persistence\orm\CascadeType;
@@ -26,8 +26,10 @@ use n2n\persistence\orm\attribute\JoinTable;
 use n2n\persistence\orm\attribute\OrderBy;
 use n2n\persistence\orm\attribute\OneToOne;
 use n2n\persistence\orm\attribute\ManyToMany;
+use internship\listeners\LastModListener;
+use n2n\persistence\orm\attribute\File;
+use n2n\web\http\controller\impl\mock\StringValueObjectMock;
 
-#[Table('newsarticle')]
 /* #[Inheritance(InheritanceType::TABLE_PER_CLASS)] */
 /* #[DiscriminatorValue('textItem')] */
 /* #[DiscriminatorColumn('type')] */
@@ -43,26 +45,38 @@ use n2n\persistence\orm\attribute\ManyToMany;
 /*	#[ManyToMany('articles', Article::class)] */
 /*	#[OneToMany('assignement.assignementGroup', Member::class)]*/
 /*  #[ManyToOne(targetEntity: BlogArticle::class, cascade: null, cascadeType: CascadeType::ALL, fetch: FetchType::EAGER)] */
+
+#[Table('newsarticle')]
+#[AttributeOverrides(
+	['text_finished' => 'text',
+	 'description_finished' => 'description']
+)]
+#[EntityListeners(LastModListener::class)]
+#[Inheritance(InheritanceType::SINGLE_TABLE)]
+#[DiscriminatorColumn('type')]
+#[DiscriminatorValue('news')]
 class News extends ObjectAdapter implements \JsonSerializable {
 	public int $id;
 	/*  #[OrderBy(array('name' => 'ASC'))]
 		#[Embedded(Address::class)]
-		#[AttributeOverrides(['forename' => 'bill_forename',
-		'surname' => 'bill_surname'])]
 		#[Column('firstname')]
 	*/
 	/* #[ManagedFile(FileManager::TYPE_PUBLIC)] */
 	/* #[AssociationOverrides(array( 'assignementGroup' => new JoinColumn('group_id')))] */
 	/* #[JoinColumn('article_id')] */
-	public string $title;
 
-	public string $description;
+	public \DateTime $lastMod;
+
+	#[Column('title')]
+	public string $title2;
+
+	public string $description_finished;
 
-	public string $text;
+	public string $text_finished;
 
 	public function jsonSerialize(): mixed {
-		return ['title' => $this->title,
-				'description' => $this->description,
-				'text' => $this->text ];
+		return ['title' => $this->title2,
+				'description' => $this->description_finished,
+				'text' => $this->text_finished];
 	}
 }

+ 16 - 0
src-php/app/internship/bo/TeaserNews.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace internship\bo;
+
+use n2n\persistence\orm\attribute\DiscriminatorValue;
+
+#[DiscriminatorValue('teaserNews')]
+class TeaserNews extends News {
+	public string $intro;
+
+	public function jsonSerialize(): mixed {
+		$parentJSON = parent::jsonSerialize();
+		$parentJSON['intro'] = $this->intro;
+		return $parentJSON;
+	}
+}

+ 4 - 4
src-php/app/internship/controller/NewsApiController.php

@@ -51,11 +51,11 @@ class NewsApiController extends ControllerAdapter {
 		$this->sendJson($newsToAdd);
 	}
 
-	function createNews($title, $description, $text): News {
+	function createNews($title, $description, $text, $intro): News {
 		$news = new News();
-		$news->title = $title;
-		$news->description = $description;
-		$news->text = $text;
+		$news->title2 = $title;
+		$news->description_finished = $description;
+		$news->text_finished = $text;
 		return $news;
 	}
 }

+ 21 - 0
src-php/app/internship/listeners/LastModListener.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace internship\listeners;
+
+use n2n\reflection\ObjectAdapter;
+use internship\bo\News;
+use internship\bo\Article;
+use n2n\context\attribute\Lookupable;
+
+#[Lookupable]
+class LastModListener extends ObjectAdapter {
+
+	private function _prePersist(News|Article $news) {
+		$news->lastMod = new \DateTime();
+	}
+
+	private function _preUpdate(News $news) {
+		$news->lastMod = new \DateTime();
+	}
+
+}

+ 3 - 0
src-php/app/internship/model/NewsDao.php

@@ -8,6 +8,7 @@ use internship\bo\Comment;
 use n2n\context\attribute\Inject;
 use internship\bo\News;
 use internship\bo\Article;
+use internship\bo\MainNews;
 
 #[RequestScoped]
 class NewsDao {
@@ -18,6 +19,8 @@ class NewsDao {
 	 * Gebe alle {@see Comment}-Objekte, nach id absteigend sortiert, zurück.
 	 *
 	 * @return Comment[]
+	 *
+	 * http://localhost/php-storm/internship-playground/src-php/public/api/news/news
 	 */
 	function getNews(): array {
 		$criteria = $this->em->createSimpleCriteria(News::getClass(), array(), array('id' => 'DESC'));

+ 2 - 0
src-php/test/internship/controller/ArticleControllerTest.php

@@ -95,6 +95,7 @@ class ArticleControllerTest extends TestCase {
 
     /**
      * @throws StatusException
+	 * This test {@link ArticleApiController::postDoArticle()}
      */
     function testPostDoArticle() {
         $tx = TestEnv::createTransaction(true);
@@ -130,6 +131,7 @@ class ArticleControllerTest extends TestCase {
 		$tx = TestEnv::createTransaction(true);
 		$this->assertSame(3, TestEnv::temUtil()->count(Article::class));
 		$article = TestEnv::tem()->find(Article::class, $this->article1Id);
+		$article = ArticleTestEnv::findArticle($this->article1Id);
 		$this->assertSame('Title 3', $article->title);
 		$this->assertSame('Lorem ipsum 3', $article->text);
 		$tx->commit();

+ 2 - 3
src-php/test/internship/test/ArticleTestEnv.php

@@ -22,9 +22,8 @@ class ArticleTestEnv  {
         return $article;
     }
 
-    static function getArticleById($articleId) {
-        $article = TestEnv::em()->find(Article::getClass(), $articleId);
-        return $article;
+    static function findArticle(int $articleId) {
+		return TestEnv::em()->find(Article::getClass(), $articleId);
     }
 
     static function updateArticle($articleId, string $title, string $text, string $categoryName): Article {

+ 85 - 0
src-php/test/internship/test/CommentEnvTest.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace internship\test;
+
+use internship\bo\Article;
+use n2n\test\TestEnv;
+use internship\bo\Comment;
+use PHPUnit\Framework\TestCase;
+use util\GeneralTestEnv;
+use n2n\web\http\StatusException;
+use n2n\web\http\PageNotFoundException;
+use InvalidArgumentException;
+
+class CommentEnvTest extends TestCase {
+	private int $article1Id;
+
+	function setUp(): void {
+		GeneralTestEnv::tearDown();
+
+		$tx = TestEnv::createTransaction();
+		$article1 = ArticleTestEnv::setUpArticle('Title 1', 'Lorem ipsum 1', 'sport');
+		$tx->commit();
+
+		$this->article1Id = $article1->id;
+	}
+
+	/**
+	 * @throws StatusException
+	 */
+	function testComments() {
+		$tx = TestEnv::createTransaction(true);
+		$this->assertEquals(0, TestEnv::temUtil()->count(Comment::class));
+		$tx->commit();
+
+		$tx = TestEnv::createTransaction(false);
+		$comment = CommentTestEnv::setUpComment($this->article1Id);
+		$tx->commit();
+		$id = $comment->id;
+
+		$tx = TestEnv::createTransaction(true);
+		$this->assertEquals(1, TestEnv::temUtil()->count(Comment::class));
+		$comment1 = CommentTestEnv::findComment($id);
+		$this->assertNotNull($comment1->author);
+		$this->assertStringContainsString('Testerich ', $comment1->author);
+		$this->assertNotNull($comment1->text);
+		$this->assertStringContainsString('Text ', $comment1->text);
+		$tx->commit();
+
+		$tx = TestEnv::createTransaction(false);
+		$article = ArticleTestEnv::findArticle($this->article1Id);
+		CommentTestEnv::setUpComment($article);
+		$tx->commit();
+
+
+		$tx = TestEnv::createTransaction(true);
+		$this->assertEquals(2, TestEnv::temUtil()->count(Comment::class));
+		$tx->commit();
+	}
+
+	/**
+	 * @throws StatusException
+	 */
+	function testFindComment() {
+		$tx = TestEnv::createTransaction(false);
+		$comment1 = CommentTestEnv::setUpComment($this->article1Id);
+		$comment2 = CommentTestEnv::setUpComment($this->article1Id, 'Markus Rutz', 'My Text');
+		$comment3 = CommentTestEnv::setUpComment($this->article1Id);
+		$tx->commit();
+		$id = $comment2->id;
+		$tx = TestEnv::createTransaction(true);
+		$comment2 = CommentTestEnv::findComment($id);
+		$this->assertEquals('Markus Rutz', $comment2->author);
+		$this->assertEquals('My Text', $comment2->text);
+		$tx->commit();
+	}
+
+	function testSetupCommentExpectExceptionBecauseArticleIdNotFound() {
+		$this->expectException(InvalidArgumentException::class);
+		$this->expectExceptionMessage('Could not find Article with id 9999');
+		$tx = TestEnv::createTransaction(true);
+		CommentTestEnv::setUpComment(9999);
+		$tx->commit();
+	}
+
+}

+ 16 - 4
src-php/test/internship/test/CommentTestEnv.php

@@ -6,18 +6,26 @@ use internship\bo\Article;
 use n2n\util\HashUtils;
 use n2n\test\TestEnv;
 use internship\bo\Comment;
+use n2n\util\type\ArgUtils;
 
 class CommentTestEnv {
-	static function setUpComment(Article $article, string $author, ?string $text = null): Comment {
+	static function setUpComment(Article|int $article, ?string $author = null, ?string $text = null): Comment {
 
-		// $uniqueID = HashUtils::base36Uniqid(false);
+		$uniqueID = HashUtils::base36Uniqid(false);
 
 		//$article1 = ArticleTestEnv::setUpArticle('Title 1', 'Lorem ipsum 1', 'sport');
 
+		// For entity arguments it must always be possible to also pass the just id of the entity.
+		if (is_int($article)) {
+			$articleId = $article;
+			$article = ArticleTestEnv::findArticle($article);
+			ArgUtils::assertTrue($article !== null, 'Could not find Article with id ' . $articleId);
+		}
+
 		$comment = new Comment();
 		$comment->article = $article;
-		$comment->author = $author;
-		$comment->text = $text;
+		$comment->author = $author ?? 'Testerich ' . $uniqueID;
+		$comment->text = $text ?? 'Text ' . $uniqueID;
 
 		TestEnv::em()->persist($article);
 		TestEnv::em()->persist($comment);
@@ -25,4 +33,8 @@ class CommentTestEnv {
 
 		return $comment;
 	}
+
+	static function findComment(int $commentId): ?Comment {
+		return TestEnv::em()->find(Comment::getClass(), $commentId);
+	}
 }

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

@@ -9,4 +9,6 @@ controllers[/api/news] = "internship\\controller\\NewsApiController"
 entities[] = "internship\bo\Article"
 entities[] = "internship\bo\Category"
 entities[] = "internship\bo\Comment"
-entities[] = "internship\bo\News"
+entities[] = "internship\bo\News"
+entities[] = "internship\bo\MainNews"
+entities[] = "internship\bo\TeaserNews"