|
7 | 7 | use Doctrine\ORM\Mapping\ReflectionReadonlyProperty; |
8 | 8 | use Doctrine\Tests\Models\CMS\CmsTag; |
9 | 9 | use Doctrine\Tests\Models\ReadonlyProperties\Author; |
| 10 | +use Doctrine\Tests\Models\ReadonlyProperties\Library; |
| 11 | +use Doctrine\Tests\Models\ValueObjects\Uuid; |
| 12 | +use Generator; |
10 | 13 | use InvalidArgumentException; |
11 | 14 | use LogicException; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
12 | 16 | use PHPUnit\Framework\TestCase; |
13 | 17 | use ReflectionProperty; |
14 | 18 |
|
15 | 19 | class ReflectionReadonlyPropertyTest extends TestCase |
16 | 20 | { |
17 | | - public function testSecondWriteWithSameValue(): void |
| 21 | + #[DataProvider('sameValueProvider')] |
| 22 | + public function testSecondWriteWithSameValue(object $entity, string $property, mixed $value, mixed $sameValue): void |
18 | 23 | { |
19 | | - $author = new Author(); |
20 | | - |
21 | | - $wrappedReflection = new ReflectionProperty($author, 'name'); |
| 24 | + $wrappedReflection = new ReflectionProperty($entity, $property); |
22 | 25 | $reflection = new ReflectionReadonlyProperty($wrappedReflection); |
23 | 26 |
|
24 | | - $reflection->setValue($author, 'John Doe'); |
| 27 | + $reflection->setValue($entity, $value); |
25 | 28 |
|
26 | | - self::assertSame('John Doe', $wrappedReflection->getValue($author)); |
27 | | - self::assertSame('John Doe', $reflection->getValue($author)); |
| 29 | + self::assertSame($value, $wrappedReflection->getValue($entity)); |
| 30 | + self::assertSame($value, $reflection->getValue($entity)); |
28 | 31 |
|
29 | | - $reflection->setValue($author, 'John Doe'); |
| 32 | + $reflection->setValue($entity, $sameValue); |
30 | 33 |
|
31 | | - self::assertSame('John Doe', $wrappedReflection->getValue($author)); |
32 | | - self::assertSame('John Doe', $reflection->getValue($author)); |
| 34 | + /* |
| 35 | + * Intentionally testing against the initial $value rather than the $sameValue that we just set above one in |
| 36 | + * order to catch false positives when dealing with object types |
| 37 | + */ |
| 38 | + self::assertSame($value, $wrappedReflection->getValue($entity)); |
| 39 | + self::assertSame($value, $reflection->getValue($entity)); |
33 | 40 | } |
34 | 41 |
|
35 | | - public function testSecondWriteWithDifferentValue(): void |
| 42 | + /** @return Generator<string, array{entity: object, property: string, value: string|object, sameValue: string|object}> */ |
| 43 | + public static function sameValueProvider(): Generator |
36 | 44 | { |
37 | | - $author = new Author(); |
| 45 | + yield 'string' => [ |
| 46 | + 'entity' => new Author(), |
| 47 | + 'property' => 'name', |
| 48 | + 'value' => 'John Doe', |
| 49 | + 'sameValue' => 'John Doe', |
| 50 | + ]; |
| 51 | + |
| 52 | + yield 'uuid' => [ |
| 53 | + 'entity' => new Library(), |
| 54 | + 'property' => 'uuid', |
| 55 | + 'value' => new Uuid('438d5dc3-36c9-410a-88db-7a184856ebb8'), |
| 56 | + 'sameValue' => new Uuid('438d5dc3-36c9-410a-88db-7a184856ebb8'), |
| 57 | + ]; |
| 58 | + } |
38 | 59 |
|
39 | | - $wrappedReflection = new ReflectionProperty($author, 'name'); |
| 60 | + #[DataProvider('differentValueProvider')] |
| 61 | + public function testSecondWriteWithDifferentValue( |
| 62 | + object $entity, |
| 63 | + string $property, |
| 64 | + mixed $value, |
| 65 | + mixed $differentValue, |
| 66 | + string $expectedExceptionMessage, |
| 67 | + ): void { |
| 68 | + $wrappedReflection = new ReflectionProperty($entity, $property); |
40 | 69 | $reflection = new ReflectionReadonlyProperty($wrappedReflection); |
41 | 70 |
|
42 | | - $reflection->setValue($author, 'John Doe'); |
| 71 | + $reflection->setValue($entity, $value); |
43 | 72 |
|
44 | 73 | $this->expectException(LogicException::class); |
45 | | - $this->expectExceptionMessage('Attempting to change readonly property Doctrine\Tests\Models\ReadonlyProperties\Author::$name.'); |
46 | | - $reflection->setValue($author, 'Jane Doe'); |
| 74 | + $this->expectExceptionMessage($expectedExceptionMessage); |
| 75 | + $reflection->setValue($entity, $differentValue); |
| 76 | + } |
| 77 | + |
| 78 | + /** @return Generator<string, array{entity: object, property: string, value: string|object, sameValue: string|object, expectedExceptionMessage: string}> */ |
| 79 | + public static function differentValueProvider(): Generator |
| 80 | + { |
| 81 | + yield 'string' => [ |
| 82 | + 'entity' => new Author(), |
| 83 | + 'property' => 'name', |
| 84 | + 'value' => 'John Doe', |
| 85 | + 'differentValue' => 'Jane Doe', |
| 86 | + 'expectedExceptionMessage' => 'Attempting to change readonly property Doctrine\Tests\Models\ReadonlyProperties\Author::$name.', |
| 87 | + ]; |
| 88 | + |
| 89 | + yield 'uuid' => [ |
| 90 | + 'entity' => new Library(), |
| 91 | + 'property' => 'uuid', |
| 92 | + 'value' => new Uuid('438d5dc3-36c9-410a-88db-7a184856ebb8'), |
| 93 | + 'differentValue' => new Uuid('5d5049ee-01fd-4b66-9f82-9f637fff6a7d'), |
| 94 | + 'expectedExceptionMessage' => 'Attempting to change readonly property Doctrine\Tests\Models\ReadonlyProperties\Library::$uuid.', |
| 95 | + ]; |
47 | 96 | } |
48 | 97 |
|
49 | 98 | public function testNonReadonlyPropertiesAreForbidden(): void |
|
0 commit comments