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