1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koded\Caching; |
4
|
|
|
|
5
|
|
|
use Koded\Caching\Client\CacheClientFactory; |
6
|
|
|
use Koded\Caching\Configuration\ConfigFactory; |
7
|
|
|
use Koded\Stdlib\Interfaces\Serializer; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class RedisWithOnlyJsonSerializerTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
use SimpleCacheTestCaseTrait; |
13
|
|
|
|
14
|
|
|
public function test_should_return_redis_client() |
15
|
|
|
{ |
16
|
|
|
$this->assertInstanceOf(\Redis::class, $this->cache->client()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @dataProvider simpleData |
21
|
|
|
* |
22
|
|
|
* @param $data |
23
|
|
|
*/ |
24
|
|
|
public function test_set_multiple_values($data) |
25
|
|
|
{ |
26
|
|
|
$saved = $this->cache->setMultiple($data); |
27
|
|
|
$this->assertTrue($saved); |
28
|
|
|
$this->assertSame('foo', $this->cache->get('key1')); |
29
|
|
|
$this->assertSame(false, $this->cache->get('key2')); |
30
|
|
|
|
31
|
|
|
// arrays, objects are lost |
32
|
|
|
|
33
|
|
|
$lostArray = new \stdClass; |
34
|
|
|
$lostArray->bar = true; |
35
|
|
|
|
36
|
|
|
$lostObject = new \stdClass; |
37
|
|
|
$lostObject->foo = 'bar'; |
38
|
|
|
|
39
|
|
|
$this->assertEquals($lostArray, $this->cache->get('key3'), |
40
|
|
|
'The serialized associative array is unserialized as stdClass'); |
41
|
|
|
|
42
|
|
|
$this->assertEquals($lostObject, $this->cache->get('key4'), |
43
|
|
|
'The serialized Arguments() object is lost and unserialized as stdClass'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @overridden |
48
|
|
|
* @dataProvider simpleData |
49
|
|
|
*/ |
50
|
|
|
public function test_should_store_and_retrieve_the_same_cache_item($data) |
51
|
|
|
{ |
52
|
|
|
$result = $this->cache->set('foo', $data); |
53
|
|
|
$this->assertTrue($result); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf(\stdClass::class, $this->cache->get('foo'), |
56
|
|
|
'JSON unserialized data is now stcClass (because original is associative array)'); |
57
|
|
|
|
58
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $this->cache->get('foo')->key5, |
59
|
|
|
'PHP indexed array is correctly unserialized'); |
60
|
|
|
|
61
|
|
|
$this->assertNotEquals(['foo' => 'bar'], $this->cache->get('foo')->key4, |
62
|
|
|
'PHP associative array is unserialized into stdClass'); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(\stdClass::class, $this->cache->get('foo')->key4, |
65
|
|
|
'json_encode() creates JS object from PHP assoc array'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
protected function setUp(): void |
70
|
|
|
{ |
71
|
|
|
$this->markTestSkipped('Redis JSON serializer skipped for now...'); |
72
|
|
|
|
73
|
|
|
if (false === extension_loaded('redis')) { |
74
|
|
|
$this->markTestSkipped('Redis extension is not loaded.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->cache = (new CacheClientFactory(new ConfigFactory([ |
78
|
|
|
'host' => getenv('REDIS_SERVER_HOST'), |
79
|
|
|
'port' => getenv('REDIS_SERVER_PORT'), |
80
|
|
|
|
81
|
|
|
'serializer' => Serializer::JSON, |
82
|
|
|
'binary' => false, |
83
|
|
|
|
84
|
|
|
])))->new('redis'); |
85
|
|
|
|
86
|
|
|
$this->cache->clear(); |
87
|
|
|
} |
88
|
|
|
} |