-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalCacheFactoryTest.php
More file actions
91 lines (63 loc) · 2.3 KB
/
Copy pathLocalCacheFactoryTest.php
File metadata and controls
91 lines (63 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Featurit\Client\Tests;
use Featurit\Client\LocalCacheFactory;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
class LocalCacheFactoryTest extends TestCase
{
const TEST_CACHE_DIR = "cache_test";
private $testCacheDir = "";
protected function setUp(): void
{
$this->testCacheDir = join(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', self::TEST_CACHE_DIR]);
}
protected function tearDown(): void
{
$this->deleteDirectory($this->testCacheDir);
}
private function deleteDirectory($dir): bool
{
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
public function test_local_cache_factory_returns_a_cache_interface_instance(): void
{
$localCacheFactory = new LocalCacheFactory();
$localCache = $localCacheFactory->setLocalCache(1, self::TEST_CACHE_DIR);
$this->assertInstanceOf(CacheInterface::class, $localCache);
}
public function test_returned_cache_works_properly(): void
{
$localCacheFactory = new LocalCacheFactory();
$localCache = $localCacheFactory->setLocalCache(5, self::TEST_CACHE_DIR);
$localCache->set('test_key', 'test_value');
$value = $localCache->get('test_key');
$this->assertEquals('test_value', $value);
}
public function test_returned_cache_expires_after_1_minute(): void
{
$localCacheFactory = new LocalCacheFactory();
$localCache = $localCacheFactory->setLocalCache(1, self::TEST_CACHE_DIR);
$localCache->set('test_key_2', 'test_value');
$localCache = $localCacheFactory->setLocalCache(1, self::TEST_CACHE_DIR);
$value = $localCache->get('test_key_2');
$this->assertEquals('test_value', $value);
sleep(1 * 60);
$localCache = $localCacheFactory->setLocalCache(1, self::TEST_CACHE_DIR);
$value = $localCache->get('test_key_2');
$this->assertNull($value);
}
}