1--TEST--
2PDO PgSQL pgsqlCopyFromArray using Iterator
3--EXTENSIONS--
4pdo_pgsql
5--SKIPIF--
6<?php
7require __DIR__ . '/config.inc';
8require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
9PDOTest::skip();
10?>
11--FILE--
12<?php
13require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
14$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
15$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
16$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
17
18$db->exec('CREATE TABLE test_copy_from_traversable (v int)');
19
20$iterator = new class implements Iterator{
21    private $position = 0;
22    private $values = [1, 1, 2, 3, 5];
23
24    public function rewind(): void {
25        $this->position = 0;
26    }
27
28    public function current(): int {
29        return $this->values[$this->position];
30    }
31
32    public function key(): int {
33        return $this->position;
34    }
35
36    public function next(): void {
37        ++$this->position;
38    }
39
40    public function valid(): bool {
41        return isset($this->values[$this->position]);
42    }
43};
44
45$db->pgsqlCopyFromArray('test_copy_from_traversable',$iterator);
46
47$stmt = $db->query("select * from test_copy_from_traversable order by 1");
48$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
49var_export($result);
50
51?>
52--CLEAN--
53<?php
54require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
55$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
56$db->query('DROP TABLE IF EXISTS test_copy_from_traversable CASCADE');
57?>
58--EXPECT--
59array (
60  0 => 1,
61  1 => 1,
62  2 => 2,
63  3 => 3,
64  4 => 5,
65)
66