xref: /PHP-5.3/ext/pdo/tests/pdo_031.phpt (revision 610c7fbe)
1--TEST--
2PDO Common: PDOStatement SPL iterator
3--SKIPIF--
4<?php # vim:ft=php
5if (!extension_loaded('pdo')) die('skip');
6if (!extension_loaded('SPL')) die('skip SPL not available');
7$dir = getenv('REDIR_TEST_DIR');
8if (false == $dir) die('skip no driver');
9require_once $dir . 'pdo_test.inc';
10if (!class_exists('RecursiveArrayIterator', false)) die('skip Class RecursiveArrayIterator missing');
11if (!class_exists('RecursiveTreeIterator', false) && !file_exists(getenv('REDIR_TEST_DIR').'../../spl/examples/recursivetreeiterator.inc')) die('skip Class RecursiveTreeIterator missing');
12PDOTest::skip();
13?>
14--FILE--
15<?php
16if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
17require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
18if (!class_exists('RecursiveTreeIterator', false)) require_once(getenv('REDIR_TEST_DIR').'../../spl/examples/recursivetreeiterator.inc');
19
20$data = array(
21    array('10', 'Abc', 'zxy'),
22    array('20', 'Def', 'wvu'),
23    array('30', 'Ghi', 'tsr'),
24);
25
26$db = PDOTest::factory();
27
28$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
29
30$stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)");
31foreach ($data as $row) {
32    $stmt->execute($row);
33}
34
35unset($stmt);
36
37echo "===QUERY===\n";
38
39$stmt = $db->query('SELECT * FROM test');
40
41foreach(new RecursiveTreeIterator(new RecursiveArrayIterator($stmt->fetchAll(PDO::FETCH_ASSOC)), RecursiveTreeIterator::BYPASS_KEY) as $c=>$v)
42{
43	echo "$v [$c]\n";
44}
45
46echo "===DONE===\n";
47exit(0);
48?>
49--EXPECT--
50===QUERY===
51|-Array [0]
52| |-10 [id]
53| |-Abc [val]
54| \-zxy [val2]
55|-Array [1]
56| |-20 [id]
57| |-Def [val]
58| \-wvu [val2]
59\-Array [2]
60  |-30 [id]
61  |-Ghi [val]
62  \-tsr [val2]
63===DONE===
64