Skip to content

Commit 08940dd

Browse files
committed
Add missing unit tests for some code paths of the TraceableCacheAdapterTrait trait
1 parent 83fcfcb commit 08940dd

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/Tracing/Cache/TraceableCacheAdapterTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
7171
{
7272
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
7373
if (!$this->decoratedAdapter instanceof CacheInterface) {
74-
throw new \BadMethodCallException(sprintf('The %s() method is not supported on an adapter that does not implement the "%s" interface.', __FUNCTION__, CacheInterface::class));
74+
throw new \BadMethodCallException(sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
7575
}
7676

7777
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
@@ -85,7 +85,7 @@ public function delete(string $key): bool
8585
{
8686
return $this->traceFunction('cache.delete_item', function () use ($key) {
8787
if (!$this->decoratedAdapter instanceof CacheInterface) {
88-
throw new \BadMethodCallException(sprintf('The %s() method is not supported on an adapter that does not implement the "%s" interface.', __FUNCTION__, CacheInterface::class));
88+
throw new \BadMethodCallException(sprintf('The %s::delete() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
8989
}
9090

9191
return $this->decoratedAdapter->delete($key);
@@ -159,7 +159,7 @@ public function prune(): bool
159159
{
160160
return $this->traceFunction('cache.prune', function (): bool {
161161
if (!$this->decoratedAdapter instanceof PruneableInterface) {
162-
throw new \BadMethodCallException(sprintf('The %s() method is not supported on an adapter that does not implement the "%s" interface.', __FUNCTION__, PruneableInterface::class));
162+
throw new \BadMethodCallException(sprintf('The %s::prune() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, PruneableInterface::class));
163163
}
164164

165165
return $this->decoratedAdapter->prune();
@@ -172,7 +172,7 @@ public function prune(): bool
172172
public function reset(): void
173173
{
174174
if (!$this->decoratedAdapter instanceof ResettableInterface) {
175-
throw new \BadMethodCallException(sprintf('The %s() method is not supported on an adapter that does not implement the "%s" interface.', __FUNCTION__, ResettableInterface::class));
175+
throw new \BadMethodCallException(sprintf('The %s::reset() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, ResettableInterface::class));
176176
}
177177

178178
$this->decoratedAdapter->reset();

tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ public function testGet(): void
146146
$this->assertNotNull($spans[1]->getEndTimestamp());
147147
}
148148

149+
public function testGetThrowsExceptionIfDecoratedAdapterDoesNotImplementTheCacheInterface(): void
150+
{
151+
$adapter = $this->createCacheAdapter($this->createMock(static::getAdapterClassFqcn()));
152+
153+
$this->expectException(\BadMethodCallException::class);
154+
$this->expectExceptionMessage(sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "Symfony\\Contracts\\Cache\\CacheInterface" interface.', \get_class($adapter)));
155+
156+
$adapter->get('foo', static function () {});
157+
}
158+
149159
public function testDelete(): void
150160
{
151161
$transaction = new Transaction(new TransactionContext(), $this->hub);
@@ -172,6 +182,16 @@ public function testDelete(): void
172182
$this->assertNotNull($spans[1]->getEndTimestamp());
173183
}
174184

185+
public function testDeleteThrowsExceptionIfDecoratedAdapterDoesNotImplementTheCacheInterface(): void
186+
{
187+
$adapter = $this->createCacheAdapter($this->createMock(static::getAdapterClassFqcn()));
188+
189+
$this->expectException(\BadMethodCallException::class);
190+
$this->expectExceptionMessage(sprintf('The %s::delete() method is not supported because the decorated adapter does not implement the "Symfony\\Contracts\\Cache\\CacheInterface" interface.', \get_class($adapter)));
191+
192+
$adapter->delete('foo');
193+
}
194+
175195
public function testHasItem(): void
176196
{
177197
$transaction = new Transaction(new TransactionContext(), $this->hub);
@@ -354,6 +374,16 @@ public function testPrune(): void
354374
$this->assertNotNull($spans[1]->getEndTimestamp());
355375
}
356376

377+
public function testPruneThrowsExceptionIfDecoratedAdapterIsNotPruneable(): void
378+
{
379+
$adapter = $this->createCacheAdapter($this->createMock(static::getAdapterClassFqcn()));
380+
381+
$this->expectException(\BadMethodCallException::class);
382+
$this->expectExceptionMessage(sprintf('The %s::prune() method is not supported because the decorated adapter does not implement the "Symfony\\Component\\Cache\\PruneableInterface" interface.', \get_class($adapter)));
383+
384+
$adapter->prune();
385+
}
386+
357387
public function testReset(): void
358388
{
359389
$decoratedAdapter = $this->createMock(ResettableCacheAdapterInterface::class);
@@ -364,6 +394,16 @@ public function testReset(): void
364394
$adapter->reset();
365395
}
366396

397+
public function testResetThrowsExceptionIfDecoratedAdapterIsNotResettable(): void
398+
{
399+
$adapter = $this->createCacheAdapter($this->createMock(static::getAdapterClassFqcn()));
400+
401+
$this->expectException(\BadMethodCallException::class);
402+
$this->expectExceptionMessage(sprintf('The %s::reset() method is not supported because the decorated adapter does not implement the "Symfony\\Component\\Cache\\ResettableInterface" interface.', \get_class($adapter)));
403+
404+
$adapter->reset();
405+
}
406+
367407
private static function isCachePackageInstalled(): bool
368408
{
369409
return interface_exists(BaseCacheInterface::class);

0 commit comments

Comments
 (0)