|
22 | 22 | use Config\Services; |
23 | 23 | use PHPUnit\Framework\Attributes\Group; |
24 | 24 | use stdClass; |
| 25 | +use Tests\Support\API\ParentTransformer; |
25 | 26 |
|
26 | 27 | /** |
27 | 28 | * @internal |
@@ -641,4 +642,69 @@ protected function includePosts(): array |
641 | 642 | $this->assertArrayHasKey('posts', $result); |
642 | 643 | $this->assertSame([['id' => 1, 'title' => 'Post 1']], $result['posts']); |
643 | 644 | } |
| 645 | + |
| 646 | + public function testTransformRelatedPreventsGlobalStateLeakage(): void |
| 647 | + { |
| 648 | + $request = $this->createMockRequest('include=child'); |
| 649 | + |
| 650 | + $transformer = new ParentTransformer($request); |
| 651 | + $result = $transformer->transform(['id' => 1]); |
| 652 | + |
| 653 | + $this->assertSame([ |
| 654 | + 'parent_id' => 1, |
| 655 | + 'child' => [ |
| 656 | + 'child_id' => 99, |
| 657 | + 'status' => 'transformed', |
| 658 | + ], |
| 659 | + ], $result); |
| 660 | + } |
| 661 | + |
| 662 | + public function testTransformRelatedSmartRoutingForSingleItem(): void |
| 663 | + { |
| 664 | + $request = $this->createMockRequest('include=child'); |
| 665 | + |
| 666 | + $transformer = new ParentTransformer($request); |
| 667 | + $result = $transformer->transform(['id' => 1]); |
| 668 | + |
| 669 | + $this->assertIsArray($result['child']); |
| 670 | + $this->assertArrayHasKey('child_id', $result['child']); |
| 671 | + $this->assertSame(99, $result['child']['child_id']); |
| 672 | + } |
| 673 | + |
| 674 | + public function testTransformRelatedSmartRoutingForCollection(): void |
| 675 | + { |
| 676 | + $request = $this->createMockRequest('include=childrenCollection'); |
| 677 | + |
| 678 | + $transformer = new ParentTransformer($request); |
| 679 | + $result = $transformer->transform(['id' => 1]); |
| 680 | + |
| 681 | + $this->assertIsArray($result['childrenCollection']); |
| 682 | + $this->assertCount(2, $result['childrenCollection']); |
| 683 | + $this->assertSame(77, $result['childrenCollection'][0]['child_id']); |
| 684 | + $this->assertSame(88, $result['childrenCollection'][1]['child_id']); |
| 685 | + } |
| 686 | + |
| 687 | + public function testTransformRelatedWorksWhenParentUsesTransformMany(): void |
| 688 | + { |
| 689 | + $request = $this->createMockRequest('include=child'); |
| 690 | + $transformer = new ParentTransformer($request); |
| 691 | + |
| 692 | + $parents = [ |
| 693 | + ['id' => 1], |
| 694 | + ['id' => 2], |
| 695 | + ]; |
| 696 | + |
| 697 | + $result = $transformer->transformMany($parents); |
| 698 | + |
| 699 | + $this->assertIsArray($result); |
| 700 | + $this->assertCount(2, $result); |
| 701 | + |
| 702 | + $this->assertSame(1, $result[0]['parent_id']); |
| 703 | + $this->assertIsArray($result[0]['child']); |
| 704 | + $this->assertSame(99, $result[0]['child']['child_id']); |
| 705 | + |
| 706 | + $this->assertSame(2, $result[1]['parent_id']); |
| 707 | + $this->assertIsArray($result[1]['child']); |
| 708 | + $this->assertSame(99, $result[1]['child']['child_id']); |
| 709 | + } |
644 | 710 | } |
0 commit comments