From 20d0298e0c02c8b54760253370b629418046f273 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 1 Jan 2022 23:45:05 +0100 Subject: [PATCH] Provide failing test for readonly properties --- .../Models/ReadonlyProperties/Author.php | 31 +++++ .../Tests/Models/ReadonlyProperties/Book.php | 51 ++++++++ .../Models/ReadonlyProperties/SimpleBook.php | 41 +++++++ .../ORM/Functional/ReadonlyPropertiesTest.php | 111 ++++++++++++++++++ 4 files changed, 234 insertions(+) create mode 100644 tests/Doctrine/Tests/Models/ReadonlyProperties/Author.php create mode 100644 tests/Doctrine/Tests/Models/ReadonlyProperties/Book.php create mode 100644 tests/Doctrine/Tests/Models/ReadonlyProperties/SimpleBook.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/ReadonlyPropertiesTest.php diff --git a/tests/Doctrine/Tests/Models/ReadonlyProperties/Author.php b/tests/Doctrine/Tests/Models/ReadonlyProperties/Author.php new file mode 100644 index 00000000000..901b9ed3005 --- /dev/null +++ b/tests/Doctrine/Tests/Models/ReadonlyProperties/Author.php @@ -0,0 +1,31 @@ +id; + } + + public function getName(): string + { + return $this->name; + } +} diff --git a/tests/Doctrine/Tests/Models/ReadonlyProperties/Book.php b/tests/Doctrine/Tests/Models/ReadonlyProperties/Book.php new file mode 100644 index 00000000000..31e1b5b6be6 --- /dev/null +++ b/tests/Doctrine/Tests/Models/ReadonlyProperties/Book.php @@ -0,0 +1,51 @@ +authors = new ArrayCollection(); + } + + public function getId(): int + { + return $this->id; + } + + public function getTitle(): string + { + return $this->title; + } + + /** + * @return list + */ + public function getAuthors(): array + { + return $this->authors->getValues(); + } +} diff --git a/tests/Doctrine/Tests/Models/ReadonlyProperties/SimpleBook.php b/tests/Doctrine/Tests/Models/ReadonlyProperties/SimpleBook.php new file mode 100644 index 00000000000..d978d045839 --- /dev/null +++ b/tests/Doctrine/Tests/Models/ReadonlyProperties/SimpleBook.php @@ -0,0 +1,41 @@ +id; + } + + public function getTitle(): string + { + return $this->title; + } + + public function getAuthor(): Author + { + return $this->author; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/ReadonlyPropertiesTest.php b/tests/Doctrine/Tests/ORM/Functional/ReadonlyPropertiesTest.php new file mode 100644 index 00000000000..17aa2306df6 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/ReadonlyPropertiesTest.php @@ -0,0 +1,111 @@ +_em = $this->getEntityManager(null, new AttributeDriver( + [dirname(__DIR__, 2) . '/Models/ReadonlyProperties'] + )); + $this->_schemaTool = new SchemaTool($this->_em); + + parent::setUp(); + + $this->setUpEntitySchema([Author::class, Book::class, SimpleBook::class]); + } + + public function testSimpleEntity(): void + { + $connection = $this->_em->getConnection(); + + $connection->insert('author', ['name' => 'Jane Austen']); + $authorId = $connection->lastInsertId(); + + $author = $this->_em->find(Author::class, $authorId); + + self::assertSame('Jane Austen', $author->getName()); + self::assertEquals($authorId, $author->getId()); + } + + public function testEntityWithLazyManyToOne(): void + { + $connection = $this->_em->getConnection(); + + $connection->insert('author', ['name' => 'Jane Austen']); + $authorId = $connection->lastInsertId(); + + $connection->insert('simple_book', ['title' => 'Pride and Prejudice', 'author_id' => $authorId]); + $bookId = $connection->lastInsertId(); + + $book = $this->_em->find(SimpleBook::class, $bookId); + + self::assertSame('Pride and Prejudice', $book->getTitle()); + self::assertEquals($bookId, $book->getId()); + self::assertSame('Jane Austen', $book->getAuthor()->getName()); + } + + public function testEntityWithEagerManyToOne(): void + { + $connection = $this->_em->getConnection(); + + $connection->insert('author', ['name' => 'Jane Austen']); + $authorId = $connection->lastInsertId(); + + $connection->insert('simple_book', ['title' => 'Pride and Prejudice', 'author_id' => $authorId]); + $bookId = $connection->lastInsertId(); + + [$book] = $this->_em->createQueryBuilder() + ->from(SimpleBook::class, 'b') + ->join('b.author', 'a') + ->select(['b', 'a']) + ->where('b.id = :id') + ->setParameter('id', $bookId) + ->getQuery() + ->execute(); + + self::assertInstanceOf(SimpleBook::class, $book); + self::assertSame('Pride and Prejudice', $book->getTitle()); + self::assertEquals($bookId, $book->getId()); + self::assertSame('Jane Austen', $book->getAuthor()->getName()); + } + + public function testEntityWithManyToMany(): void + { + $connection = $this->_em->getConnection(); + + $connection->insert('author', ['name' => 'Jane Austen']); + $authorId = $connection->lastInsertId(); + + $connection->insert('book', ['title' => 'Pride and Prejudice']); + $bookId = $connection->lastInsertId(); + + $connection->insert('book_author', ['book_id' => $bookId, 'author_id' => $authorId]); + + $book = $this->_em->find(Book::class, $bookId); + + self::assertSame('Pride and Prejudice', $book->getTitle()); + self::assertEquals($bookId, $book->getId()); + self::assertSame('Jane Austen', $book->getAuthors()[0]->getName()); + } +}