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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Concurrency;
use Piwik\Common;
use Piwik\Concurrency\DistributedList;
use Piwik\Date;
use Piwik\Db;
use Piwik\Option;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group Core
*/
class DistributedListTest extends IntegrationTestCase
{
const TEST_OPTION_NAME = 'test.distributed.list';
public static $defaultOptionValues = array(
'val1',
'val2',
'val3',
'val4'
);
/**
* @var DistributedList
*/
private $distributedList;
public function setUp(): void
{
parent::setUp();
$this->distributedList = new DistributedList(self::TEST_OPTION_NAME);
$this->initOptionValue();
}
public function test_getAll_CorrectlyReturnsItemsInOption()
{
$list = $this->distributedList->getAll();
$this->assertEquals(self::$defaultOptionValues, $list);
}
public function test_getAll_ReturnsValueInOption_IfOptionCacheHasSeparateValue()
{
// get option so cache is loaded
Option::get(self::TEST_OPTION_NAME);
// set option value to something else
$newList = array('1', '2', '3');
$this->initOptionValue($newList);
// test option is now different
$list = $this->distributedList->getAll();
$this->assertEquals($newList, $list);
}
public function test_setAll_CorrectlySetsNormalListInOption()
{
$newList = array('1', '2', '3');
$this->distributedList->setAll($newList);
$optionValue = $this->getOptionValueForList();
$this->assertEquals(serialize($newList), $optionValue);
$list = $this->distributedList->getAll();
$this->assertEquals($newList, $list);
}
public function test_setAll_CorrectlyConvertsItemsToString_BeforePersistingToOption()
{
$newList = array('1', Date::factory('2015-02-03'), 4.5);
$this->distributedList->setAll($newList);
$optionValue = $this->getOptionValueForList();
$expectedOptionList = array('1', '2015-02-03', '4.5');
$this->assertEquals(serialize($expectedOptionList), $optionValue);
$list = $this->distributedList->getAll();
$this->assertEquals($expectedOptionList, $list);
}
public function test_add_AddsOneItemToList_InOptionTable_IfItemIsNotArray()
{
$this->distributedList->add('val5');
$expectedOptionList = array('val1', 'val2', 'val3', 'val4', 'val5');
$this->assertEquals(serialize($expectedOptionList), $this->getOptionValueForList());
}
public function test_add_AddsMultipleItemsToList_InOptionTable_IfItemsIsArray()
{
$this->distributedList->add(array('val5', Date::factory('2015-03-04')));
$expectedOptionList = array('val1', 'val2', 'val3', 'val4', 'val5', '2015-03-04');
$this->assertEquals(serialize($expectedOptionList), $this->getOptionValueForList());
}
public function test_remove_RemovesSingleItemByValue_InOptionTable_IfItemIsNotArray()
{
$this->distributedList->remove('val2');
$expectedOptionList = array('val1', 'val3', 'val4');
$this->assertEquals(serialize($expectedOptionList), $this->getOptionValueForList());
}
public function test_remove_RemovesMultipleItemsByValue_InOptionTable_IfItemIsArray()
{
$this->distributedList->remove(array('val2', 'val4'));
$expectedOptionList = array('val1', 'val3');
$this->assertEquals(serialize($expectedOptionList), $this->getOptionValueForList());
}
public function test_removeByIndex_RemovesSingleItemByIndex_InOptionTable_IfArgIsIndex()
{
$this->distributedList->removeByIndex(2);
$expectedOptionList = array('val1', 'val2', 'val4');
$this->assertEquals(serialize($expectedOptionList), $this->getOptionValueForList());
}
public function test_removeByIndex_RemovesMultipleItemsByIndex_InOptionTable_IfArgIsArray()
{
$this->distributedList->removeByIndex(array(1, 3));
$expectedOptionList = array('val1', 'val3');
$this->assertEquals(serialize($expectedOptionList), $this->getOptionValueForList());
}
private function initOptionValue($data = false)
{
$data = $data ?: self::$defaultOptionValues;
$optionTable = Common::prefixTable('option');
Db::query("INSERT INTO `$optionTable` (option_name, option_value, autoload) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE option_value = ?",
array(self::TEST_OPTION_NAME, serialize($data), 0, serialize($data)));
}
private function getOptionValueForList()
{
$optionTable = Common::prefixTable('option');
return Db::fetchOne("SELECT option_value FROM `$optionTable` WHERE option_name = ?", array(self::TEST_OPTION_NAME));
}
}
|