|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Tests\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
6
|
|
|
use SilverStripe\Core\Cache\ApcuCacheFactory; |
|
7
|
|
|
use SilverStripe\Core\Cache\MemcachedCacheFactory; |
|
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
9
|
|
|
use SilverStripe\Core\Test\Cache\CacheTest\MockCache; |
|
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
11
|
|
|
use Symfony\Component\Cache\Simple\ApcuCache; |
|
12
|
|
|
use Symfony\Component\Cache\Simple\MemcachedCache; |
|
13
|
|
|
|
|
14
|
|
|
class CacheTest extends SapphireTest |
|
15
|
|
|
{ |
|
16
|
|
|
protected function setUp() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::setUp(); |
|
19
|
|
|
|
|
20
|
|
|
Injector::inst() |
|
|
|
|
|
|
21
|
|
|
->load([ |
|
22
|
|
|
ApcuCacheFactory::class => [ |
|
23
|
|
|
'constructor' => [ 'version' => 'ss40test' ] |
|
24
|
|
|
], |
|
25
|
|
|
MemcachedCacheFactory::class => MemcachedCacheFactory::class, |
|
26
|
|
|
CacheInterface::class . '.TestApcuCache' => [ |
|
27
|
|
|
'factory' => ApcuCacheFactory::class, |
|
28
|
|
|
'constructor' => [ |
|
29
|
|
|
'namespace' => 'TestApcuCache', |
|
30
|
|
|
'defaultLifetime' => 2600, |
|
31
|
|
|
], |
|
32
|
|
|
], |
|
33
|
|
|
CacheInterface::class . '.TestMemcache' => [ |
|
34
|
|
|
'factory' => MemcachedCacheFactory::class, |
|
35
|
|
|
'constructor' => [ |
|
36
|
|
|
'namespace' => 'TestMemCache', |
|
37
|
|
|
'defaultLifetime' => 5600, |
|
38
|
|
|
], |
|
39
|
|
|
], |
|
40
|
|
|
ApcuCache::class => MockCache::class, |
|
41
|
|
|
MemcachedCache::class => MockCache::class, |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
public function testApcuCacheFactory() |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$cache = Injector::inst()->get(CacheInterface::class .'.TestApcuCache'); |
|
48
|
|
|
$this->assertInstanceOf( |
|
49
|
|
|
MockCache::class, |
|
50
|
|
|
$cache |
|
51
|
|
|
); |
|
52
|
|
|
$this->assertEquals( |
|
53
|
|
|
[ |
|
54
|
|
|
'TestApcuCache_'.md5(BASE_PATH), |
|
55
|
|
|
2600, |
|
56
|
|
|
'ss40test' |
|
57
|
|
|
], |
|
58
|
|
|
$cache->getArgs() |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
public function testMemCacheFactory() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$cache = Injector::inst()->get(CacheInterface::class .'.TestMemcache'); |
|
65
|
|
|
$this->assertInstanceOf( |
|
66
|
|
|
MockCache::class, |
|
67
|
|
|
$cache |
|
68
|
|
|
); |
|
69
|
|
|
$this->assertEquals( |
|
70
|
|
|
[ |
|
71
|
|
|
null, |
|
72
|
|
|
'TestMemCache_'.md5(BASE_PATH), |
|
73
|
|
|
5600 |
|
74
|
|
|
], |
|
75
|
|
|
$cache->getArgs() |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: