forked from TruCopilot/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAtomicOperations.test.php
More file actions
130 lines (107 loc) · 3.7 KB
/
AtomicOperations.test.php
File metadata and controls
130 lines (107 loc) · 3.7 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <contact@geolim4.com>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
use Phpfastcache\CacheManager;
use Phpfastcache\Tests\Helper\TestHelper;
chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/mock/Autoload.php';
$testHelper = new TestHelper('Atomic Operations');
$pool = CacheManager::getInstance('Memstatic');
$testHelper->printInfoText('Testing APPEND/PREPEND methods...');
{
$cacheItem = $pool->getItem($testHelper->getRandomKey());
$cacheItem->set(['alpha', 'bravo']);
$cacheItem->append('charlie');
$pool->save($cacheItem);
$target = ['alpha', 'bravo', 'charlie'];
if(count(array_intersect($cacheItem->get(), $target)) === count($target)){
$testHelper->assertPass('Atomic operation APPEND on ARRAY works as expected');
} else {
$testHelper->assertFail('Atomic operation APPEND on ARRAY did not worked as expected');
}
}
// Reset pool
unset($target, $cacheItem);
$pool->clear();
{
$cacheItem = $pool->getItem($testHelper->getRandomKey());
$cacheItem->set('alpha_bravo');
$cacheItem->append('_charlie');
$pool->save($cacheItem);
$target = 'alpha_bravo_charlie';
if($cacheItem->get() === $target){
$testHelper->assertPass('Atomic operation APPEND on STRING works as expected');
} else {
$testHelper->assertFail('Atomic operation APPEND on STRING did not worked as expected');
}
}
// Reset pool
unset($target, $cacheItem);
$pool->clear();
{
$cacheItem = $pool->getItem($testHelper->getRandomKey());
$cacheItem->set(['bravo', 'charlie']);
$cacheItem->prepend('alpha');
$pool->save($cacheItem);
$target = ['alpha', 'bravo', 'charlie'];
if(count(array_intersect($cacheItem->get(), $target)) === count($target)){
$testHelper->assertPass('Atomic operation PREPEND on ARRAY works as expected');
} else {
$testHelper->assertFail('Atomic operation PREPEND on ARRAY did not worked as expected');
}
}
// Reset pool
unset($target, $cacheItem);
$pool->clear();
{
$cacheItem = $pool->getItem($testHelper->getRandomKey());
$cacheItem->set('bravo_charlie');
$cacheItem->prepend('alpha_');
$pool->save($cacheItem);
$target = 'alpha_bravo_charlie';
if($cacheItem->get() === $target){
$testHelper->assertPass('Atomic operation PREPEND on STRING works as expected');
} else {
$testHelper->assertFail('Atomic operation PREPEND on STRING did not worked as expected');
}
}
// Reset pool
unset($target, $cacheItem);
$pool->clear();
$testHelper->printInfoText('Testing INCREMENT/DECREMENT methods...');
{
$cacheItem = $pool->getItem($testHelper->getRandomKey());
$cacheItem->set(1330);
$cacheItem->increment(3 + 4);
$pool->save($cacheItem);
if($cacheItem->get() === 1337){
$testHelper->assertPass('Atomic operation INCREMENT on INT works as expected');
} else {
$testHelper->assertFail('Atomic operation INCREMENT on INT did not worked as expected');
}
}
// Reset pool
unset($target, $cacheItem);
$pool->clear();
{
$cacheItem = $pool->getItem($testHelper->getRandomKey());
$cacheItem->set(1340);
$cacheItem->decrement(4 - 1);
$pool->save($cacheItem);
if($cacheItem->get() === 1337){
$testHelper->assertPass('Atomic operation DECREMENT on INT works as expected');
} else {
$testHelper->assertFail('Atomic operation DECREMENT on INT did not worked as expected');
}
}
$testHelper->terminateTest();