|
22 | 22 | use Config\Services; |
23 | 23 | use PHPUnit\Framework\Attributes\Group; |
24 | 24 | use stdClass; |
| 25 | +use Tests\Support\API\ChildTransformer; |
| 26 | +use Tests\Support\API\ParentTransformer; |
25 | 27 |
|
26 | 28 | /** |
27 | 29 | * @internal |
@@ -641,4 +643,160 @@ protected function includePosts(): array |
641 | 643 | $this->assertArrayHasKey('posts', $result); |
642 | 644 | $this->assertSame([['id' => 1, 'title' => 'Post 1']], $result['posts']); |
643 | 645 | } |
| 646 | + |
| 647 | + public function testNestedTransformerDoesNotInheritIncludeState(): void |
| 648 | + { |
| 649 | + // The child transformer has no includeChildren() method. If the root |
| 650 | + // request's `include=children` leaked into it, transforming the child |
| 651 | + // would raise an ApiException for the missing include method. |
| 652 | + $request = $this->createMockRequest('include=children'); |
| 653 | + Services::injectMock('request', $request); |
| 654 | + |
| 655 | + $transformer = new ParentTransformer($request); |
| 656 | + |
| 657 | + $result = $transformer->transform(['id' => 1]); |
| 658 | + |
| 659 | + $this->assertSame([ |
| 660 | + 'parent_id' => 1, |
| 661 | + 'children' => ['child_id' => 99, 'status' => 'transformed'], |
| 662 | + ], $result); |
| 663 | + } |
| 664 | + |
| 665 | + public function testNestedTransformerDoesNotInheritFieldFilter(): void |
| 666 | + { |
| 667 | + // `fields=parent_id` applies to the root only. If it leaked into the |
| 668 | + // child, array_intersect_key would strip every child field, leaving []. |
| 669 | + $request = $this->createMockRequest('include=children&fields=parent_id'); |
| 670 | + Services::injectMock('request', $request); |
| 671 | + |
| 672 | + $transformer = new ParentTransformer($request); |
| 673 | + |
| 674 | + $result = $transformer->transform(['id' => 1]); |
| 675 | + |
| 676 | + $this->assertSame([ |
| 677 | + 'parent_id' => 1, |
| 678 | + 'children' => ['child_id' => 99, 'status' => 'transformed'], |
| 679 | + ], $result); |
| 680 | + } |
| 681 | + |
| 682 | + public function testNestedCollectionTransformerDoesNotInheritState(): void |
| 683 | + { |
| 684 | + $request = $this->createMockRequest('include=childrenCollection&fields=parent_id'); |
| 685 | + Services::injectMock('request', $request); |
| 686 | + |
| 687 | + $transformer = new ParentTransformer($request); |
| 688 | + |
| 689 | + $result = $transformer->transform(['id' => 1]); |
| 690 | + |
| 691 | + $this->assertSame([ |
| 692 | + 'parent_id' => 1, |
| 693 | + 'childrenCollection' => [ |
| 694 | + ['child_id' => 77, 'status' => 'transformed'], |
| 695 | + ['child_id' => 88, 'status' => 'transformed'], |
| 696 | + ], |
| 697 | + ], $result); |
| 698 | + } |
| 699 | + |
| 700 | + public function testBareNestedInstantiationDoesNotInheritState(): void |
| 701 | + { |
| 702 | + // Reproduces the exact leak vector: a child created with a bare |
| 703 | + // `new ChildTransformer()` (no request passed) inside an include |
| 704 | + // method must not pick up the root request's scope from request(). |
| 705 | + $request = $this->createMockRequest('include=children&fields=parent_id'); |
| 706 | + Services::injectMock('request', $request); |
| 707 | + |
| 708 | + $root = new ParentTransformer(); |
| 709 | + |
| 710 | + $result = $root->transform(['id' => 5]); |
| 711 | + |
| 712 | + $this->assertSame([ |
| 713 | + 'parent_id' => 5, |
| 714 | + 'children' => ['child_id' => 99, 'status' => 'transformed'], |
| 715 | + ], $result); |
| 716 | + } |
| 717 | + |
| 718 | + public function testNestedTransformerHonorsExplicitRequest(): void |
| 719 | + { |
| 720 | + // A child created with an explicitly passed request must honor that |
| 721 | + // request's scope even while nested - the isolation only suppresses |
| 722 | + // the implicit global fallback, not deliberate developer intent. |
| 723 | + $request = $this->createMockRequest('include=explicitChild&fields=child_id,parent_id'); |
| 724 | + Services::injectMock('request', $request); |
| 725 | + |
| 726 | + $transformer = new ParentTransformer($request); |
| 727 | + |
| 728 | + $result = $transformer->transform(['id' => 1]); |
| 729 | + |
| 730 | + $this->assertSame([ |
| 731 | + 'parent_id' => 1, |
| 732 | + 'explicitChild' => ['child_id' => 99], |
| 733 | + ], $result); |
| 734 | + } |
| 735 | + |
| 736 | + public function testRootScopeStillAppliesAfterNesting(): void |
| 737 | + { |
| 738 | + // Sanity check that the root transformer keeps applying its own scope |
| 739 | + // while nested children are isolated. |
| 740 | + $request = $this->createMockRequest('include=children&fields=parent_id'); |
| 741 | + Services::injectMock('request', $request); |
| 742 | + |
| 743 | + $transformer = new ParentTransformer($request); |
| 744 | + |
| 745 | + $result = $transformer->transform(['id' => 1, 'secret' => 'hidden']); |
| 746 | + |
| 747 | + // Root keeps only parent_id (plus the include key), the child is intact. |
| 748 | + $this->assertArrayHasKey('parent_id', $result); |
| 749 | + $this->assertArrayNotHasKey('secret', $result); |
| 750 | + $this->assertSame(['child_id' => 99, 'status' => 'transformed'], $result['children']); |
| 751 | + } |
| 752 | + |
| 753 | + public function testDepthIsRestoredAfterIncludeThrows(): void |
| 754 | + { |
| 755 | + $request = $this->createMockRequest('include=nonexistent'); |
| 756 | + Services::injectMock('request', $request); |
| 757 | + |
| 758 | + $throwingRoot = new class ($request) extends BaseTransformer { |
| 759 | + public function toArray(mixed $resource): array |
| 760 | + { |
| 761 | + return $resource; |
| 762 | + } |
| 763 | + }; |
| 764 | + |
| 765 | + try { |
| 766 | + $throwingRoot->transform(['id' => 1, 'name' => 'Test']); |
| 767 | + $this->fail('Expected ApiException was not thrown.'); |
| 768 | + } catch (ApiException) { |
| 769 | + // expected |
| 770 | + } |
| 771 | + |
| 772 | + // The nesting depth must be balanced after the exception, so a fresh |
| 773 | + // root transformer still applies the request scope (depth back to 0). |
| 774 | + $fieldsRequest = $this->createMockRequest('fields=id'); |
| 775 | + Services::injectMock('request', $fieldsRequest); |
| 776 | + |
| 777 | + $nextRoot = new class ($fieldsRequest) extends BaseTransformer { |
| 778 | + public function toArray(mixed $resource): array |
| 779 | + { |
| 780 | + return $resource; |
| 781 | + } |
| 782 | + }; |
| 783 | + |
| 784 | + $result = $nextRoot->transform(['id' => 1, 'name' => 'Test']); |
| 785 | + |
| 786 | + $this->assertSame(['id' => 1], $result); |
| 787 | + } |
| 788 | + |
| 789 | + public function testBareNestedTransformerStillUsedByChildTransformerDirectly(): void |
| 790 | + { |
| 791 | + // When ChildTransformer is itself the root (no parent), it must apply |
| 792 | + // request scope as usual - the isolation only affects nesting. |
| 793 | + $request = $this->createMockRequest('fields=child_id'); |
| 794 | + Services::injectMock('request', $request); |
| 795 | + |
| 796 | + $transformer = new ChildTransformer($request); |
| 797 | + |
| 798 | + $result = $transformer->transform(['id' => 42]); |
| 799 | + |
| 800 | + $this->assertSame(['child_id' => 42], $result); |
| 801 | + } |
644 | 802 | } |
0 commit comments