|
7 | 7 | use DateTime; |
8 | 8 | use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
9 | 9 | use Doctrine\ODM\MongoDB\Tests\BaseTestCase; |
| 10 | +use Doctrine\ODM\MongoDB\Tests\CaptureDeprecationMessages; |
10 | 11 | use Doctrine\ODM\MongoDB\Types\ClosureToPHP; |
11 | 12 | use Doctrine\ODM\MongoDB\Types\Type; |
12 | 13 | use Exception; |
|
20 | 21 |
|
21 | 22 | class CustomTypeTest extends BaseTestCase |
22 | 23 | { |
| 24 | + use CaptureDeprecationMessages; |
| 25 | + |
23 | 26 | public function setUp(): void |
24 | 27 | { |
25 | 28 | parent::setUp(); |
26 | 29 |
|
27 | 30 | Type::addType('date_collection', DateCollectionType::class); |
28 | 31 | Type::addType(Language::class, LanguageType::class); |
| 32 | + Type::addType('custom_type_without_closure_to_php', CustomTypeWithoutClosureToPHP::class); |
29 | 33 | } |
30 | 34 |
|
31 | 35 | #[After] |
@@ -89,6 +93,16 @@ public function testTypeFromPHPVariable(): void |
89 | 93 | $databaseValue = Type::convertPHPToDatabaseValue($lang); |
90 | 94 | self::assertSame(['name' => 'French', 'code' => 'fr'], $databaseValue); |
91 | 95 | } |
| 96 | + |
| 97 | + public function testNotOverridingClosureToPHPIsDeprecated(): void |
| 98 | + { |
| 99 | + $type = Type::getType('custom_type_without_closure_to_php'); |
| 100 | + |
| 101 | + $code = $this->captureDeprecationMessages(static fn () => $type->closureToPHP(), $deprecations); |
| 102 | + |
| 103 | + self::assertSame('$return = $value;', $code); |
| 104 | + self::assertSame(['Since doctrine/mongodb-odm 2.16: The method Type::closureToPHP() will change its default implementation in 3.0 to use convertToPHPValue(). Override this method if you need custom behavior before upgrading to 3.0 or use the trait ClosureToPHP to get the upcoming behavior now.'], $deprecations); |
| 105 | + } |
92 | 106 | } |
93 | 107 |
|
94 | 108 | class DateCollectionType extends Type |
@@ -198,3 +212,7 @@ public function convertToPHPValue($value): ?Language |
198 | 212 | return new Language($value['name'], $value['code']); |
199 | 213 | } |
200 | 214 | } |
| 215 | + |
| 216 | +class CustomTypeWithoutClosureToPHP extends Type |
| 217 | +{ |
| 218 | +} |
0 commit comments