-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Support readonly properties for read operations #9316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
derrabus
merged 2 commits into
doctrine:2.11.x
from
derrabus:improvement/readonly-properties
Jan 9, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Mapping; | ||
|
||
use InvalidArgumentException; | ||
use LogicException; | ||
use ReflectionProperty; | ||
|
||
use function assert; | ||
use function func_get_args; | ||
use function func_num_args; | ||
use function is_object; | ||
use function sprintf; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ReflectionReadonlyProperty extends ReflectionProperty | ||
{ | ||
public function __construct( | ||
private ReflectionProperty $wrappedProperty | ||
) { | ||
if (! $wrappedProperty->isReadOnly()) { | ||
throw new InvalidArgumentException('Given property is not readonly.'); | ||
} | ||
|
||
parent::__construct($wrappedProperty->class, $wrappedProperty->name); | ||
} | ||
|
||
public function getValue(?object $object = null): mixed | ||
{ | ||
return $this->wrappedProperty->getValue(...func_get_args()); | ||
} | ||
|
||
public function setValue(mixed $objectOrValue, mixed $value = null): void | ||
{ | ||
if (func_num_args() < 2 || $objectOrValue === null || ! $this->isInitialized($objectOrValue)) { | ||
$this->wrappedProperty->setValue(...func_get_args()); | ||
|
||
return; | ||
} | ||
|
||
assert(is_object($objectOrValue)); | ||
|
||
if (parent::getValue($objectOrValue) !== $value) { | ||
throw new LogicException(sprintf('Attempting to change readonly property %s::$%s.', $this->class, $this->name)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\ReadonlyProperties; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity, Table(name: 'author')] | ||
class Author | ||
{ | ||
#[Column, Id, GeneratedValue] | ||
private readonly int $id; | ||
|
||
#[Column] | ||
private readonly string $name; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\ReadonlyProperties; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\JoinTable; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity, Table(name: 'book')] | ||
class Book | ||
{ | ||
#[Column, Id, GeneratedValue] | ||
private readonly int $id; | ||
|
||
#[Column] | ||
private readonly string $title; | ||
|
||
#[ManyToMany(targetEntity: Author::class), JoinTable(name: 'book_author')] | ||
private readonly Collection $authors; | ||
|
||
public function __construct() | ||
{ | ||
$this->authors = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
/** | ||
* @return list<Author> | ||
*/ | ||
public function getAuthors(): array | ||
{ | ||
return $this->authors->getValues(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Doctrine/Tests/Models/ReadonlyProperties/SimpleBook.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\ReadonlyProperties; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\JoinColumn; | ||
use Doctrine\ORM\Mapping\ManyToOne; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity, Table(name: 'simple_book')] | ||
class SimpleBook | ||
{ | ||
#[Column, Id, GeneratedValue] | ||
private readonly int $id; | ||
|
||
#[Column] | ||
private readonly string $title; | ||
|
||
#[ManyToOne, JoinColumn(nullable: false)] | ||
private readonly Author $author; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function getAuthor(): Author | ||
{ | ||
return $this->author; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
tests/Doctrine/Tests/ORM/Functional/ReadonlyPropertiesTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?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'] | ||
)); | ||
$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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah 😎