1: <?php
2: class StatementTest extends AMysql_TestCase {
3:
4: public function testDoubleExecute() {
5: $sql = "SELECT * FROM $this->tableName";
6: $stmt = $this->_amysql->prepare($sql);
7: $stmt->execute();
8: $this->setExpectedException('LogicException');
9: $stmt->execute();
10: }
11:
12: public function testNamedBinds1() {
13: $sql = ":foo: :bar:";
14: $binds = array (
15: ':foo:' => 'text1',
16: ':bar:' => 'text2'
17: );
18: $stmt = $this->_amysql->prepare($sql);
19: $expected = "'text1' 'text2'";
20: $stmt->binds = $binds;
21: $this->assertEquals($expected, $stmt->getSql());
22: }
23:
24: public function testNamedBinds2() {
25: $sql = ":foo: :foo:";
26: $binds = array (
27: ':foo:' => 'text1'
28: );
29: $stmt = $this->_amysql->prepare($sql);
30: $expected = "'text1' 'text1'";
31: $stmt->binds = $binds;
32: $this->assertEquals($expected, $stmt->getSql());
33: }
34:
35: public function testNamedBinds3() {
36: $sql = ":fooo :foo";
37: $binds = array (
38: ':foo' => 'shorter',
39: ':fooo' => 'longer'
40: );
41: $stmt = $this->_amysql->prepare($sql);
42: $expected = "'longer' 'shorter'";
43: $stmt->binds = $binds;
44: $this->assertEquals($expected, $stmt->getSql());
45: }
46:
47: public function testNamedBinds4() {
48: $sql = ":foo :bar";
49: $binds = array (
50: 'foo' => ':bar',
51: 'bar' => 'cheese'
52: );
53: $stmt = $this->_amysql->prepare($sql);
54: $expected = "':bar' 'cheese'";
55: $stmt->binds = $binds;
56: $this->assertEquals($expected, $stmt->getSql());
57: }
58:
59: public function testAutoColon1() {
60: $sql = ":foo :bar";
61: $binds = array (
62: 'foo' => 's1',
63: 'bar' => 's2'
64: );
65: $stmt = $this->_amysql->prepare($sql);
66: $expected = "'s1' 's2'";
67: $stmt->binds = $binds;
68: $this->assertEquals($expected, $stmt->getSql());
69: }
70:
71: public function testAutoColon2() {
72: $sql = ":foo:bar";
73: $binds = array (
74: 'foo' => 's1',
75: 'bar' => 's2'
76: );
77: $stmt = $this->_amysql->prepare($sql);
78: $expected = "'s1''s2'";
79: $stmt->binds = $binds;
80: $this->assertEquals($expected, $stmt->getSql());
81: }
82:
83: public function testAutoColon3() {
84: $sql = ":ékezet:árvíz";
85: $binds = array (
86: 'ékezet' => 's1',
87: 'árvíz' => 's2'
88: );
89: $stmt = $this->_amysql->prepare($sql);
90: $expected = "'s1''s2'";
91: $stmt->binds = $binds;
92: $this->assertEquals($expected, $stmt->getSql());
93: }
94:
95: public function testAutoColon4() {
96: $sql = ":foo\n:bar :a:b :c :d";
97: $binds = array (
98: 'foo' => 's1',
99: 'bar' => 's2',
100: 'a' => 's3',
101: 'b' => 's4',
102: 'c' => 's5',
103: 'd' => ''
104: );
105: $stmt = $this->_amysql->prepare($sql);
106: $expected = "'s1'\n's2' 's3''s4' 's5' ''";
107: $stmt->binds = $binds;
108: $this->assertEquals($expected, $stmt->getSql());
109: }
110:
111: public function testNoAutoColon1() {
112: $sql = "@ékezet:árvíz:";
113: $binds = array (
114: '@ékezet' => 's1',
115: 'árvíz:' => 's2'
116: );
117: $stmt = $this->_amysql->prepare($sql);
118: $expected = "'s1''s2'";
119: $stmt->binds = $binds;
120: $this->assertEquals($expected, $stmt->getSql());
121: }
122:
123: public function testPairUp() {
124: $data = array (
125: 'string' => array (
126: 3, 'blah'
127: )
128: );
129: $this->_amysql->insert($this->tableName, $data);
130: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
131: $results = $stmt->pairUp();
132: $expected = array (
133: '1' => '3',
134: '2' => 'blah'
135: );
136: $this->assertEquals($expected, $results);
137: }
138:
139: public function testPairUpColumnNames() {
140: $data = array (
141: 'string' => array (
142: 3, 'blah'
143: )
144: );
145: $this->_amysql->insert($this->tableName, $data);
146: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
147: $results = $stmt->pairUp('string', 'id');
148: $expected = array (
149: '3' => '1',
150: 'blah' => '2'
151: );
152: $this->assertEquals($expected, $results);
153: }
154:
155: public function testPairUpMixed() {
156: $data = array (
157: 'string' => array (
158: 3, 'blah'
159: )
160: );
161: $this->_amysql->insert($this->tableName, $data);
162: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
163: $results = $stmt->pairUp('string', 0);
164: $expected = array (
165: '3' => '1',
166: 'blah' => '2'
167: );
168: $this->assertEquals($expected, $results);
169: }
170:
171: public function testPairUpSame() {
172: $data = array (
173: 'string' => array (
174: 3, 'blah'
175: )
176: );
177: $this->_amysql->insert($this->tableName, $data);
178: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
179: $results = $stmt->pairUp(1, 1);
180: $expected = array (
181: '3' => '3',
182: 'blah' => 'blah'
183: );
184: $this->assertEquals($expected, $results);
185: }
186:
187: public function testFetchObject() {
188: $data = array (
189: 'string' => array (
190: 3, 'blah'
191: )
192: );
193: $this->_amysql->insert($this->tableName, $data);
194: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
195: $result = $stmt->fetchObject();
196: $this->assertEquals('1', $result->id);
197: $this->assertEquals('3', $result->string);
198: }
199:
200: public function testFetchObject2() {
201: $data = array (
202: 'string' => array (
203: 3, 'blah'
204: )
205: );
206: $this->_amysql->insert($this->tableName, $data);
207: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
208: $result = $stmt->fetchObject();
209: $this->assertTrue($result instanceof stdClass);
210: $this->assertEquals(3, $result->string);
211: $result = $stmt->fetchObject('ArrayObject',
212: array (array (),
213: ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST
214: )
215: );
216: $this->assertTrue($result instanceof ArrayObject);
217: $this->assertEquals('blah', $result->string);
218: }
219:
220: public function testFetchAllAssoc() {
221: $data = array (
222: 'string' => array (
223: 3, 'blah'
224: )
225: );
226: $this->_amysql->insert($this->tableName, $data);
227: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
228: $results = $stmt->fetchAllAssoc();
229: $expected = array (
230: array (
231: 'id' => '1',
232: 'string' => '3'
233: ),
234: array (
235: 'id' => '2',
236: 'string' => 'blah'
237: )
238: );
239: $this->assertEquals($expected, $results);
240: }
241:
242: public function testFetchAllDefault() {
243: $data = array (
244: 'string' => array (
245: 3, 'blah'
246: )
247: );
248: $this->_amysql->insert($this->tableName, $data);
249: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
250: $results = $stmt->fetchAll();
251: $expected = array (
252: array (
253: 'id' => '1',
254: 'string' => '3'
255: ),
256: array (
257: 'id' => '2',
258: 'string' => 'blah'
259: )
260: );
261: $this->assertEquals($expected, $results);
262: }
263:
264: public function testFetchAllAssocIdColumn() {
265: $data = array (
266: 'string' => array (
267: 3, 'blah'
268: )
269: );
270: $this->_amysql->insert($this->tableName, $data);
271: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
272: $results = $stmt->fetchAllAssoc('id');
273: $expected = array (
274: '1' => array (
275: 'id' => '1',
276: 'string' => '3'
277: ),
278: '2' => array (
279: 'id' => '2',
280: 'string' => 'blah'
281: )
282: );
283: $this->assertEquals($expected, $results);
284: }
285:
286: public function testFetchAllAssocIdColumn2() {
287: $data = array (
288: 'string' => array (
289: 3, 'blah'
290: )
291: );
292: $this->_amysql->insert($this->tableName, $data);
293: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
294: $results = $stmt->fetchAllAssoc(1);
295: $expected = array (
296: '3' => array (
297: 'id' => '1',
298: 'string' => '3'
299: ),
300: 'blah' => array (
301: 'id' => '2',
302: 'string' => 'blah'
303: )
304: );
305: $this->assertEquals($expected, $results);
306: }
307:
308: public function testBindParam() {
309: $sql = " :a ";
310: $bind = 1;
311: $stmt = $this->_amysql->prepare($sql);
312: $stmt->bindParam('a', $bind);
313: $bind = 2;
314: $resultSql = $stmt->getSql();
315: $this->assertEquals(' 2 ', $resultSql);
316: }
317:
318: public function testBindValue() {
319: $sql = " :a ";
320: $bind = 1;
321: $stmt = $this->_amysql->prepare($sql);
322: $stmt->bindValue('a', $bind);
323: $bind = 2;
324: $resultSql = $stmt->getSql();
325: $this->assertEquals(' 1 ', $resultSql);
326: }
327:
328: public function () {
329: $data = array (
330: 'string' => array (
331: 3, 'blah'
332: )
333: );
334: $this->_amysql->insert($this->tableName, $data);
335: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
336: $stmt->setFetchMode(AMysql_Abstract::FETCH_OBJECT, 'ArrayObject',
337: array (array (),
338: ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST
339: )
340: );
341: $result = $stmt->fetch();
342: $this->assertTrue($result instanceof ArrayObject);
343: $this->assertEquals('3', $result->string);
344: }
345:
346: 347: 348:
349: public function repeat20() {
350: return array_fill(0, 20, array ());
351: }
352:
353: public function testCount() {
354: $data = array (
355: array (
356: 'string' => 3
357: ),
358: array (
359: 'string' => 'blah',
360: )
361: );
362: $this->_amysql->insert($this->tableName, $data);
363: $stmt = $this->_amysql->query("SELECT * FROM $this->tableName");
364: $this->assertEquals(2, count($stmt));
365: }
366:
367: public function testCountNonSelect() {
368: $data = array (
369: array (
370: 'string' => 3
371: ),
372: array (
373: 'string' => 'blah',
374: )
375: );
376: $this->_amysql->insert($this->tableName, $data);
377: $stmt = $this->_amysql->lastStatement;
378: $this->setExpectedException('LogicException');
379: count($stmt);
380: }
381:
382: public function testFetchAllColumns() {
383: $data = array (
384: array (
385: 'string' => 3
386: ),
387: array (
388: 'string' => 'blah',
389: )
390: );
391: $this->_amysql->insert($this->tableName, $data);
392: $stmt = $this->_amysql->lastStatement;
393: $sql = "SELECT * FROM $this->tableName";
394: $stmt = $this->_amysql->query($sql);
395: $result = $stmt->fetchAllColumns();
396:
397: $expected = array (
398: 'id' => array ('1', '2'),
399: 'string' => array ('3', 'blah')
400: );
401: $this->assertEquals($expected, $result);
402: }
403:
404: public function testFetchAllColumnsEmpty() {
405: $sql = "SELECT * FROM $this->tableName";
406: $stmt = $this->_amysql->query($sql);
407: $result = $stmt->fetchAllColumns();
408: $this->assertEquals(array(), $result);
409: }
410:
411: public function testFetchAllColumnsNamed() {
412: $data = array (
413: array (
414: 'string' => 3
415: ),
416: array (
417: 'string' => 'blah',
418: )
419: );
420: $this->_amysql->insert($this->tableName, $data);
421: $stmt = $this->_amysql->lastStatement;
422: $sql = "SELECT * FROM $this->tableName";
423: $stmt = $this->_amysql->query($sql);
424: $result = $stmt->fetchAllColumns(1);
425:
426: $expected = array (
427: 'id' => array ('3' => '1', 'blah' => '2'),
428: 'string' => array ('3' => '3', 'blah' => 'blah')
429: );
430: $this->assertEquals($expected, $result);
431: }
432:
433: public function testProfiling() {
434: $data = array (
435: array (
436: 'string' => 3
437: ),
438: array (
439: 'string' => 'blah',
440: )
441: );
442: $this->_amysql->useNewProfiler();
443: $this->_amysql->insert($this->tableName, $data);
444: $this->assertTrue(is_float(
445: $this->_amysql->lastStatement->queryTime
446: ));
447: $this->assertGreaterThan(0.0, $this->_amysql->lastStatement->queryTime);
448: $this->assertSame($this->_amysql->totalTime,
449: $this->_amysql->lastStatement->queryTime);
450: }
451:
452: public function testProfilingClass() {
453: $data = array (
454: array (
455: 'string' => 3
456: ),
457: array (
458: 'string' => 'blah',
459: )
460: );
461: $this->_amysql->useNewProfiler();
462: $profiler = $this->_amysql->getProfiler();
463: $this->_amysql->insert($this->tableName, $data);
464: $this->assertInternalType('float',
465: $this->_amysql->lastStatement->queryTime
466: );
467: $this->assertGreaterThan(0.0, $profiler['totalTime']);
468: $this->assertSame(
469: $profiler['totalTime'],
470: $this->_amysql->lastStatement->queryTime
471: );
472: }
473:
474: public function testProfilingClassQueriesData() {
475: $data = array (
476: array (
477: 'string' => 3
478: ),
479: array (
480: 'string' => 'blah',
481: )
482: );
483: $this->_amysql->useNewProfiler();
484: $profiler = $this->_amysql->getProfiler();
485: $this->_amysql->insert($this->tableName, $data);
486: $lastQueryData = $profiler['queriesData'][count($profiler['queriesData']) - 1];
487: $this->assertInternalType('array', $lastQueryData);
488: $keys = array('query', 'time', 'backtrace');
489: $this->assertEquals($keys, array_keys($lastQueryData));
490: $this->assertSame($profiler['totalTime'], $lastQueryData['time']);
491: $this->assertSame($this->_amysql->lastStatement->query, $lastQueryData['query']);
492: }
493:
494: public function testProfilingMethodsInAbstract()
495: {
496: $data = array (
497: array (
498: 'string' => 3
499: ),
500: array (
501: 'string' => 'blah',
502: )
503: );
504: $this->_amysql->useNewProfiler();
505: $profiler = $this->_amysql->getProfiler();
506: $this->_amysql->insert($this->tableName, $data);
507: $queriesData = $this->_amysql->getQueriesData();
508: $lastQueryData = $queriesData[count($profiler['queriesData']) - 1];
509: $this->assertInternalType('array', $lastQueryData);
510: $keys = array('query', 'time', 'backtrace');
511: $this->assertEquals($keys, array_keys($lastQueryData));
512: }
513:
514: public function testProfilingGetQueries()
515: {
516: $data = array (
517: array (
518: 'string' => 3
519: ),
520: array (
521: 'string' => 'blah',
522: )
523: );
524: $this->_amysql->useNewProfiler();
525: $profiler = $this->_amysql->getProfiler();
526: $stmt = $this->_amysql->ins($this->tableName, $data);
527: $queryString = "INSERT INTO `abstracttest` (`string`) VALUES (3), ('blah')";
528:
529: $queriesInProfiler = $this->_amysql->getProfiler()->getQueries();
530: $this->assertSame($queryString, $queriesInProfiler[0]);
531:
532: $queries = $this->_amysql->getQueries();
533: $this->assertSame($queryString, $queries[0]);
534: }
535: }
536: ?>
537: