Skip to content

Commit d4df050

Browse files
authored
Merge pull request #27 from bc-luke/big-31502-super-reflection-cache-p
PHPMNT-100 Ensure CachingClassInspector works with no-op cache
2 parents 635b1e6 + 50cbc7e commit d4df050

2 files changed

Lines changed: 87 additions & 19 deletions

File tree

src/Reflection/CachingClassInspector.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Bigcommerce\Injector\Reflection;
66

77
use Bigcommerce\Injector\Cache\ServiceCacheInterface;
8-
use ReflectionClass;
98
use ReflectionException;
109

1110
class CachingClassInspector implements ClassInspectorInterface
@@ -44,14 +43,14 @@ public function warmCache(string $class, string $method): void
4443
public function classHasMethod(string $class, string $method): bool
4544
{
4645
$key = "$class::{$method}::exists";
47-
if (!$this->serviceCache->has($key)) {
48-
$this->serviceCache->set(
49-
$key,
50-
$this->classInspector->classHasMethod($class, $method),
51-
);
46+
if ($this->serviceCache->has($key)) {
47+
$value = $this->serviceCache->get($key);
48+
} else {
49+
$value = $this->classInspector->classHasMethod($class, $method);
50+
$this->serviceCache->set($key, $value);
5251
}
5352

54-
return $this->serviceCache->get($key);
53+
return $value;
5554
}
5655

5756
/**
@@ -65,14 +64,14 @@ public function classHasMethod(string $class, string $method): bool
6564
public function methodIsPublic(string $class, string $method): bool
6665
{
6766
$key = "$class::{$method}::is_public";
68-
if (!$this->serviceCache->has($key)) {
69-
$this->serviceCache->set(
70-
$key,
71-
$this->classInspector->methodIsPublic($class, $method),
72-
);
67+
if ($this->serviceCache->has($key)) {
68+
$value = $this->serviceCache->get($key);
69+
} else {
70+
$value = $this->classInspector->methodIsPublic($class, $method);
71+
$this->serviceCache->set($key, $value);
7372
}
7473

75-
return $this->serviceCache->get($key);
74+
return $value;
7675
}
7776

7877
/**
@@ -86,14 +85,14 @@ public function methodIsPublic(string $class, string $method): bool
8685
public function getMethodSignature(string $class, string $method): array
8786
{
8887
$key = "$class::{$method}::signature";
89-
if (!$this->serviceCache->has($key)) {
90-
$this->serviceCache->set(
91-
$key,
92-
$this->classInspector->getMethodSignature($class, $method),
93-
);
88+
if ($this->serviceCache->has($key)) {
89+
$value = $this->serviceCache->get($key);
90+
} else {
91+
$value = $this->classInspector->getMethodSignature($class, $method);
92+
$this->serviceCache->set($key, $value);
9493
}
9594

96-
return $this->serviceCache->get($key);
95+
return $value;
9796
}
9897

9998
public function getStats(): ClassInspectorStats
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Reflection;
6+
7+
use Bigcommerce\Injector\Cache\ArrayServiceCache;
8+
use Bigcommerce\Injector\Cache\ServiceCacheInterface;
9+
use Bigcommerce\Injector\Reflection\CachingClassInspector;
10+
use Bigcommerce\Injector\Reflection\ClassInspector;
11+
use PHPUnit\Framework\TestCase;
12+
use Prophecy\Argument;
13+
use Prophecy\PhpUnit\ProphecyTrait;
14+
use Prophecy\Prophecy\ObjectProphecy;
15+
use Tests\Dummy\DummyDependency;
16+
17+
class NoOpCachingClassInspectorTest extends TestCase
18+
{
19+
use ProphecyTrait;
20+
21+
private CachingClassInspector $subject;
22+
private ArrayServiceCache|ObjectProphecy $serviceCache;
23+
private ClassInspector|ObjectProphecy $classInspector;
24+
25+
protected function setUp(): void
26+
{
27+
parent::setUp();
28+
$serviceCache = $this->prophesize(ServiceCacheInterface::class);
29+
$serviceCache->has(Argument::cetera())->willReturn(false);
30+
$serviceCache->get(Argument::cetera())->willReturn(false);
31+
32+
$this->serviceCache = $serviceCache;
33+
$this->classInspector = $this->prophesize(ClassInspector::class);
34+
$this->subject = new CachingClassInspector(
35+
$this->classInspector->reveal(),
36+
$this->serviceCache->reveal(),
37+
);
38+
}
39+
40+
public function testClassHasMethodWorksWithNoOpCache(): void
41+
{
42+
$this->classInspector->classHasMethod(Argument::cetera())->willReturn(true);
43+
$this->serviceCache->set(Argument::cetera())->shouldBeCalled();
44+
45+
$hasMethod = $this->subject->classHasMethod(DummyDependency::class, 'isEnabled');
46+
47+
$this->assertTrue($hasMethod);
48+
}
49+
50+
public function testMethodIsPublicWorksWithNoOpCache(): void
51+
{
52+
$this->classInspector->methodIsPublic(Argument::cetera())->willReturn(true);
53+
$this->serviceCache->set(Argument::cetera())->shouldBeCalled();
54+
55+
$public = $this->subject->methodIsPublic(DummyDependency::class, 'isEnabled');
56+
57+
$this->assertTrue($public);
58+
}
59+
60+
public function testGetMethodSignatureWorksWithNoOpCache(): void
61+
{
62+
$this->classInspector->getMethodSignature(Argument::cetera())->willReturn([]);
63+
$this->serviceCache->set(Argument::cetera())->shouldBeCalled();
64+
65+
$signature = $this->subject->getMethodSignature(DummyDependency::class, 'isEnabled');
66+
67+
$this->assertEquals([], $signature);
68+
}
69+
}

0 commit comments

Comments
 (0)