xref: /PHP-8.2/ext/pdo_pgsql/tests/getnotify.phpt (revision 39131219)
1--TEST--
2PDO PgSQL LISTEN/NOTIFY support
3--EXTENSIONS--
4pdo
5pdo_pgsql
6--SKIPIF--
7<?php
8require __DIR__ . '/config.inc';
9require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
10PDOTest::skip();
11?>
12--FILE--
13<?php
14require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
15$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
16$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
17
18// pgsqlGetPid should return something meaningful
19$pid = $db->pgsqlGetPid();
20var_dump($pid > 0);
21
22// No listen, no notifies
23var_dump($db->pgsqlGetNotify());
24
25// Listen started, no notifies
26$db->exec("LISTEN notifies_phpt");
27var_dump($db->pgsqlGetNotify());
28
29// No parameters, use default PDO::FETCH_NUM
30$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM);
31$db->exec("NOTIFY notifies_phpt");
32$notify = $db->pgsqlGetNotify();
33var_dump(count($notify));
34var_dump($notify[0]);
35var_dump($notify[1] == $pid);
36
37// No parameters, use default PDO::FETCH_ASSOC
38$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
39$db->exec("NOTIFY notifies_phpt");
40$notify = $db->pgsqlGetNotify();
41var_dump(count($notify));
42var_dump($notify['message']);
43var_dump($notify['pid'] == $pid);
44
45// Test PDO::FETCH_NUM as parameter
46$db->exec("NOTIFY notifies_phpt");
47$notify = $db->pgsqlGetNotify(PDO::FETCH_NUM);
48var_dump(count($notify));
49var_dump($notify[0]);
50var_dump($notify[1] == $pid);
51
52// Test PDO::FETCH_ASSOC as parameter
53$db->exec("NOTIFY notifies_phpt");
54$notify = $db->pgsqlGetNotify(PDO::FETCH_ASSOC);
55var_dump(count($notify));
56var_dump($notify['message']);
57var_dump($notify['pid'] == $pid);
58
59// Test PDO::FETCH_BOTH as parameter
60$db->exec("NOTIFY notifies_phpt");
61$notify = $db->pgsqlGetNotify(PDO::FETCH_BOTH);
62var_dump(count($notify));
63var_dump($notify['message']);
64var_dump($notify['pid'] == $pid);
65var_dump($notify[0]);
66var_dump($notify[1] == $pid);
67
68// Verify that there are no notifies queued
69var_dump($db->pgsqlGetNotify());
70
71
72// Test second parameter, should wait 2 seconds because no notify is queued
73$t = microtime(1);
74$notify = $db->pgsqlGetNotify(PDO::FETCH_ASSOC, 1000);
75$diff = microtime(1) - $t;
76var_dump($diff >= 1 || 1 - abs($diff) < .05);
77var_dump($notify);
78
79// Test second parameter, should return immediately because a notify is queued
80$db->exec("NOTIFY notifies_phpt");
81$t = microtime(1);
82$notify = $db->pgsqlGetNotify(PDO::FETCH_ASSOC, 5000);
83$diff = microtime(1) - $t;
84var_dump($diff < 1 || abs(1 - abs($diff)) < .05);
85var_dump(count($notify));
86
87?>
88--EXPECT--
89bool(true)
90bool(false)
91bool(false)
92int(2)
93string(13) "notifies_phpt"
94bool(true)
95int(2)
96string(13) "notifies_phpt"
97bool(true)
98int(2)
99string(13) "notifies_phpt"
100bool(true)
101int(2)
102string(13) "notifies_phpt"
103bool(true)
104int(4)
105string(13) "notifies_phpt"
106bool(true)
107string(13) "notifies_phpt"
108bool(true)
109bool(false)
110bool(true)
111bool(false)
112bool(true)
113int(2)
114