|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Functional; |
| 6 | + |
| 7 | +use Doctrine\ORM\Mapping\Driver\AttributeDriver; |
| 8 | +use Doctrine\ORM\Tools\SchemaTool; |
| 9 | +use Doctrine\Tests\Models\ReadonlyProperties\Author; |
| 10 | +use Doctrine\Tests\Models\ReadonlyProperties\Book; |
| 11 | +use Doctrine\Tests\Models\ReadonlyProperties\SimpleBook; |
| 12 | +use Doctrine\Tests\OrmFunctionalTestCase; |
| 13 | +use Doctrine\Tests\TestUtil; |
| 14 | + |
| 15 | +use function dirname; |
| 16 | + |
| 17 | +/** |
| 18 | + * @requires PHP 8.1 |
| 19 | + */ |
| 20 | +class ReadonlyPropertiesTest extends OrmFunctionalTestCase |
| 21 | +{ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + if (! isset(static::$sharedConn)) { |
| 25 | + static::$sharedConn = TestUtil::getConnection(); |
| 26 | + } |
| 27 | + |
| 28 | + $this->_em = $this->getEntityManager(null, new AttributeDriver( |
| 29 | + [dirname(__DIR__, 2) . '/Models/ReadonlyProperties'] |
| 30 | + )); |
| 31 | + $this->_schemaTool = new SchemaTool($this->_em); |
| 32 | + |
| 33 | + parent::setUp(); |
| 34 | + |
| 35 | + $this->setUpEntitySchema([Author::class, Book::class, SimpleBook::class]); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSimpleEntity(): void |
| 39 | + { |
| 40 | + $connection = $this->_em->getConnection(); |
| 41 | + |
| 42 | + $connection->insert('author', ['name' => 'Jane Austen']); |
| 43 | + $authorId = $connection->lastInsertId(); |
| 44 | + |
| 45 | + $author = $this->_em->find(Author::class, $authorId); |
| 46 | + |
| 47 | + self::assertSame('Jane Austen', $author->getName()); |
| 48 | + self::assertEquals($authorId, $author->getId()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testEntityWithLazyManyToOne(): void |
| 52 | + { |
| 53 | + $connection = $this->_em->getConnection(); |
| 54 | + |
| 55 | + $connection->insert('author', ['name' => 'Jane Austen']); |
| 56 | + $authorId = $connection->lastInsertId(); |
| 57 | + |
| 58 | + $connection->insert('simple_book', ['title' => 'Pride and Prejudice', 'author_id' => $authorId]); |
| 59 | + $bookId = $connection->lastInsertId(); |
| 60 | + |
| 61 | + $book = $this->_em->find(SimpleBook::class, $bookId); |
| 62 | + |
| 63 | + self::assertSame('Pride and Prejudice', $book->getTitle()); |
| 64 | + self::assertEquals($bookId, $book->getId()); |
| 65 | + self::assertSame('Jane Austen', $book->getAuthor()->getName()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testEntityWithEagerManyToOne(): void |
| 69 | + { |
| 70 | + $connection = $this->_em->getConnection(); |
| 71 | + |
| 72 | + $connection->insert('author', ['name' => 'Jane Austen']); |
| 73 | + $authorId = $connection->lastInsertId(); |
| 74 | + |
| 75 | + $connection->insert('simple_book', ['title' => 'Pride and Prejudice', 'author_id' => $authorId]); |
| 76 | + $bookId = $connection->lastInsertId(); |
| 77 | + |
| 78 | + [$book] = $this->_em->createQueryBuilder() |
| 79 | + ->from(SimpleBook::class, 'b') |
| 80 | + ->join('b.author', 'a') |
| 81 | + ->select(['b', 'a']) |
| 82 | + ->where('b.id = :id') |
| 83 | + ->setParameter('id', $bookId) |
| 84 | + ->getQuery() |
| 85 | + ->execute(); |
| 86 | + |
| 87 | + self::assertInstanceOf(SimpleBook::class, $book); |
| 88 | + self::assertSame('Pride and Prejudice', $book->getTitle()); |
| 89 | + self::assertEquals($bookId, $book->getId()); |
| 90 | + self::assertSame('Jane Austen', $book->getAuthor()->getName()); |
| 91 | + } |
| 92 | + |
| 93 | + public function testEntityWithManyToMany(): void |
| 94 | + { |
| 95 | + $connection = $this->_em->getConnection(); |
| 96 | + |
| 97 | + $connection->insert('author', ['name' => 'Jane Austen']); |
| 98 | + $authorId = $connection->lastInsertId(); |
| 99 | + |
| 100 | + $connection->insert('book', ['title' => 'Pride and Prejudice']); |
| 101 | + $bookId = $connection->lastInsertId(); |
| 102 | + |
| 103 | + $connection->insert('book_author', ['book_id' => $bookId, 'author_id' => $authorId]); |
| 104 | + |
| 105 | + $book = $this->_em->find(Book::class, $bookId); |
| 106 | + |
| 107 | + self::assertSame('Pride and Prejudice', $book->getTitle()); |
| 108 | + self::assertEquals($bookId, $book->getId()); |
| 109 | + self::assertSame('Jane Austen', $book->getAuthors()[0]->getName()); |
| 110 | + } |
| 111 | +} |
0 commit comments