1--TEST--
2SQLite3::createAggregate() use F ZPP for trampoline callback and does not leak
3--EXTENSIONS--
4sqlite3
5--FILE--
6<?php
7
8require_once(__DIR__ . '/new_db.inc');
9
10class TrampolineTest {
11    public function __call(string $name, array $arguments) {
12        echo 'Trampoline for ', $name, PHP_EOL;
13        $context = $arguments[0];
14        if ($name === 'finalize') {
15            return implode(',', $context['values']);
16        }
17        if (empty($context)) {
18            $context = ['total' => 0, 'values' => []];
19        }
20        $context['total'] += (int) $arguments[2];
21        $context['values'][] = $context['total'];
22        return $context;
23    }
24}
25$o = new TrampolineTest();
26$step = [$o, 'step'];
27$finalize = [$o, 'finalize'];
28
29var_dump($db->createAggregate('', $step, $finalize, 1));
30
31try {
32    var_dump($db->createAggregate(new stdClass(), $step, $finalize, new stdClass()));
33} catch (\Throwable $e) {
34    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
35}
36try {
37    var_dump($db->createAggregate('S', $step, $finalize, new stdClass()));
38} catch (\Throwable $e) {
39    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
40}
41try {
42    var_dump($db->createAggregate('S', $step, 'no_func', 1));
43} catch (\Throwable $e) {
44    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
45}
46try {
47    var_dump($db->createAggregate('S', 'no_func', $finalize, 1));
48} catch (\Throwable $e) {
49    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
50}
51
52try {
53    var_dump($db->createAggregate('S', $step, $finalize, 'not a number'));
54} catch (\Throwable $e) {
55    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
56}
57
58echo "Invalid SQLite3 object:\n";
59$rc = new ReflectionClass(SQLite3::class);
60$obj = $rc->newInstanceWithoutConstructor();
61
62try {
63    var_dump($obj->createAggregate('S', $step, $finalize, 1));
64} catch (\Throwable $e) {
65    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
66}
67
68var_dump($db->createAggregate('S', $step, $finalize, 1));
69
70echo "Closing database\n";
71var_dump($db->close());
72echo "Done\n";
73?>
74--EXPECT--
75bool(false)
76TypeError: SQLite3::createAggregate(): Argument #1 ($name) must be of type string, stdClass given
77TypeError: SQLite3::createAggregate(): Argument #4 ($argCount) must be of type int, stdClass given
78TypeError: SQLite3::createAggregate(): Argument #3 ($finalCallback) must be a valid callback, function "no_func" not found or invalid function name
79TypeError: SQLite3::createAggregate(): Argument #2 ($stepCallback) must be a valid callback, function "no_func" not found or invalid function name
80TypeError: SQLite3::createAggregate(): Argument #4 ($argCount) must be of type int, string given
81Invalid SQLite3 object:
82Error: The SQLite3 object has not been correctly initialised or is already closed
83bool(true)
84Closing database
85bool(true)
86Done
87