11<?php
22
3- require_once (__DIR__ . '/tests .php ' );
3+ require_once (__DIR__ . '/Tests .php ' );
44
5- class PostgresqlTest extends PHP_CRUD_API_Test
5+ class PostgresqlTest extends Tests
66{
7- public static $ gis_installed ;
8- public static $ pg_server_version ;
7+ /**
8+ * Connects to the Database
9+ *
10+ * @return object Database connection
11+ */
12+ public function connect ($ config )
13+ {
14+ $ e = function ($ v ) { return str_replace (array ('\'' ,'\\' ),array ('\\\'' ,'\\\\' ),$ v ); };
15+ $ hostname = $ e ($ config ['hostname ' ]);
16+ $ database = $ e ($ config ['database ' ]);
17+ $ username = $ e ($ config ['username ' ]);
18+ $ password = $ e ($ config ['password ' ]);
19+ $ connectionString = "host=' $ hostname' dbname=' $ database' user=' $ username' password=' $ password' options='--client_encoding=UTF8' " ;
20+
21+ return pg_connect ($ connectionString );
22+ }
923
10- public static function setUpBeforeClass ()
24+ /**
25+ * Disconnects from the Database
26+ *
27+ * @return boolean Success
28+ */
29+ public function disconnect ($ db )
1130 {
12- static ::setConfig ('PostgreSQL ' );
13- self ::seedDatabase ();
31+ return pg_close ($ db );
1432 }
1533
1634 /**
17- * Seeds the database for this connection
35+ * Checks the version of the Database
1836 *
1937 * @return void
2038 */
21- public function seedDatabase ( )
39+ public function checkVersion ( $ db )
2240 {
23- if (static ::$ config ['database ' ]=='{{test_database}} ' ) {
24- die ("Configure database in 'config.php' before running tests. \n" );
41+ $ major = 9 ;
42+ $ minor = 1 ;
43+ $ version = pg_version ();
44+ $ v = explode ('. ' ,$ version ['server ' ]);
45+ if ($ v [0 ]<$ major || ($ v [0 ]==$ major && $ v [1 ]<$ minor )) {
46+ die ("Detected PostgreSQL $ v [0 ]. $ v [1 ], but only $ major. $ minor and up are supported \n" );
2547 }
48+ }
2649
27- $ e = function ($ v ) { return str_replace (array ('\'' ,'\\' ),array ('\\\'' ,'\\\\' ),$ v ); };
28- $ hostname = $ e (static ::$ config ['hostname ' ]);
29- $ database = $ e (static ::$ config ['database ' ]);
30- $ username = $ e (static ::$ config ['username ' ]);
31- $ password = $ e (static ::$ config ['password ' ]);
32- $ conn_string = "host=' $ hostname' dbname=' $ database' user=' $ username' password=' $ password' options='--client_encoding=UTF8' " ;
50+ /**
51+ * Gets the capabilities of the Database
52+ *
53+ * @return int Capabilites
54+ */
55+ public function getCapabilities ($ db )
56+ {
57+ $ capabilities = 0 ;
58+ $ major = 9 ;
59+ $ minor = 4 ;
60+ $ version = pg_version ();
61+ $ v = explode ('. ' ,$ version ['server ' ]);
62+ if ($ v [0 ]>$ major || ($ v [0 ]==$ major && $ v [1 ]>=$ minor )) {
63+ $ capabilities |= self ::JSON ;
64+ }
65+ $ extensions = pg_fetch_all (pg_query ($ db , "SELECT * FROM pg_extension; " ));
66+ foreach ($ extensions as $ extension ) {
67+ if ($ extension ['extname ' ] === 'postgis ' ) {
68+ $ capabilities |= self ::GIS ;
69+ }
70+ }
71+ return $ capabilities ;
72+ }
3373
34- $ db = pg_connect ($ conn_string );
74+ /**
75+ * Seeds the database for this connection
76+ *
77+ * @return void
78+ */
79+ public function seedDatabase ($ db ,$ capabilities )
80+ {
81+ $ fixture = __DIR__ .'/data/blog_postgresql.sql ' ;
82+ $ contents = file_get_contents ($ fixture );
3583
36- if (!$ db ) {
37- die ("Connect failed: " . pg_last_error ());
84+ if (!($ capabilities & self ::GIS )) {
85+ $ contents = preg_replace ('/(geometry) NOT NULL/i ' ,'text NOT NULL ' ,$ contents );
86+ $ contents = preg_replace ('/ST_GeomFromText/i ' ,'concat ' ,$ contents );
87+ }
88+ if (!($ capabilities & self ::JSON )) {
89+ $ contents = preg_replace ('/JSONB? NOT NULL/i ' ,'text NOT NULL ' ,$ contents );
3890 }
3991
40- static ::$ pg_server_version = pg_version ()['server ' ];
41- $ gisInstalled = self ::isGisInstalled (
42- pg_fetch_all (
43- pg_query ($ db , "SELECT * FROM pg_extension; " )
44- )
45- );
46- $ seed_file = self ::getSeedFile ();
47- $ queries = preg_split ('/;\s*\n/ ' , file_get_contents ($ seed_file ));
92+ $ queries = preg_split ('/;\s*\n/ ' , $ contents );
4893 array_pop ($ queries );
4994
5095 foreach ($ queries as $ i =>$ query ) {
@@ -53,8 +98,6 @@ public function seedDatabase()
5398 die ("Loading ' $ seed_file' failed on statemement # $ i with error: \n" .print_r ( pg_last_error ($ db ), true )."\n" );
5499 }
55100 }
56-
57- pg_close ($ db );
58101 }
59102
60103 /**
0 commit comments