-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleDatastoreTest.php
168 lines (143 loc) · 5.29 KB
/
simpleDatastoreTest.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
require(dirname(__FILE__) . '/../src/simpleDatastore.php');
use meyersm\simpleDatastore;
class simpleDatastoreTest extends PHPUnit_Framework_TestCase
{
public function testCreateNewDatastore()
{
$datastore = new simpleDatastore("teststore");
$datastore->lobsters = array("bob","archibold","thomas");
$datastore->lobster_max_power = 50;
$ret = $datastore->save();
$datastore->close();
$this->assertGreaterThan(0,$ret);
}
/**
* @depends testCreateNewDatastore
*/
public function testLoadDatastore()
{
$datastore = new simpleDatastore("teststore");
$this->assertEquals($datastore->lobster_max_power,50);
}
/**
* @depends testLoadDatastore
*/
public function testAdvancedFunctionality()
{
$datastore = new simpleDatastore("teststore");
$datastore['lobster_king'] = array("name"=>'Lobstonia the cruel',"lobster_power"=>50,"age"=>34,"powers"=>"Claw attack");
$datastore[] = "welcome";
$datastore->save();
$datastore->close();
$newdata = new simpleDatastore("teststore");
$this->assertEquals($newdata->lobster_king['lobster_power'],50);
$this->assertEquals($newdata['lobster_king']['powers'],'Claw attack');
$this->assertTrue(strpos($newdata,'welcome') !== false);
$newdata->destroy();
}
public function testFileLocking()
{
$datastore = new simpleDatastore("popular");
$datastore->value = 1;
$datastore->save(true);
unset($datastore);
$teamA = new simpleDatastore();
$teamA->timeBetweenLockAttempts = 0.1;
$teamA->debug_mode = true;
$teamB = new simpleDatastore();
$teamB->timeBetweenLockAttempts = 0.1;
$teamB->debug_mode = true;
$teamB->error_mode = simpleDatastore::$ERROR_MODE_SILENT;
$teamA->open("popular");
$teamA->value = 2;
$teamA->save();
$teamB->open("popular");
$teamB->value = 3;
$teamA->value = 900;
$teamA->save(true);
$teamB->save(true);
unset($teamA);
unset($teamB);
$datastore = new simpleDatastore("popular");
$this->assertEquals($datastore->value,900);
$datastore->destroy();
}
public function testReadOnly()
{
$datastore = new simpleDatastore("popular");
$datastore->value = 1;
$datastore->save(true);
unset($datastore);
$teamA = new simpleDatastore();
$teamA->timeBetweenLockAttempts = 0.1;
$teamA->debug_mode = true;
$teamB = new simpleDatastore(null,true);
$teamB->timeBetweenLockAttempts = 0.1;
$teamB->debug_mode = true;
$teamB->error_mode = simpleDatastore::$ERROR_MODE_SILENT;
$teamA->open("popular");
$teamA->value = 2;
$teamA->save();
$teamB->open("popular");
$this->assertEquals($teamA->value,$teamB->value);
$teamB->value = 3;
$teamA->value = 900;
$teamA->save();
$teamB->open('popular');
$this->assertEquals($teamA->value,$teamB->value);
unset($teamB);
$teamA->destroy();
}
public function testSerialize()
{
$smallDatastore = (object) array("code"=>12345);
$bigDatastore = new simpleDatastore("big",false,true);
$bigDatastore->small_guy = $smallDatastore;
$bigDatastore->save(true);
unset($smallDatastore);
unset($bigDatastore);
$otherDatastore = new simpleDatastore("big",false,true);
$this->assertEquals($otherDatastore->small_guy->code,12345);
}
/**
* @depends testSerialize
*/
public function testDeepSerializing()
{
$smallDatastore = new simpleDatastore("small",false,true);
$smallDatastore->code = 12345;
$smallDatastore->passkey = json_decode('{"red_code":"alpha","blue_code":"zulu"}');
$smallDatastore->save();
$bigDatastore = new simpleDatastore("big",false,true);
$bigDatastore->small_guy = $smallDatastore;
$bigDatastore->save();
$bigDatastore->close();
$otherGuy = new simpleDatastore("big",false,true);
$this->assertEquals($otherGuy->small_guy->passkey->red_code,"alpha");
$otherGuy->small_guy->passkey->red_code = "beta";
$otherGuy->small_guy->save(true);
$otherGuy->destroy();
$anotherSmallGuy = new simpleDatastore("small",false,true);
$this->assertEquals($anotherSmallGuy->passkey->blue_code,"zulu");
$anotherSmallGuy->destroy();
}
public function testLockConfiguration()
{
$firstGuy = new simpleDatastore("race");
$firstGuy['something'] = "someOtherThing";
$firstGuy->save();
$secondGuy = new simpleDatastore();
$secondGuy->setLockConfig(1,5);
$arr = $secondGuy->getLockConfig();
$this->assertEquals($arr['secondsBetweenLockAttempts'],1);
$this->assertEquals($arr['lockAttempts'],5);
$secondGuy->error_mode = simpleDatastore::$ERROR_MODE_SILENT;
$oldTime = time();
$secondGuy->open("race");
$newTime = time();
$time = $newTime - $oldTime;
$this->assertGreaterThan(4,$time);//Should take more then 4 seconds for 5 attempts with 1 second sleep.
$firstGuy->destroy();
}
}