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
45try {
46	$db->pgsqlCopyFromArray('test_copy_from_traversable',new stdClass());
47} catch (\TypeError $e) {
48	echo $e->getMessage() . PHP_EOL;
49}
50
51$db->pgsqlCopyFromArray('test_copy_from_traversable',$iterator);
52
53$stmt = $db->query("select * from test_copy_from_traversable order by 1");
54$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
55var_export($result);
56
57?>
58--CLEAN--
59<?php
60require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
61$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
62$db->query('DROP TABLE IF EXISTS test_copy_from_traversable CASCADE');
63?>
64--EXPECT--
65PDO::pgsqlCopyFromArray(): Argument #2 ($rows) must be of type Traversable|array, stdClass given
66array (
67  0 => 1,
68  1 => 1,
69  2 => 2,
70  3 => 3,
71  4 => 5,
72)
73