forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReadonlyPropertiesTest.php
112 lines (84 loc) · 3.62 KB
/
ReadonlyPropertiesTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Models\ReadonlyProperties\Author;
use Doctrine\Tests\Models\ReadonlyProperties\Book;
use Doctrine\Tests\Models\ReadonlyProperties\SimpleBook;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\TestUtil;
use function dirname;
/**
* @requires PHP 8.1
*/
class ReadonlyPropertiesTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
if (! isset(static::$sharedConn)) {
static::$sharedConn = TestUtil::getConnection();
}
$this->_em = $this->getEntityManager(null, new AttributeDriver(
[dirname(__DIR__, 2) . '/Models/ReadonlyProperties'],
true
));
$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());
}
}