1: <?php
2: class AbstractTest extends AMysql_TestCase
3: {
4:
5: public function testConnect() {
6: $this->tearDown();
7: $this->_amysql = new AMysql;
8: $this->_amysql->setConnDetails(array (
9: 'host' => AMYSQL_TEST_HOST,
10: 'username' =>AMYSQL_TEST_USER,
11: 'password' => AMYSQL_TEST_PASS,
12: 'db' => AMYSQL_TEST_DB,
13: ));
14: $this->_amysql->connect();
15: $this->createTable();
16: }
17:
18: public function testConnectThenToDb() {
19: $this->tearDown();
20: $this->_amysql = new AMysql;
21: $this->_amysql->setConnDetails(array (
22: 'host' => AMYSQL_TEST_HOST,
23: 'username' =>AMYSQL_TEST_USER,
24: 'password' => AMYSQL_TEST_PASS,
25: ));
26: $this->_amysql->connect();
27: $this->_amysql->selectDb(AMYSQL_TEST_DB);
28: $this->createTable();
29: }
30:
31: public function testLazyConnect() {
32: $this->tearDown();
33: $this->_amysql = new AMysql;
34: $this->_amysql->setConnDetails(array (
35: 'host' => AMYSQL_TEST_HOST,
36: 'username' =>AMYSQL_TEST_USER,
37: 'password' => AMYSQL_TEST_PASS,
38: 'db' => AMYSQL_TEST_DB,
39: ));
40: $this->createTable();
41: }
42:
43: public function testConnectRightPort() {
44: $this->tearDown();
45: $this->_amysql = new AMysql;
46: $this->_amysql->setConnDetails(array (
47: 'host' => AMYSQL_TEST_HOST,
48: 'username' =>AMYSQL_TEST_USER,
49: 'password' => AMYSQL_TEST_PASS,
50: 'port' => 3306,
51: 'db' => AMYSQL_TEST_DB,
52: ));
53: $this->createTable();
54: }
55:
56: public function testForceMysql() {
57: if (!function_exists('mysql_connect')) {
58: $this->markTestIncomplete("mysql_* isn't available on this PHP build.");
59: }
60: if ($mysqlDeprecated = (version_compare(PHP_VERSION, '5.5.0') >= 0)) {
61: error_reporting(error_reporting() & ~E_DEPRECATED);
62: }
63: $this->tearDown();
64: $this->_amysql = new AMysql;
65: $this->_amysql->setConnDetails(array (
66: 'host' => AMYSQL_TEST_HOST,
67: 'username' =>AMYSQL_TEST_USER,
68: 'password' => AMYSQL_TEST_PASS,
69: 'port' => 3306,
70: 'db' => AMYSQL_TEST_DB,
71: ));
72: AMysql_Abstract::$useMysqli = false;
73: $this->createTable();
74: $this->assertEquals('resource', gettype($this->_amysql->link));
75: AMysql_Abstract::$useMysqli = true;
76: if ($mysqlDeprecated) {
77: error_reporting(error_reporting() | E_DEPRECATED);
78: }
79: }
80:
81: public function testInsertSingleRow() {
82: $data = array (
83: array (
84: 'string' => 3
85: ),
86: );
87: $this->_amysql->insert($this->tableName, $data);
88: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
89: $results = $stmt->fetchAllAssoc();
90: $expected = array (
91: array (
92: 'id' => '1',
93: 'string' => '3'
94: ),
95: );
96: $this->assertEquals($expected, $results);
97: }
98:
99: 100: 101: 102:
103: public function testInsert() {
104: $data = array (
105: array (
106: 'string' => 3
107: ),
108: array (
109: 'string' => 'blah',
110: )
111: );
112: $this->_amysql->insert($this->tableName, $data);
113: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
114: $results = $stmt->fetchAllAssoc();
115: $expected = array (
116: array (
117: 'id' => '1',
118: 'string' => '3'
119: ),
120: array (
121: 'id' => '2',
122: 'string' => 'blah'
123: )
124: );
125: $this->assertEquals($expected, $results);
126: }
127:
128: 129: 130: 131: 132: 133: 134:
135: public function testInsert2() {
136: $data = array (
137: 'string' => array (
138: 3, 'blah'
139: )
140: );
141: $this->_amysql->insert($this->tableName, $data);
142: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
143: $results = $stmt->fetchAllAssoc();
144: $expected = array (
145: array (
146: 'id' => '1',
147: 'string' => '3'
148: ),
149: array (
150: 'id' => '2',
151: 'string' => 'blah'
152: )
153: );
154: $this->assertEquals($expected, $results);
155: }
156:
157: public function testGetOne() {
158: $data = array (
159: 'string' => array (
160: 3, 'blah'
161: )
162: );
163: $this->_amysql->insert($this->tableName, $data);
164: $result = $this->_amysql->getOne("SELECT string FROM $this->tableName");
165: $this->assertEquals('3', $result);
166: }
167:
168: public function testGetOneWarning() {
169: $data = array (
170: 'string' => array (
171: 3, 'blah'
172: )
173: );
174: $this->_amysql->insert($this->tableName, $data);
175: $this->setExpectedException('PHPUnit_Framework_Error_Warning');
176: $result = $this->_amysql->getOne("SELECT string FROM $this->tableName
177: WHERE id = '3'");
178: }
179:
180: public function testGetOneInt() {
181: $data = array (
182: 'string' => array (
183: 3, 'blah'
184: )
185: );
186: $this->_amysql->insert($this->tableName, $data);
187: $result = $this->_amysql->getOneInt("SELECT id FROM $this->tableName");
188: $this->assertSame(1, $result);
189: $this->assertNotSame('1', $result);
190: }
191:
192: public function testGetOneInt2() {
193: $data = array (
194: 'string' => array (
195: 3, 'blah'
196: )
197: );
198: $this->_amysql->insert($this->tableName, $data);
199: $result = $this->_amysql->getOneInt("SELECT id FROM $this->tableName
200: WHERE id = '3'");
201: $this->assertSame(0, $result);
202: }
203:
204: public function testGetOneInt3() {
205: $data = array (
206: 'string' => array (
207: 3, 'blah'
208: )
209: );
210: $this->_amysql->insert($this->tableName, $data);
211: $result = $this->_amysql->getOneInt(
212: "SELECT string FROM $this->tableName WHERE string = 'blah'"
213: );
214: $this->assertSame(0, $result);
215: }
216:
217: public function testGetOneNull() {
218: $data = array (
219: 'string' => array (
220: 3, 'blah'
221: )
222: );
223: $this->_amysql->insert($this->tableName, $data);
224: $result = $this->_amysql->getOneNull(
225: "SELECT string FROM $this->tableName WHERE id = '3'"
226: );
227: $this->assertNull($result);
228: }
229:
230: public function testDelete() {
231: $data = array (
232: array (
233: 'string' => 3
234: ),
235: );
236: $this->_amysql->insert($this->tableName, $data);
237: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
238: $results = $stmt->fetchAllAssoc();
239: $expected = array (
240: array (
241: 'id' => '1',
242: 'string' => '3'
243: ),
244: );
245: $this->assertEquals($expected, $results);
246: $success = $this->_amysql->delete($this->tableName, 'id = ?', 2);
247: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
248: $results = $stmt->fetchAllAssoc();
249: $this->assertEquals($expected, $results);
250: $success = $this->_amysql->delete($this->tableName, 'id = ?', 1);
251: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
252: $results = $stmt->fetchAllAssoc();
253: $this->assertSame(array (), $results);
254: }
255:
256: public function testUpdate() {
257: $data = array (
258: 'string' => array (
259: 3, 'blah'
260: )
261: );
262: $this->_amysql->insert($this->tableName, $data);
263: $data = array (
264: 'string' => 'foo'
265: );
266: $this->_amysql->update($this->tableName, $data, 'id = ?',
267: array ('1'));
268: $results = $this->_amysql->query("SELECT * FROM $this->tableName")
269: ->fetchAllAssoc();
270: $expected = array (
271: array (
272: 'id' => '1',
273: 'string' => 'foo'
274: ),
275: array (
276: 'id' => '2',
277: 'string' => 'blah'
278: )
279: );
280: $this->assertEquals($expected, $results);
281: }
282:
283: public function testUpdateNotArrayBinds() {
284: $data = array (
285: 'string' => array (
286: 3, 'blah'
287: )
288: );
289: $this->_amysql->insert($this->tableName, $data);
290: $data = array (
291: 'string' => 'foo'
292: );
293: $this->_amysql->update($this->tableName, $data, 'id = ?', '1');
294: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName WHERE
295: id = ?", 1);
296: $result = $stmt->fetchAssoc();
297: $this->assertEquals('foo', $result['string']);
298: }
299:
300: public function testUpdateMultipleByKey() {
301: $data = array (
302: 'string' => array (
303: 3, 'blah'
304: )
305: );
306: $this->_amysql->insert($this->tableName, $data);
307: $data = array (
308: 1 => array (
309: 'string' => 'foo'
310: ),
311: 2 => array (
312: 'string' => 'bar'
313: )
314: );
315: $this->_amysql->updateMultipleByKey($this->tableName, $data, 'id');
316:
317: $this->assertEquals(2, $this->_amysql->multipleAffectedRows);
318:
319: $results = $this->_amysql->query("SELECT * FROM $this->tableName")
320: ->fetchAllAssoc();
321: $expected = array (
322: array (
323: 'id' => '1',
324: 'string' => 'foo'
325: ),
326: array (
327: 'id' => '2',
328: 'string' => 'bar'
329: )
330: );
331: $this->assertEquals($expected, $results);
332:
333: $this->_amysql->updateMultipleByKey($this->tableName, $data, 'id');
334: $this->assertEquals(0, $this->_amysql->multipleAffectedRows);
335: }
336:
337: public function testUpdateMultipleByKeySameColumn() {
338: $data = array (
339: 'string' => array (
340: 3, 'blah'
341: )
342: );
343: $this->_amysql->insert($this->tableName, $data);
344: $data = array (
345: 1 => array (
346: 'id' => 3,
347: 'string' => 'foo'
348: ),
349: 2 => array (
350: 'id' => 4,
351: 'string' => 'bar'
352: )
353: );
354: $this->_amysql->updateMultipleByKey($this->tableName, $data, 'id');
355: $results = $this->_amysql->query("SELECT * FROM $this->tableName")
356: ->fetchAllAssoc();
357: $expected = array (
358: array (
359: 'id' => '1',
360: 'string' => 'foo'
361: ),
362: array (
363: 'id' => '2',
364: 'string' => 'bar'
365: )
366: );
367: $this->assertEquals($expected, $results);
368: }
369:
370: public function testUpdateMultipleByKeySameColumn2() {
371: $data = array (
372: 'string' => array (
373: 3, 'blah'
374: )
375: );
376: $this->_amysql->insert($this->tableName, $data);
377: $data = array (
378: 1 => array (
379: 'id' => 3,
380: 'string' => 'foo'
381: ),
382: 2 => array (
383: 'id' => 4,
384: 'string' => 'bar'
385: )
386: );
387: $success =
388: $this->_amysql->updateMultipleByKey($this->tableName, $data, 'id',
389: true);
390: $results = $this->_amysql->query("SELECT * FROM $this->tableName")
391: ->fetchAllAssoc();
392: $expected = array (
393: array (
394: 'id' => '3',
395: 'string' => 'foo'
396: ),
397: array (
398: 'id' => '4',
399: 'string' => 'bar'
400: )
401: );
402: $this->assertTrue($success);
403: $this->assertEquals($expected, $results);
404: }
405:
406: public function testUpdateMultipleByData() {
407: $data = array (
408: 'string' => array (
409: 3, 'blah'
410: )
411: );
412: $this->_amysql->insert($this->tableName, $data);
413: $data = array (
414: array (
415: 'id' => 2,
416: 'string' => 'foo'
417: ),
418: array (
419: 'id' => 1,
420: 'string' => 'bar'
421: )
422: );
423: $success =
424: $this->_amysql->updateMultipleByData($this->tableName, $data, 'id',
425: true);
426: $results = $this->_amysql->query("SELECT * FROM $this->tableName")
427: ->fetchAllAssoc();
428: $expected = array (
429: array (
430: 'id' => '1',
431: 'string' => 'bar'
432: ),
433: array (
434: 'id' => '2',
435: 'string' => 'foo'
436: )
437: );
438: $this->assertTrue($success);
439: $this->assertEquals($expected, $results);
440:
441: $this->assertEquals(2, $this->_amysql->multipleAffectedRows);
442: $data[1]['string'] = 'bar2';
443: $success =
444: $this->_amysql->updateMultipleByData($this->tableName, $data, 'id',
445: true);
446: $this->assertEquals(1, $this->_amysql->multipleAffectedRows);
447: }
448:
449: public function testTranspose() {
450: $input = array (
451: 3 => array (
452: 'col1' => 'bla',
453: 'col2' => 'yo'
454: ),
455: 9 => array (
456: 'col1' => 'ney',
457: 'col2' => 'lol'
458: )
459: );
460:
461: $expected = array (
462: 'col1' => array (
463: 3 => 'bla',
464: 9 => 'ney'
465: ),
466: 'col2' => array (
467: 3 => 'yo',
468: 9 => 'lol'
469: )
470: );
471:
472: $result = AMysql_Abstract::transpose($input);
473: $this->assertEquals($expected, $result);
474: }
475:
476: public function testReplace() {
477: $data = array (
478: 'string' => array (
479: 3, 'blah'
480: )
481: );
482: $this->_amysql->insert($this->tableName, $data);
483: $data = array (
484: array (
485: 'id' => 2,
486: 'string' => 'replaced1'
487: ),
488: array (
489: 'id' => 4,
490: 'string' => 'replaced2'
491: ),
492: );
493: $this->_amysql->replace($this->tableName, $data);
494:
495: $result = $this->_amysql->query("SELECT * FROM $this->tableName")
496: ->fetchAllAssoc();
497:
498: $expected = array (
499: array (
500: 'id' => '1',
501: 'string' => '3'
502: ),
503: array (
504: 'id' => '2',
505: 'string' => 'replaced1'
506: ),
507: array (
508: 'id' => '4',
509: 'string' => 'replaced2'
510: )
511: );
512: $this->assertEquals($expected, $result);
513: }
514:
515: public function testProfiling () {
516: $data = array (
517: array (
518: 'string' => 3
519: ),
520: array (
521: 'string' => 'blah',
522: )
523: );
524: $this->_amysql->profileQueries = true;
525: $this->_amysql->insert($this->tableName, $data);
526: $this->assertTrue(is_float($this->_amysql->totalTime));
527: $this->assertGreaterThan(0.0, $this->_amysql->totalTime);
528: }
529:
530: public function testProfiling2 () {
531: $data = array (
532: array (
533: 'string' => 3
534: ),
535: array (
536: 'string' => 'blah',
537: )
538: );
539: $this->_amysql->profileQueries = true;
540: $this->_amysql->includeBacktrace = true;
541:
542: $this->_amysql->insert($this->tableName, $data);
543: $totalTimeSoFar = $this->_amysql->totalTime;
544: $this->assertTrue(is_float($this->_amysql->totalTime));
545: $this->assertGreaterThan(0.0, $this->_amysql->totalTime);
546:
547: $this->_amysql->insert($this->tableName, $data);
548: $this->assertTrue(is_float($this->_amysql->totalTime));
549: $this->assertGreaterThan($totalTimeSoFar, $this->_amysql->totalTime);
550: }
551:
552: public function testAnsi()
553: {
554: $this->_amysql->setAnsi(true);
555: $actual = $this->_amysql->escapeColumn('column');
556: $expected = '"column"';
557: $this->assertEquals($expected, $actual);
558: $this->_amysql->setAnsi(false);
559: $actual = $this->_amysql->escapeColumn('column');
560: $expected = '`column`';
561: $this->assertEquals($expected, $actual);
562: }
563:
564: public function testInTransaction()
565: {
566: $amysql = $this->_amysql;
567: $this->assertFalse($amysql->inTransaction());
568: $amysql->startTransaction();
569: $this->assertTrue($amysql->inTransaction());
570: $amysql->startTransaction();
571: $this->assertTrue($amysql->inTransaction());
572: $amysql->commit();
573: $this->assertFalse($amysql->inTransaction());
574: $amysql->startTransaction();
575: $this->assertTrue($amysql->inTransaction());
576: $amysql->rollback();
577: $this->assertFalse($amysql->inTransaction());
578: }
579:
580: public function testUtf8()
581: {
582: $amysql = $this->_amysql;
583: $amysql->setCharset('latin1')->setNames('latin1');
584: $string = 'aéó';
585: $data = array ('string' => $string);
586: $amysql->insert($this->tableName, array('string' => $data));
587: $amysql->setUtf8();
588: $row = $amysql->select('*')->from($this->tableName)->execute()->fetchAssoc();
589: $this->assertEquals('aéó', $row['string']);
590: $amysql->delete($this->tableName, 1, array());
591:
592: $amysql->setUtf8();
593: $string = 'aéőŰ';
594: $data = array ('string' => $string);
595: $amysql->insert($this->tableName, array('string' => $data));
596: $row = $amysql->select('*')->from($this->tableName)->execute()->fetchAssoc();
597: $this->assertEquals($string, $row['string']);
598: }
599:
600: public function testAutoPing()
601: {
602: $amysql = $this->_amysql;
603:
604: $amysql->setAutoPingSeconds(1);
605: $amysql->query('SET wait_timeout=1');
606: sleep(2);
607:
608: $amysql->setNames('utf8');
609: $this->assertTrue(true);
610: }
611:
612: public function testNoAutoPing()
613: {
614: try {
615: $amysql = $this->_amysql;
616:
617: $amysql->query('SET wait_timeout=1');
618: sleep(2);
619:
620: $amysql->setNames('utf8');
621: $amysql->query('SET wait_timeout=30');
622: $this->fail();
623: } catch (AMysql_Exception $e) {
624: $this->assertEquals(
625: AMysql_Exception::CODE_SERVER_GONE_AWAY,
626: $e->getCode()
627: );
628: }
629: }
630:
631: public function testAutoReconnect()
632: {
633: $amysql = $this->_amysql;
634: $amysql->setAutoReconnect(true);
635:
636: $amysql->query('SET wait_timeout=1');
637: sleep(2);
638:
639: $amysql->setNames('utf8');
640: $this->assertTrue(true);
641: }
642:
643: 644: 645:
646: public function repeat100() {
647: return array_fill(0, 100, array ());
648: }
649: }
650: ?>
651:
652: