1: <?php
2:
3: 4: 5: 6:
7: if(!class_exists('Person')){
8: require_once __INCLUDES__ .'/model/Person.class.php';
9: }
10: if(!class_exists('Project')){
11: require_once __INCLUDES__ .'/model/Project.class.php';
12: }
13: if(!class_exists('Login')){
14: require_once __INCLUDES__ .'/model/Login.class.php';
15: }
16: if(!class_exists('Milestone')){
17: require_once __INCLUDES__ .'/model/Milestone.class.php';
18: }
19: if(!class_exists('Address')){
20: require_once __INCLUDES__ .'/model/Address.class.php';
21: }
22:
23: if(!class_exists('QAbstractCacheProvider')){
24: include_once __QCUBED_CORE__ . '/framework/QCacheProviderLocalMemory.class.php';
25: }
26:
27: 28: 29:
30: class QCacheProviderLocalMemoryTest extends QCacheProviderLocalMemory {
31: 32: 33: 34:
35: public $arrLocalCache;
36:
37: }
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49: class QDatabaseTests extends QUnitTestCaseBase {
50:
51: public function testTransactionWithCacheSaveRollBack() {
52: Person::GetDatabase()->Caching = true;
53:
54: $objCacheProvider = QApplication::$objCacheProvider;
55: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
56:
57:
58: $strPerson1_FirstName = null;
59: try {
60: Person::GetDatabase()->TransactionBegin();
61:
62: $objPerson1 = Person::Load(1);
63:
64: $strPerson1_FirstName = $objPerson1->FirstName;
65: $objPerson1->FirstName = "New value 1";
66: $objPerson1->Save();
67:
68:
69:
70:
71: $objPerson1a = Person::Load(1);
72:
73:
74: throw new Exception("DATABASE ERROR!");
75:
76: Person::GetDatabase()->TransactionCommit();
77: } catch(Exception $ex) {
78: Person::GetDatabase()->TransactionRollBack();
79:
80: }
81: $this->assertEquals(0, count(QApplication::$objCacheProvider->arrLocalCache), "Object is not placed in a cache because of the transaction roll back.");
82:
83:
84: QApplication::$objCacheProvider = $objCacheProvider;
85: }
86:
87:
88: public function testTransactionWithCacheSaveRollBack2() {
89: Person::GetDatabase()->Caching = true;
90:
91: $objCacheProvider = QApplication::$objCacheProvider;
92: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
93:
94:
95: $objPerson1z = Person::Load(1);
96:
97: $strPerson1_FirstName = $objPerson1z->FirstName;
98: try {
99: Person::GetDatabase()->TransactionBegin();
100:
101: $objPerson1 = Person::Load(1);
102:
103: $objPerson1->FirstName = "New value 1";
104: $objPerson1->Save();
105:
106:
107:
108:
109: $objPerson1a = Person::Load(1);
110:
111:
112: throw new Exception("DATABASE ERROR!");
113:
114: Person::GetDatabase()->TransactionCommit();
115: } catch(Exception $ex) {
116: Person::GetDatabase()->TransactionRollBack();
117:
118: }
119: $this->assertEquals(1, count(QApplication::$objCacheProvider->arrLocalCache), "Object is not dropped from a cache because of the transaction roll back.");
120: foreach (QApplication::$objCacheProvider->arrLocalCache as $objPerson) {
121: $this->assertEquals($strPerson1_FirstName, $objPerson->FirstName, "Object is not modified in a cache because of the transaction roll back.");
122: }
123:
124:
125: QApplication::$objCacheProvider = $objCacheProvider;
126: }
127:
128:
129: public function testTransactionWithCacheSaveCommit() {
130: Person::GetDatabase()->Caching = true;
131:
132: $objCacheProvider = QApplication::$objCacheProvider;
133: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
134:
135:
136: try {
137: Person::GetDatabase()->TransactionBegin();
138:
139: $objPerson1 = Person::Load(1);
140:
141: $objPerson1->FirstName = "New value 1";
142: $objPerson1->Save();
143:
144:
145: Person::GetDatabase()->TransactionCommit();
146:
147: } catch(Exception $ex) {
148: Person::GetDatabase()->TransactionRollBack();
149: }
150:
151: $this->assertEquals(0, count(QApplication::$objCacheProvider->arrLocalCache), "Object is not placed in a cache after save because of the transaction commit.");
152:
153:
154:
155: $objPerson1a = Person::Load(1);
156:
157:
158: $this->assertEquals(1, count(QApplication::$objCacheProvider->arrLocalCache), "Object is added to a cache because of the transaction commit.");
159: foreach (QApplication::$objCacheProvider->arrLocalCache as $objPerson) {
160: $this->assertEquals("New value 1", $objPerson->FirstName, "Object is modified in a cache because of the transaction commit.");
161: }
162:
163:
164: $objPerson1a->FirstName = "John";
165: $objPerson1a->Save();
166:
167:
168: QApplication::$objCacheProvider = $objCacheProvider;
169: }
170:
171:
172: public function testTransactionWithCacheSaveCommit2() {
173: Person::GetDatabase()->Caching = true;
174:
175: $objCacheProvider = QApplication::$objCacheProvider;
176: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
177:
178:
179: $objPerson1z = Person::Load(1);
180:
181: $strPerson1_FirstName = $objPerson1z->FirstName;
182: try {
183: Person::GetDatabase()->TransactionBegin();
184:
185: $objPerson1 = Person::Load(1);
186:
187: $objPerson1->FirstName = "New value 1";
188: $objPerson1->Save();
189:
190:
191: Person::GetDatabase()->TransactionCommit();
192:
193: } catch(Exception $ex) {
194: Person::GetDatabase()->TransactionRollBack();
195: }
196:
197: $this->assertEquals(0, count(QApplication::$objCacheProvider->arrLocalCache), "Object is dropped from a cache because of the transaction commit.");
198:
199:
200:
201: $objPerson1a = Person::Load(1);
202:
203:
204: $this->assertEquals(1, count(QApplication::$objCacheProvider->arrLocalCache), "Object is not dropped from a cache because of the transaction commit.");
205: foreach (QApplication::$objCacheProvider->arrLocalCache as $objPerson) {
206: $this->assertEquals("New value 1", $objPerson->FirstName, "Object is modified in a cache because of the transaction commit.");
207: }
208:
209:
210: $objPerson1a->FirstName = "John";
211: $objPerson1a->Save();
212:
213:
214: QApplication::$objCacheProvider = $objCacheProvider;
215: }
216:
217:
218: public function testTransactionWithCacheDeleteCommit() {
219:
220: $objPerson1z = new Person;
221: $objPerson1z->FirstName = "test";
222: $objPerson1z->LastName = "test";
223: $objPerson1z->Save();
224:
225: Person::GetDatabase()->Caching = true;
226:
227: $objCacheProvider = QApplication::$objCacheProvider;
228: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
229:
230:
231: try {
232: Person::GetDatabase()->TransactionBegin();
233:
234: $objPerson1 = Person::Load($objPerson1z->Id);
235:
236: $objPerson1->Delete();
237:
238:
239: Person::GetDatabase()->TransactionCommit();
240:
241: } catch(Exception $ex) {
242: Person::GetDatabase()->TransactionRollBack();
243: }
244:
245: $this->assertEquals(0, count(QApplication::$objCacheProvider->arrLocalCache), "Object is not placed in a cache after delete because of the transaction commit.");
246:
247:
248: QApplication::$objCacheProvider = $objCacheProvider;
249: }
250:
251:
252: public function testTransactionWithCacheDeleteCommit2() {
253: Person::GetDatabase()->Caching = true;
254:
255: $objCacheProvider = QApplication::$objCacheProvider;
256: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
257:
258:
259:
260: $objPerson1z = new Person;
261: $objPerson1z->FirstName = "test";
262: $objPerson1z->LastName = "test";
263: $objPerson1z->Save();
264:
265: Person::Load($objPerson1z->Id);
266:
267: $this->assertEquals(1, count(QApplication::$objCacheProvider->arrLocalCache), "Object is placed in a cache.");
268:
269: try {
270: Person::GetDatabase()->TransactionBegin();
271:
272: $objPerson1 = Person::Load($objPerson1z->Id);
273:
274: $objPerson1->Delete();
275:
276:
277: Person::GetDatabase()->TransactionCommit();
278:
279: } catch(Exception $ex) {
280: Person::GetDatabase()->TransactionRollBack();
281: }
282:
283: $this->assertEquals(0, count(QApplication::$objCacheProvider->arrLocalCache), "Object is removed from a cache after delete because of the transaction commit.");
284:
285:
286: QApplication::$objCacheProvider = $objCacheProvider;
287: }
288:
289:
290: public function testTransactionWithCacheDeleteRollBack() {
291:
292: $objPerson1z = new Person;
293: $objPerson1z->FirstName = "test";
294: $objPerson1z->LastName = "test";
295: $objPerson1z->Save();
296:
297: Person::GetDatabase()->Caching = true;
298:
299: $objCacheProvider = QApplication::$objCacheProvider;
300: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
301:
302:
303: try {
304: Person::GetDatabase()->TransactionBegin();
305:
306: $objPerson1 = Person::Load($objPerson1z->Id);
307:
308: $objPerson1->Delete();
309:
310:
311: throw new Exception("DATABASE ERROR!");
312:
313: Person::GetDatabase()->TransactionCommit();
314: } catch(Exception $ex) {
315: Person::GetDatabase()->TransactionRollBack();
316:
317: }
318:
319: $this->assertEquals(0, count(QApplication::$objCacheProvider->arrLocalCache), "Object is not placed in a cache after delete because of the transaction roll back.");
320:
321:
322: QApplication::$objCacheProvider = $objCacheProvider;
323:
324:
325: $objPerson1 = Person::Load($objPerson1z->Id);
326:
327: $objPerson1->Delete();
328: }
329:
330:
331: public function testTransactionWithCacheDeleteRollBack2() {
332: Person::GetDatabase()->Caching = true;
333:
334: $objCacheProvider = QApplication::$objCacheProvider;
335: QApplication::$objCacheProvider = new QCacheProviderLocalMemoryTest(array());
336:
337:
338:
339: $objPerson1z = new Person;
340: $objPerson1z->FirstName = "test";
341: $objPerson1z->LastName = "test";
342: $objPerson1z->Save();
343:
344: Person::Load($objPerson1z->Id);
345:
346: $this->assertEquals(1, count(QApplication::$objCacheProvider->arrLocalCache), "Object is placed in a cache.");
347:
348: try {
349: Person::GetDatabase()->TransactionBegin();
350:
351: $objPerson1 = Person::Load($objPerson1z->Id);
352:
353: $objPerson1->Delete();
354:
355:
356: throw new Exception("DATABASE ERROR!");
357:
358: Person::GetDatabase()->TransactionCommit();
359: } catch(Exception $ex) {
360: Person::GetDatabase()->TransactionRollBack();
361:
362: }
363:
364: $this->assertEquals(1, count(QApplication::$objCacheProvider->arrLocalCache), "Object is NOT removed from a cache after delete because of the transaction roll back.");
365:
366:
367: QApplication::$objCacheProvider = $objCacheProvider;
368:
369:
370: $objPerson1 = Person::Load($objPerson1z->Id);
371:
372: $objPerson1->Delete();
373: }
374: }