|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koded\Caching\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Caching\CacheException; |
|
6
|
|
|
use Koded\Caching\Configuration\ConfigFactory; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class ClientFactoryTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public function test_should_create_memory_client_without_configuration() |
|
13
|
|
|
{ |
|
14
|
|
|
$client = (new CacheClientFactory(new ConfigFactory))->new(); |
|
15
|
|
|
$this->assertInstanceOf(MemoryClient::class, $client); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function test_should_create_memcached_client() |
|
19
|
|
|
{ |
|
20
|
|
|
if (false === extension_loaded('Memcached')) { |
|
21
|
|
|
$this->markTestSkipped('Memcached is not installed on this environment.'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$client = (new CacheClientFactory(new ConfigFactory))->new('memcached'); |
|
25
|
|
|
$this->assertInstanceOf(MemcachedClient::class, $client); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @depends test_should_create_memcached_client |
|
30
|
|
|
*/ |
|
31
|
|
|
public function test_should_create_memcached_client_with_ttl() |
|
32
|
|
|
{ |
|
33
|
|
|
if (false === extension_loaded('Memcached')) { |
|
34
|
|
|
$this->markTestSkipped('Memcached is not installed on this environment.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$client = (new CacheClientFactory(new ConfigFactory(['ttl' => 120])))->new('memcached'); |
|
38
|
|
|
|
|
39
|
|
|
$r = new \ReflectionClass($client); |
|
40
|
|
|
$ttl = $r->getProperty('ttl'); |
|
41
|
|
|
$ttl->setAccessible(true); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertSame(120, $ttl->getValue($client)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function test_should_create_redis_client() |
|
47
|
|
|
{ |
|
48
|
|
|
if (false === extension_loaded('redis')) { |
|
49
|
|
|
$this->markTestSkipped('Redis is not installed on this environment.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$client = (new CacheClientFactory(new ConfigFactory([ |
|
53
|
|
|
'host' => getenv('REDIS_SERVER_HOST'), |
|
54
|
|
|
'port' => getenv('REDIS_SERVER_PORT'), |
|
55
|
|
|
|
|
56
|
|
|
'auth' => 'fubar', |
|
57
|
|
|
'binary' => 'msgpack' |
|
58
|
|
|
])))->new('redis'); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertInstanceOf(RedisClient::class, $client); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function test_should_create_predis_client() |
|
64
|
|
|
{ |
|
65
|
|
|
$client = (new CacheClientFactory(new ConfigFactory([ |
|
66
|
|
|
'host' => getenv('REDIS_SERVER_HOST'), |
|
67
|
|
|
'port' => getenv('REDIS_SERVER_PORT'), |
|
68
|
|
|
])))->new('predis'); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertInstanceOf(PredisClient::class, $client); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function test_should_create_file_client() |
|
74
|
|
|
{ |
|
75
|
|
|
$client = (new CacheClientFactory(new ConfigFactory))->new('file'); |
|
76
|
|
|
$this->assertInstanceOf(FileClient::class, $client); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function test_should_create_memory_client() |
|
80
|
|
|
{ |
|
81
|
|
|
$client = (new CacheClientFactory(new ConfigFactory))->new('memory'); |
|
82
|
|
|
$this->assertInstanceOf(MemoryClient::class, $client); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function test_non_supported_logger_exception() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->expectException(CacheException::class); |
|
88
|
|
|
$this->expectExceptionMessage('The cache logger should be NULL or an instance of Psr\Log\LoggerInterface, given Closure'); |
|
89
|
|
|
|
|
90
|
|
|
(new CacheClientFactory(new ConfigFactory([ |
|
91
|
|
|
'logger' => function() { } |
|
92
|
|
|
])))->new('file'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|