-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
check readonly property via serialization #11617
Open
garak
wants to merge
3
commits into
doctrine:2.20.x
Choose a base branch
from
garak:fix-readonly-property
base: 2.20.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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 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 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,20 @@ | ||
<?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\Tests\Models\ValueObjects\Uuid; | ||
|
||
#[Entity] | ||
class Library | ||
{ | ||
#[Column] | ||
#[Id] | ||
#[GeneratedValue(strategy: 'NONE')] | ||
public readonly Uuid $uuid; | ||
} |
This file contains 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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\ValueObjects; | ||
|
||
class Uuid | ||
{ | ||
/** @var string */ | ||
public $id; | ||
|
||
public function __construct(string $id) | ||
{ | ||
$this->id = $id; | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -7,6 +7,9 @@ | |
use Doctrine\ORM\Mapping\ReflectionReadonlyProperty; | ||
use Doctrine\Tests\Models\CMS\CmsTag; | ||
use Doctrine\Tests\Models\ReadonlyProperties\Author; | ||
use Doctrine\Tests\Models\ReadonlyProperties\Library; | ||
use Doctrine\Tests\Models\ValueObjects\Uuid; | ||
use Generator; | ||
use InvalidArgumentException; | ||
use LogicException; | ||
use PHPUnit\Framework\TestCase; | ||
|
@@ -17,36 +20,89 @@ | |
*/ | ||
class ReflectionReadonlyPropertyTest extends TestCase | ||
{ | ||
public function testSecondWriteWithSameValue(): void | ||
/** | ||
* @dataProvider sameValueProvider | ||
*/ | ||
public function testSecondWriteWithSameValue(object $entity, string $property, mixed $value, mixed $sameValue): void | ||
{ | ||
$author = new Author(); | ||
|
||
$wrappedReflection = new ReflectionProperty($author, 'name'); | ||
$wrappedReflection = new ReflectionProperty($entity, $property); | ||
$reflection = new ReflectionReadonlyProperty($wrappedReflection); | ||
|
||
$reflection->setValue($author, 'John Doe'); | ||
$reflection->setValue($entity, $value); | ||
|
||
self::assertSame('John Doe', $wrappedReflection->getValue($author)); | ||
self::assertSame('John Doe', $reflection->getValue($author)); | ||
self::assertSame($value, $wrappedReflection->getValue($entity)); | ||
self::assertSame($value, $reflection->getValue($entity)); | ||
|
||
$reflection->setValue($author, 'John Doe'); | ||
$reflection->setValue($entity, $sameValue); | ||
|
||
self::assertSame('John Doe', $wrappedReflection->getValue($author)); | ||
self::assertSame('John Doe', $reflection->getValue($author)); | ||
/* | ||
* Intentionally testing against the initial $value rather than the $sameValue that we just set above one in | ||
* order to catch false positives when dealing with object types | ||
*/ | ||
self::assertSame($value, $wrappedReflection->getValue($entity)); | ||
self::assertSame($value, $reflection->getValue($entity)); | ||
} | ||
|
||
public function testSecondWriteWithDifferentValue(): void | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use |
||
* @return Generator<string, array{entity: object, property: string, value: string|object, sameValue: string|object}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wow didn't know we could use named arguments here 💡 |
||
*/ | ||
public static function sameValueProvider(): Generator | ||
{ | ||
$author = new Author(); | ||
yield 'string' => [ | ||
'entity' => new Author(), | ||
'property' => 'name', | ||
'value' => 'John Doe', | ||
'sameValue' => 'John Doe', | ||
]; | ||
|
||
yield 'uuid' => [ | ||
'entity' => new Library(), | ||
'property' => 'uuid', | ||
'value' => new Uuid('438d5dc3-36c9-410a-88db-7a184856ebb8'), | ||
'sameValue' => new Uuid('438d5dc3-36c9-410a-88db-7a184856ebb8'), | ||
]; | ||
} | ||
|
||
$wrappedReflection = new ReflectionProperty($author, 'name'); | ||
/** | ||
* @dataProvider differentValueProvider | ||
*/ | ||
public function testSecondWriteWithDifferentValue( | ||
object $entity, | ||
string $property, | ||
mixed $value, | ||
mixed $differentValue, | ||
string $expectedExceptionMessage | ||
): void { | ||
$wrappedReflection = new ReflectionProperty($entity, $property); | ||
$reflection = new ReflectionReadonlyProperty($wrappedReflection); | ||
|
||
$reflection->setValue($author, 'John Doe'); | ||
$reflection->setValue($entity, $value); | ||
|
||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Attempting to change readonly property Doctrine\Tests\Models\ReadonlyProperties\Author::$name.'); | ||
$reflection->setValue($author, 'Jane Doe'); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
$reflection->setValue($entity, $differentValue); | ||
} | ||
|
||
/** | ||
* @return Generator<string, array{entity: object, property: string, value: string|object, sameValue: string|object, expectedExceptionMessage: string}> | ||
*/ | ||
public static function differentValueProvider(): Generator | ||
{ | ||
yield 'string' => [ | ||
'entity' => new Author(), | ||
'property' => 'name', | ||
'value' => 'John Doe', | ||
'differentValue' => 'Jane Doe', | ||
'expectedExceptionMessage' => 'Attempting to change readonly property Doctrine\Tests\Models\ReadonlyProperties\Author::$name.', | ||
]; | ||
|
||
yield 'uuid' => [ | ||
'entity' => new Library(), | ||
'property' => 'uuid', | ||
'value' => new Uuid('438d5dc3-36c9-410a-88db-7a184856ebb8'), | ||
'differentValue' => new Uuid('5d5049ee-01fd-4b66-9f82-9f637fff6a7d'), | ||
'expectedExceptionMessage' => 'Attempting to change readonly property Doctrine\Tests\Models\ReadonlyProperties\Library::$uuid.', | ||
]; | ||
} | ||
|
||
public function testNonReadonlyPropertiesAreForbidden(): void | ||
|
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.
Another way to do it would be using a loose comparison, although I'm not sure if there are any performance implications. I'd like to know if you've considered it though.
orm/src/UnitOfWork.php
Lines 2319 to 2320 in 982d606
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.
I didn't try it indeed. I'm not a big fan of loose comparison, which often leads to unexpected consequences in PHP.
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.
Ok, I'm undecided myself. Let's see what other maintainers have to say.
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.
this looks extremely expensive to call serialize
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.
"extremely", really?