Skip to content

Commit

Permalink
Fix CachedProviderTestCase to allow changing values when projecting t…
Browse files Browse the repository at this point in the history
…o cache, to take in consideration that pre-projection callables can change this values (#41)
  • Loading branch information
hugo-goncalves-kununu authored Jan 19, 2023
1 parent 4fc1e44 commit 1fb0bfe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
]
];
}
Expand Down
11 changes: 9 additions & 2 deletions src/TestCase/Provider/CachedProviderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ abstract class CachedProviderTestCase extends TestCase
* @param ProjectionItemIterableInterface $item
* @param ProjectionItemIterableInterface|null $projectedItem
* @param iterable|null $providerData
* @param callable|null $preProjection
*/
public function testGetAndCacheData(
$originalProvider,
string $method,
array $args,
ProjectionItemIterableInterface $item,
?ProjectionItemIterableInterface $projectedItem,
?iterable $providerData
?iterable $providerData,
?callable $preProjection = null
): void {
// Get from cache
($repository = $this->getProjectionRepository())
Expand All @@ -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);
Expand Down

0 comments on commit 1fb0bfe

Please sign in to comment.