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 function setUpBeforeClass ()
7+ /**
8+ * Connects to the Database
9+ *
10+ * @return object Database connection
11+ */
12+ public function connect ($ config )
813 {
9- static ::setConfig ('PostgreSQL ' );
10- self ::seedDatabase ();
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 );
1122 }
1223
1324 /**
14- * Seeds the database for this connection
25+ * Disconnects from the Database
26+ *
27+ * @return boolean Success
28+ */
29+ public function disconnect ($ db )
30+ {
31+ return pg_close ($ db );
32+ }
33+
34+ /**
35+ * Checks the version of the Database
1536 *
1637 * @return void
1738 */
18- public function seedDatabase ( )
39+ public function checkVersion ( $ db )
1940 {
20- if (static ::$ config ['database ' ]=='{{test_database}} ' ) {
21- 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" );
2247 }
48+ }
2349
24- $ fixture = __DIR__ . ' /data/blog_ ' . strtolower ( static :: $ config [ ' dbengine ' ]). ' .sql ' ;
25-
26- $ e = function ( $ v ) { return str_replace ( array ( '\'' , '\\' ), array ( '\\\'' , '\\\\' ), $ v ); };
27- $ hostname = $ e ( static :: $ config [ ' hostname ' ]);
28- $ database = $ e ( static :: $ config [ ' database ' ]);
29- $ username = $ e ( static :: $ config [ ' username ' ]);
30- $ password = $ e ( static :: $ config [ ' password ' ]);
31- $ conn_string = " host=' $ hostname ' dbname=' $ database ' user=' $ username ' password=' $ password ' options='--client_encoding=UTF8' " ;
32-
33- $ db = pg_connect ( $ conn_string );
34-
35- if (! $ db ) {
36- die ( " Connect failed: " . pg_last_error ());
50+ /**
51+ * Gets the capabilities of the Database
52+ *
53+ * @return int Capabilites
54+ */
55+ public function getCapabilities ( $ db )
56+ {
57+ $ capabilities = 0 ;
58+ $ extensions = pg_fetch_all ( pg_query ( $ db , " SELECT * FROM pg_extension; " ));
59+ foreach ( $ extensions as $ extension ) {
60+ if ( $ extension [ ' extname ' ] === ' postgis ' ) {
61+ $ capabilities |= self :: GIS ;
62+ }
3763 }
64+ return $ capabilities ;
65+ }
3866
67+ /**
68+ * Seeds the database for this connection
69+ *
70+ * @return void
71+ */
72+ public function seedDatabase ($ db ,$ capabilities )
73+ {
74+ $ fixture = __DIR__ .'/data/blog_postgresql.sql ' ;
3975 $ queries = preg_split ('/;\s*\n/ ' , file_get_contents ($ fixture ));
4076 array_pop ($ queries );
4177
@@ -45,7 +81,5 @@ public function seedDatabase()
4581 die ("Loading ' $ fixture' failed on statemement # $ i with error: \n" .print_r ( pg_last_error ($ db ), true )."\n" );
4682 }
4783 }
48-
49- pg_close ($ db );
5084 }
5185}
0 commit comments