Skip to content

Commit

Permalink
Added test to ensure CacheItemPool is not cloned in CachingConnector.
Browse files Browse the repository at this point in the history
Removed note in Readme about caching not being supported.
  • Loading branch information
Bilge committed Jul 29, 2023
1 parent 291d172 commit 26f4c9a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,6 @@ Any connector can be wrapped in a `CachingConnector` to provide [PSR-6][] cachin

Remember that whilst using a `CachingConnector` enables caching, caching must also be enabled on a per-import basis by calling `Import::enableCache()`.

Note that Caching is not yet supported for asynchronous imports.

### Example

The follow example enables connector caching.
Expand Down
2 changes: 2 additions & 0 deletions src/Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface Provider
{
/**
* Gets a connector compatible with this provider's resources.
*
* NB: this should not be a factory method as it will break things like caching.
*/
public function getConnector(): Connector;
}
20 changes: 18 additions & 2 deletions test/Integration/Connector/CachingConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testCacheBypassedForDifferentOptions(): void
}

/**
* That that when the generated cache key contains non-compliant PSR-6 characters,
* Tests that when the generated cache key contains non-compliant PSR-6 characters,
* InvalidCacheKeyException is thrown.
*/
public function testValidateCacheKey(): void
Expand All @@ -88,10 +88,26 @@ public function testGetWrappedConnector(): void
/**
* Tests that cloning the caching connector also clones the wrapped connector.
*/
public function testClone(): void
public function testCloneConnector(): void
{
$clone = clone $this->connector;

self::assertNotSame($this->wrappedConnector, $clone->getWrappedConnector());
}

/**
* Tests that cloning the caching connector does not clone the cache item pool (both instances point to the same
* pool).
*/
public function testCloneCacheItemPool(): void
{
$clone = clone $this->connector;

$cacheA = \Closure::bind(fn () => $this->cache, $this->connector, $this->connector)();
$cacheB = \Closure::bind(fn () => $this->cache, $clone, $clone)();
self::assertSame($cacheA, $cacheB);

$clone->fetch($this->source);
self::assertSame(1, $cacheA->count(), 'Fetch on clone updates original cache item pool.');
}
}

0 comments on commit 26f4c9a

Please sign in to comment.