|
36 | 36 | #[\PHPUnit\Framework\Attributes\UsesClass(TransformerException::class)] |
37 | 37 | class MappingTransformerTest extends TestCase |
38 | 38 | { |
| 39 | + /** |
| 40 | + * @var list<string> |
| 41 | + */ |
| 42 | + private array $deprecations = []; |
| 43 | + |
39 | 44 | public function testGetCode(): void |
40 | 45 | { |
41 | 46 | self::assertSame('mapping', $this->createTransformer()->getCode()); |
@@ -423,6 +428,65 @@ public function testMissingNonSimpleTargetPropertyOfAStdClassThrows(string $targ |
423 | 428 | $transformer->transform([], $options); |
424 | 429 | } |
425 | 430 |
|
| 431 | + public function testSimpleTargetPropertyIsAddedToAnArrayDestinationWithoutDeprecation(): void |
| 432 | + { |
| 433 | + $transformer = $this->createTransformer(); |
| 434 | + $options = $this->resolveOptions($transformer, [ |
| 435 | + 'mapping' => [ |
| 436 | + 'field2' => ['code' => '[field]'], |
| 437 | + ], |
| 438 | + ]); |
| 439 | + |
| 440 | + $result = $this->transformCollectingDeprecations($transformer, ['field' => 'value'], $options); |
| 441 | + |
| 442 | + self::assertSame(['field2' => 'value'], $result); |
| 443 | + self::assertSame([], $this->deprecations); |
| 444 | + } |
| 445 | + |
| 446 | + /** |
| 447 | + * @return iterable<string, array{string}> |
| 448 | + */ |
| 449 | + public static function nonSimpleArrayTargetPropertyProvider(): iterable |
| 450 | + { |
| 451 | + yield 'nested path' => ['field2.child']; |
| 452 | + yield 'index after a property' => ['field2[child]']; |
| 453 | + yield 'property after an index' => ['[field2].child']; |
| 454 | + } |
| 455 | + |
| 456 | + #[DataProvider('nonSimpleArrayTargetPropertyProvider')] |
| 457 | + public function testNonSimpleTargetPropertyOfAnArrayDestinationIsDeprecated(string $targetProperty): void |
| 458 | + { |
| 459 | + $transformer = $this->createTransformer(); |
| 460 | + $options = $this->resolveOptions($transformer, [ |
| 461 | + 'mapping' => [ |
| 462 | + $targetProperty => ['code' => '[field]'], |
| 463 | + ], |
| 464 | + ]); |
| 465 | + |
| 466 | + $result = $this->transformCollectingDeprecations($transformer, ['field' => 'value'], $options); |
| 467 | + |
| 468 | + self::assertSame([$targetProperty => 'value'], $result); |
| 469 | + self::assertSame([ |
| 470 | + "Setting the target property '{$targetProperty}' as a literal key of an array destination is deprecated, it will throw an \\UnexpectedValueException in v6.0. Use '[{$targetProperty}]' to keep a literal key, or the index notation for nested arrays (e.g. '[a][b]').", |
| 471 | + ], $this->deprecations); |
| 472 | + } |
| 473 | + |
| 474 | + public function testIndexNotationKeepsALiteralKeyWithoutDeprecation(): void |
| 475 | + { |
| 476 | + $transformer = $this->createTransformer(); |
| 477 | + $options = $this->resolveOptions($transformer, [ |
| 478 | + 'mapping' => [ |
| 479 | + '[field2.child]' => ['code' => '[field]'], |
| 480 | + '[field3][child]' => ['code' => '[field]'], |
| 481 | + ], |
| 482 | + ]); |
| 483 | + |
| 484 | + $result = $this->transformCollectingDeprecations($transformer, ['field' => 'value'], $options); |
| 485 | + |
| 486 | + self::assertSame(['field2.child' => 'value', 'field3' => ['child' => 'value']], $result); |
| 487 | + self::assertSame([], $this->deprecations); |
| 488 | + } |
| 489 | + |
426 | 490 | public function testKeepInputCopiesAnArrayInput(): void |
427 | 491 | { |
428 | 492 | $transformer = $this->createTransformer(); |
@@ -565,6 +629,23 @@ public function log($level, \Stringable|string $message, array $context = []): v |
565 | 629 | }; |
566 | 630 | } |
567 | 631 |
|
| 632 | + /** |
| 633 | + * @param array<string, mixed> $options |
| 634 | + */ |
| 635 | + private function transformCollectingDeprecations(MappingTransformer $transformer, mixed $value, array $options): mixed |
| 636 | + { |
| 637 | + set_error_handler(function (int $errno, string $errstr): bool { |
| 638 | + $this->deprecations[] = $errstr; |
| 639 | + |
| 640 | + return true; |
| 641 | + }, \E_USER_DEPRECATED); |
| 642 | + try { |
| 643 | + return $transformer->transform($value, $options); |
| 644 | + } finally { |
| 645 | + restore_error_handler(); |
| 646 | + } |
| 647 | + } |
| 648 | + |
568 | 649 | /** |
569 | 650 | * @param array<string, mixed> $options |
570 | 651 | * |
|
0 commit comments