|
| 1 | +
|
| 2 | + |
| 3 | +namespace League\Fractal\Test; |
| 4 | + |
| 5 | +use League\Fractal\Manager; |
| 6 | +use League\Fractal\Resource\Collection; |
| 7 | +use League\Fractal\Resource\Item; |
| 8 | +use League\Fractal\Resource\Primitive; |
| 9 | +use League\Fractal\Resource\ResourceInterface; |
| 10 | +use League\Fractal\Scope; |
| 11 | +use League\Fractal\ScopeFactoryInterface; |
| 12 | +use League\Fractal\TransformerAbstract; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class MemoryTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @dataProvider TransformerProvider |
| 19 | + */ |
| 20 | + public function testDoesntLeak(ResourceInterface $resource, TransformerAbstract $transformer): void |
| 21 | + { |
| 22 | + $resource->setTransformer($transformer); |
| 23 | + $scopeFactory = \Mockery::mock(ScopeFactoryInterface::class); |
| 24 | + $manager = new Manager($scopeFactory); |
| 25 | + $scope = new Scope($manager, $resource, 'main'); |
| 26 | + $subScope = new Scope($manager, new Collection([]), 'sub'); |
| 27 | + $scopeFactory->shouldReceive('createScopeFor')->andReturn($scope); |
| 28 | + $scopeFactory->shouldReceive('createChildScopeFor')->andReturn($subScope); |
| 29 | + |
| 30 | + if ($resource instanceof Primitive) { |
| 31 | + $manager->createData($resource)->transformPrimitiveResource(); |
| 32 | + } else { |
| 33 | + $manager->createData($resource)->toArray(); |
| 34 | + } |
| 35 | + $this->assertEquals($scope, $transformer->givenScope ?? null); |
| 36 | + |
| 37 | + if ($transformer->getDefaultIncludes() !== []) { |
| 38 | + $this->assertEquals($resource instanceof Primitive ? null : $scope, $transformer->givenSubScope ?? null); |
| 39 | + } |
| 40 | + $this->assertNull($transformer->getCurrentScope()); |
| 41 | + } |
| 42 | + |
| 43 | + public function TransformerProvider(): \Generator |
| 44 | + { |
| 45 | + $basicTransformer = function () { |
| 46 | + return new class extends TransformerAbstract { |
| 47 | + public $givenScope = null; |
| 48 | + public $givenSubScope = null; |
| 49 | + |
| 50 | + public function transform($data) |
| 51 | + { |
| 52 | + $this->givenScope = $this->getCurrentScope(); |
| 53 | + return $data; |
| 54 | + } |
| 55 | + |
| 56 | + public function includeFoo(): ResourceInterface |
| 57 | + { |
| 58 | + $this->givenSubScope = $this->getCurrentScope(); |
| 59 | + return new Item(['foo'], $this); |
| 60 | + } |
| 61 | + }; |
| 62 | + }; |
| 63 | + $resources = [ |
| 64 | + new Primitive('foo'), |
| 65 | + new Collection([['a' => 'b'], ['c' => 'd']]), |
| 66 | + new Item(['foo', 'bar', 'baz']), |
| 67 | + ]; |
| 68 | + |
| 69 | + foreach ($resources as $resource) { |
| 70 | + yield [$resource, $basicTransformer()]; |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($resources as $resource) { |
| 74 | + $transformer = $basicTransformer(); |
| 75 | + $transformer->setDefaultIncludes(['foo']); |
| 76 | + yield [$resource, $transformer]; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public function testScopeHasNoStrongReferences(): void |
| 81 | + { |
| 82 | + $transformer = new class extends TransformerAbstract { |
| 83 | + public \WeakReference $givenScope; |
| 84 | + public function transform($data) |
| 85 | + { |
| 86 | + $this->givenScope = \WeakReference::create($this->getCurrentScope()); |
| 87 | + return $data; |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + $resource = new Item(['foo', 'bar', 'baz'], $transformer); |
| 92 | + $manager = new Manager(); |
| 93 | + |
| 94 | + $manager->createData($resource)->toArray(); |
| 95 | + |
| 96 | + gc_collect_cycles(); |
| 97 | + $this->assertNull($transformer->getCurrentScope()); |
| 98 | + $this->assertNull($transformer->givenScope->get()); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments