1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSFS\tests\base; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use PSFS\base\Cache; |
7
|
|
|
use PSFS\base\config\Config; |
8
|
|
|
use PSFS\base\exception\GeneratorException; |
9
|
|
|
use PSFS\base\Security; |
10
|
|
|
use PSFS\base\types\helpers\FileHelper; |
11
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CacheTest |
15
|
|
|
* @package PSFS\tests\base |
16
|
|
|
*/ |
17
|
|
|
class CacheTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Function to test the instance of Cache class |
21
|
|
|
* @return Cache |
22
|
|
|
*/ |
23
|
|
|
private function getInstance(): Cache |
24
|
|
|
{ |
25
|
|
|
FileHelper::deleteDir(CACHE_DIR); |
26
|
|
|
GeneratorHelper::createDir(CACHE_DIR); |
27
|
|
|
Cache::dropInstance(); |
28
|
|
|
$cache = Cache::getInstance(); |
29
|
|
|
|
30
|
|
|
$this->assertNotNull($cache); |
31
|
|
|
$this->assertInstanceOf(Cache::class, $cache, 'Instance different from expected'); |
32
|
|
|
|
33
|
|
|
return $cache; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test for basic usage of cache |
38
|
|
|
* @covers |
39
|
|
|
*/ |
40
|
|
|
public function testCacheUse() |
41
|
|
|
{ |
42
|
|
|
$cache = $this->getInstance(); |
43
|
|
|
|
44
|
|
|
// Test data |
45
|
|
|
$data = [uniqid('test', true) => microtime()]; |
46
|
|
|
$hash = sha1(microtime()); |
47
|
|
|
|
48
|
|
|
// TXT cache test |
49
|
|
|
$cache->storeData('tests' . DIRECTORY_SEPARATOR . $hash, json_encode($data)); |
50
|
|
|
$this->assertFileExists(CACHE_DIR . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . $hash, 'Cache not stored!!'); |
51
|
|
|
|
52
|
|
|
// Gather cache data from file without transformation |
53
|
|
|
$cachedData = $cache->readFromCache('tests' . DIRECTORY_SEPARATOR . $hash, 300, function () { |
54
|
|
|
}); |
55
|
|
|
$this->assertEquals($cachedData, json_encode($data), 'Different data cached!'); |
56
|
|
|
|
57
|
|
|
// Gather cache data from file with JSON transformation |
58
|
|
|
$cache->storeData('tests' . DIRECTORY_SEPARATOR . $hash, $data, Cache::JSONGZ); |
59
|
|
|
$cachedData = $cache->readFromCache('tests' . DIRECTORY_SEPARATOR . $hash, 300, function () { |
60
|
|
|
}, Cache::JSONGZ); |
61
|
|
|
$this->assertEquals($cachedData, $data, 'Error when try to gather cache with JSON transform'); |
62
|
|
|
|
63
|
|
|
// Gather cache data from expired file |
64
|
|
|
sleep(2); |
65
|
|
|
$cachedData = $cache->readFromCache('tests' . DIRECTORY_SEPARATOR . $hash, 1, function () use ($data) { |
66
|
|
|
return $data; |
67
|
|
|
}, Cache::JSONGZ); |
68
|
|
|
$this->assertEquals($cachedData, $data, 'Error when try to gather cache with JSON transform'); |
69
|
|
|
|
70
|
|
|
FileHelper::deleteDir(CACHE_DIR . DIRECTORY_SEPARATOR . 'test'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function prepareTestVariables(): array |
74
|
|
|
{ |
75
|
|
|
$session = Security::getInstance(); |
76
|
|
|
$hash = sha1(microtime()); |
77
|
|
|
$session->setSessionKey('__CACHE__', [ |
78
|
|
|
'cache' => 600, |
79
|
|
|
'params' => [], |
80
|
|
|
'http' => 'GET', |
81
|
|
|
'slug' => 'test-' . $hash, |
82
|
|
|
'class' => 'Test', |
83
|
|
|
'method' => 'test' . ucfirst($hash), |
84
|
|
|
'module' => 'TEST', |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
return Cache::getInstance()->getRequestCacheHash(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test for specific cache functionality in requests |
92
|
|
|
* @covers |
93
|
|
|
*/ |
94
|
|
|
public function testCacheForRequests() |
95
|
|
|
{ |
96
|
|
|
list($path, $hash) = $this->prepareTestVariables(); |
97
|
|
|
$this->assertNotNull($hash, 'Invalid cache hash'); |
98
|
|
|
$this->assertNotEmpty($path, 'Invalid path to save the cache'); |
99
|
|
|
|
100
|
|
|
$config = Config::getInstance()->dumpConfig(); |
101
|
|
|
$cache_data_config = Config::getParam('cache.data.enable'); |
102
|
|
|
Config::save([], [ |
103
|
|
|
'label' => ['cache.data.enable'], |
104
|
|
|
'value' => [true] |
105
|
|
|
]); |
106
|
|
|
Config::getInstance()->setDebugMode(false); |
107
|
|
|
$this->assertNotFalse(Cache::needCache(), 'Test url expired or error checking cache'); |
108
|
|
|
|
109
|
|
|
// Flushing cache |
110
|
|
|
Cache::getInstance()->flushCache(); |
111
|
|
|
$this->assertDirectoryDoesNotExist(CACHE_DIR . DIRECTORY_SEPARATOR . $path, 'Cache directory not deleted properly'); |
112
|
|
|
|
113
|
|
|
$config['cache.data.enable'] = $cache_data_config; |
114
|
|
|
Config::save($config, []); |
115
|
|
|
|
116
|
|
|
// Cleaning test data |
117
|
|
|
FileHelper::deleteDir(CACHE_DIR . DIRECTORY_SEPARATOR . 'tests'); |
118
|
|
|
$this->assertDirectoryDoesNotExist(CACHE_DIR . DIRECTORY_SEPARATOR . 'tests', 'Test data directory not cleaned properly'); |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Test privileges in folder |
124
|
|
|
* @covers |
125
|
|
|
* @throws GeneratorException |
126
|
|
|
*/ |
127
|
|
|
public function testPrivileges() |
128
|
|
|
{ |
129
|
|
|
list($path, $hash) = $this->prepareTestVariables(); |
130
|
|
|
GeneratorHelper::createDir(dirname(CACHE_DIR . DIRECTORY_SEPARATOR . $path)); |
131
|
|
|
GeneratorHelper::createDir(CACHE_DIR . DIRECTORY_SEPARATOR . $path); |
132
|
|
|
$cache = $this->getInstance(); |
133
|
|
|
chmod(realpath(CACHE_DIR . DIRECTORY_SEPARATOR . $path), 0600); |
134
|
|
|
$cache->storeData($path . $hash, json_encode('')); |
135
|
|
|
chmod(realpath(CACHE_DIR . DIRECTORY_SEPARATOR . $path), 0777); |
136
|
|
|
FileHelper::deleteDir(CACHE_DIR . DIRECTORY_SEPARATOR . $path); |
137
|
|
|
chmod(realpath(CACHE_DIR . DIRECTORY_SEPARATOR . 'TEST'), 0777); |
138
|
|
|
FileHelper::deleteDir(CACHE_DIR . DIRECTORY_SEPARATOR . 'TEST'); |
139
|
|
|
} |
140
|
|
|
} |