|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spiral\Tests\Cache\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Mockery as m; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Spiral\Cache\CacheManager; |
|
11
|
|
|
use Spiral\Cache\Config\CacheConfig; |
|
12
|
|
|
use Spiral\Cache\Core\CacheInjector; |
|
13
|
|
|
use Spiral\Cache\Storage\ArrayStorage; |
|
14
|
|
|
use Spiral\Core\Exception\Container\NotFoundException; |
|
15
|
|
|
use Spiral\Core\FactoryInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class CacheInjectorTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
private ?CacheInterface $defaultCache = null; |
|
20
|
|
|
|
|
21
|
|
|
public function testGetByContext(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$injector = $this->createInjector(); |
|
24
|
|
|
$reflection = new ReflectionClass(ArrayStorage::class); |
|
25
|
|
|
|
|
26
|
|
|
$result = $injector->createInjection($reflection, 'array'); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertInstanceOf(ArrayStorage::class, $result); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testGetByIncorrectContext(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$injector = $this->createInjector(); |
|
34
|
|
|
$reflection = new ReflectionClass(CacheInterface::class); |
|
35
|
|
|
|
|
36
|
|
|
$result = $injector->createInjection($reflection, 'userCache'); |
|
37
|
|
|
|
|
38
|
|
|
// The default connection should be returned |
|
39
|
|
|
$this->assertSame($this->defaultCache, $result); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testBadArgumentTypeException(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$injector = $this->createInjector(); |
|
45
|
|
|
$this->expectException(RuntimeException::class); |
|
46
|
|
|
$this->expectExceptionMessage('The cache obtained by the context'); |
|
47
|
|
|
|
|
48
|
|
|
$reflection = new ReflectionClass(ArrayStorage::class); |
|
49
|
|
|
$injector->createInjection($reflection, 'cache'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function createInjector(): CacheInjector |
|
53
|
|
|
{ |
|
54
|
|
|
$this->defaultCache = m::mock(CacheInterface::class); |
|
55
|
|
|
$config = new CacheConfig([ |
|
56
|
|
|
'default' => 'test', |
|
57
|
|
|
'aliases' => [ |
|
58
|
|
|
'config-cache' => 'roadrunner', |
|
59
|
|
|
'routes-cache' => 'array', |
|
60
|
|
|
'cache' => 'test', |
|
61
|
|
|
'test' => 'test', |
|
62
|
|
|
], |
|
63
|
|
|
'typeAliases' => [], |
|
64
|
|
|
'storages' => [ |
|
65
|
|
|
'array' => [ |
|
66
|
|
|
'type' => 'array', |
|
67
|
|
|
], |
|
68
|
|
|
'test' => [ |
|
69
|
|
|
'type' => 'test', |
|
70
|
|
|
], |
|
71
|
|
|
], |
|
72
|
|
|
]); |
|
73
|
|
|
$factory = m::mock(FactoryInterface::class); |
|
74
|
|
|
$factory->shouldReceive('make')->andReturnUsing(function (string $name): CacheInterface { |
|
|
|
|
|
|
75
|
|
|
$result = [ |
|
76
|
|
|
'test' => $this->defaultCache, |
|
77
|
|
|
'array' => new ArrayStorage(), |
|
78
|
|
|
][$name] ?? null; |
|
79
|
|
|
if ($result === null) { |
|
80
|
|
|
throw new NotFoundException(); |
|
81
|
|
|
} |
|
82
|
|
|
return $result; |
|
83
|
|
|
}); |
|
84
|
|
|
$manager = new CacheManager($config, $factory); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
return new CacheInjector($manager, $config); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.