From 7e737bfb76132168b95000410692a44d256f0532 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Fri, 3 May 2024 14:39:12 +0200 Subject: [PATCH] Cleanup tets --- tests/CreationTest.php | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/CreationTest.php b/tests/CreationTest.php index ec40420c..d4e7d056 100644 --- a/tests/CreationTest.php +++ b/tests/CreationTest.php @@ -1074,36 +1074,33 @@ public function __invoke(SimpleData $data) })->skip(fn () => config('data.features.cast_and_transform_iterables') === false); it('keeps the creation context path up to date', function () { - global $testCreationContexts; - $testCreationContexts = []; - class TestDataPipe implements DataPipe + class TestCreationContextCollectorDataPipe implements DataPipe { + public static array $contexts = []; + public function handle(mixed $payload, DataClass $class, array $properties, CreationContext $creationContext): array { - global $testCreationContexts; - $testCreationContexts[] = clone $creationContext; + static::$contexts[] = clone $creationContext; return $properties; } } - class SimpleDataWithTestPipe extends SimpleData + class TestDataWithCreationContextCollectorPipe extends SimpleData { public static function pipeline(): DataPipeline { - return parent::pipeline() - ->through(TestDataPipe::class); + return parent::pipeline()->through(TestCreationContextCollectorDataPipe::class); } } $dataClass = new class () extends Data { - #[DataCollectionOf(SimpleDataWithTestPipe::class)] + #[DataCollectionOf(TestDataWithCreationContextCollectorPipe::class)] public Collection $collection; public static function pipeline(): DataPipeline { - return parent::pipeline() - ->through(TestDataPipe::class); + return parent::pipeline()->through(TestCreationContextCollectorDataPipe::class); } }; @@ -1111,12 +1108,13 @@ public static function pipeline(): DataPipeline 'collection' => [['string' => 'no'], 'models', ['string' => 'here']], ]); - expect($testCreationContexts)->toHaveCount(3); - expect($testCreationContexts[0])->toBeInstanceOf(CreationContext::class); + expect(TestCreationContextCollectorDataPipe::$contexts) + ->toHaveCount(3) + ->each()->toBeInstanceOf(CreationContext::class); - expect($testCreationContexts[0]->currentPath)->toBe([0 => 'collection', 1 => 0]); - expect($testCreationContexts[1]->currentPath)->toBe([0 => 'collection', 1 => 2]); - expect($testCreationContexts[2]->currentPath)->toHaveCount(0); + expect(TestCreationContextCollectorDataPipe::$contexts[0]->currentPath)->toBe([0 => 'collection', 1 => 0]); + expect(TestCreationContextCollectorDataPipe::$contexts[1]->currentPath)->toBe([0 => 'collection', 1 => 2]); + expect(TestCreationContextCollectorDataPipe::$contexts[2]->currentPath)->toHaveCount(0); });