Skip to content

Commit f2df979

Browse files
authored
Avoid throwing from the TraceableCacheAdapterTrait::prune() and TraceableCacheAdapterTrait::reset() methods (#543)
1 parent dc611e5 commit f2df979

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fix return type for `TracingDriver::getDatabase()` method (#541)
6+
- Avoid throwing exception from the `TraceableCacheAdapterTrait::prune()` and `TraceableCacheAdapterTrait::reset()` methods when the decorated adapter does not implement the respective interfaces (#543)
67

78
## 4.2.0 (2021-08-12)
89

phpstan-baseline.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ parameters:
222222

223223
-
224224
message: "#^Call to an undefined method TCacheAdapter of Symfony\\\\Component\\\\Cache\\\\Adapter\\\\AdapterInterface\\:\\:reset\\(\\)\\.$#"
225-
count: 2
225+
count: 1
226226
path: tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php
227227

228228
-

src/Tracing/Cache/TraceableCacheAdapterTrait.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -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::prune() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, PruneableInterface::class));
162+
return false;
163163
}
164164

165165
return $this->decoratedAdapter->prune();
@@ -171,11 +171,9 @@ public function prune(): bool
171171
*/
172172
public function reset(): void
173173
{
174-
if (!$this->decoratedAdapter instanceof ResettableInterface) {
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));
174+
if ($this->decoratedAdapter instanceof ResettableInterface) {
175+
$this->decoratedAdapter->reset();
176176
}
177-
178-
$this->decoratedAdapter->reset();
179177
}
180178

181179
/**

tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,24 @@ public function testPrune(): void
375375
$this->assertNotNull($spans[1]->getEndTimestamp());
376376
}
377377

378-
public function testPruneThrowsExceptionIfDecoratedAdapterIsNotPruneable(): void
378+
public function testPruneReturnsFalseIfDecoratedAdapterIsNotPruneable(): void
379379
{
380+
$transaction = new Transaction(new TransactionContext(), $this->hub);
381+
$transaction->initSpanRecorder();
382+
383+
$this->hub->expects($this->once())
384+
->method('getSpan')
385+
->willReturn($transaction);
386+
380387
$adapter = $this->createCacheAdapter($this->createMock(static::getAdapterClassFqcn()));
381388

382-
$this->expectException(\BadMethodCallException::class);
383-
$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)));
389+
$this->assertFalse($adapter->prune());
384390

385-
$adapter->prune();
391+
$spans = $transaction->getSpanRecorder()->getSpans();
392+
393+
$this->assertCount(2, $spans);
394+
$this->assertSame('cache.prune', $spans[1]->getOp());
395+
$this->assertNotNull($spans[1]->getEndTimestamp());
386396
}
387397

388398
public function testReset(): void
@@ -395,16 +405,6 @@ public function testReset(): void
395405
$adapter->reset();
396406
}
397407

398-
public function testResetThrowsExceptionIfDecoratedAdapterIsNotResettable(): void
399-
{
400-
$adapter = $this->createCacheAdapter($this->createMock(static::getAdapterClassFqcn()));
401-
402-
$this->expectException(\BadMethodCallException::class);
403-
$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)));
404-
405-
$adapter->reset();
406-
}
407-
408408
private static function isCachePackageInstalled(): bool
409409
{
410410
return interface_exists(BaseCacheInterface::class);

0 commit comments

Comments
 (0)