From 1fb0bfed7cb010ac182149748ee2689c339dad47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Gon=C3=A7alves?= Date: Thu, 19 Jan 2023 17:03:25 +0000 Subject: [PATCH] Fix CachedProviderTestCase to allow changing values when projecting to cache, to take in consideration that pre-projection callables can change this values (#41) --- README.md | 16 +++++++++++++++- src/TestCase/Provider/CachedProviderTestCase.php | 11 +++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index db630c4..291f3a2 100644 --- a/README.md +++ b/README.md @@ -609,7 +609,21 @@ public function getCustomerDataDataProvider(): array $args, // Arguments to your method (int this case: [123 <- $customerId]) $item, // Projection item to search in cache (e.g. new CustomerByIdProjectionItem(123)) $projectedItem, // Projected item to be return by the projection repository (null to simulate a cache miss) - $expectedProviderData, // Expected result + $providerData, // Expected result + // Optional, by default is null and only required when you are doing manipulations on your cached provider + // before projecting the item to the cache. + // + // To test this cases you can change the item as you expect it before doing the projection + // The $item received here is a clone of the $item defined above and if $providerData is iterable it is already + // injected in the item via the storeData method + function($itemToProject) { + // Do something to the item before adding it to the cache + // E.g. set a property on the item that usually is set on the pre-projection callables of the + // getAndCacheData method of the cached provider + $itemToProject->setField('a value'); + + return $itemToProject; + } ] ]; } diff --git a/src/TestCase/Provider/CachedProviderTestCase.php b/src/TestCase/Provider/CachedProviderTestCase.php index 1dfed25..4212e42 100644 --- a/src/TestCase/Provider/CachedProviderTestCase.php +++ b/src/TestCase/Provider/CachedProviderTestCase.php @@ -26,6 +26,7 @@ abstract class CachedProviderTestCase extends TestCase * @param ProjectionItemIterableInterface $item * @param ProjectionItemIterableInterface|null $projectedItem * @param iterable|null $providerData + * @param callable|null $preProjection */ public function testGetAndCacheData( $originalProvider, @@ -33,7 +34,8 @@ public function testGetAndCacheData( array $args, ProjectionItemIterableInterface $item, ?ProjectionItemIterableInterface $projectedItem, - ?iterable $providerData + ?iterable $providerData, + ?callable $preProjection = null ): void { // Get from cache ($repository = $this->getProjectionRepository()) @@ -44,11 +46,16 @@ public function testGetAndCacheData( // Cache miss if (null === $projectedItem && is_iterable($providerData)) { + $itemToProject = (clone $item)->storeData($providerData); + if (is_callable($preProjection)) { + $itemToProject = $preProjection($itemToProject); + } + // Get data from provider $repository ->expects($this->once()) ->method('add') - ->with((clone $item)->storeData($providerData)); + ->with($itemToProject); } $result = call_user_func_array([$this->getProvider($originalProvider), $method], $args);