Skip to content

Commit ed3fdb4

Browse files
committed
Provide failing test for readonly properties
1 parent 40a2038 commit ed3fdb4

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\ReadonlyProperties;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
use Doctrine\ORM\Mapping\GeneratedValue;
10+
use Doctrine\ORM\Mapping\Id;
11+
use Doctrine\ORM\Mapping\Table;
12+
13+
#[Entity, Table(name: 'author')]
14+
class Author
15+
{
16+
#[Column, Id, GeneratedValue]
17+
private readonly int $id;
18+
19+
#[Column]
20+
private readonly string $name;
21+
22+
public function getId(): int
23+
{
24+
return $this->id;
25+
}
26+
27+
public function getName(): string
28+
{
29+
return $this->name;
30+
}
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\ReadonlyProperties;
6+
7+
use Doctrine\Common\Collections\ArrayCollection;
8+
use Doctrine\Common\Collections\Collection;
9+
use Doctrine\ORM\Mapping\Column;
10+
use Doctrine\ORM\Mapping\Entity;
11+
use Doctrine\ORM\Mapping\GeneratedValue;
12+
use Doctrine\ORM\Mapping\Id;
13+
use Doctrine\ORM\Mapping\JoinTable;
14+
use Doctrine\ORM\Mapping\ManyToMany;
15+
use Doctrine\ORM\Mapping\Table;
16+
17+
#[Entity, Table(name: 'book')]
18+
class Book
19+
{
20+
#[Column, Id, GeneratedValue]
21+
private readonly int $id;
22+
23+
#[Column]
24+
private readonly string $title;
25+
26+
#[ManyToMany(targetEntity: Author::class), JoinTable(name: 'book_author')]
27+
private readonly Collection $authors;
28+
29+
public function __construct()
30+
{
31+
$this->authors = new ArrayCollection();
32+
}
33+
34+
public function getId(): int
35+
{
36+
return $this->id;
37+
}
38+
39+
public function getTitle(): string
40+
{
41+
return $this->title;
42+
}
43+
44+
/**
45+
* @return list<Author>
46+
*/
47+
public function getAuthors(): array
48+
{
49+
return $this->authors->getValues();
50+
}
51+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\ReadonlyProperties;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
use Doctrine\ORM\Mapping\GeneratedValue;
10+
use Doctrine\ORM\Mapping\Id;
11+
use Doctrine\ORM\Mapping\JoinColumn;
12+
use Doctrine\ORM\Mapping\ManyToOne;
13+
use Doctrine\ORM\Mapping\Table;
14+
15+
#[Entity, Table(name: 'simple_book')]
16+
class SimpleBook
17+
{
18+
#[Column, Id, GeneratedValue]
19+
private readonly int $id;
20+
21+
#[Column]
22+
private readonly string $title;
23+
24+
#[ManyToOne, JoinColumn(nullable: false)]
25+
private readonly Author $author;
26+
27+
public function getId(): int
28+
{
29+
return $this->id;
30+
}
31+
32+
public function getTitle(): string
33+
{
34+
return $this->title;
35+
}
36+
37+
public function getAuthor(): Author
38+
{
39+
return $this->author;
40+
}
41+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)