@@ -119,112 +119,20 @@ abstract class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
119119{
120120 public static $ config ;
121121
122- public static function setUpBeforeClass ()
123- {
124- if (static ::$ config ['database ' ]=='{{test_database}} ' ) {
125- die ("Configure database in 'config.php' before running tests. \n" );
126- }
127-
128- $ dbengine = static ::$ config ['dbengine ' ];
129- $ hostname = static ::$ config ['hostname ' ];
130- $ username = static ::$ config ['username ' ];
131- $ password = static ::$ config ['password ' ];
132- $ database = static ::$ config ['database ' ];
133-
134- $ fixture = __DIR__ .'/data/blog_ ' .strtolower ($ dbengine ).'.sql ' ;
135-
136- if ($ dbengine == 'MySQL ' ) {
137-
138- $ link = mysqli_connect ($ hostname , $ username , $ password , $ database );
139- if (mysqli_connect_errno ()) {
140- die ("Connect failed: " .mysqli_connect_error ()."\n" );
141- }
142- mysqli_set_charset ($ link ,'utf8 ' );
143-
144- $ i =0 ;
145- if (mysqli_multi_query ($ link , file_get_contents ($ fixture ))) {
146- do { $ i ++; mysqli_next_result ($ link ); } while (mysqli_more_results ($ link ));
147- }
148- if (mysqli_errno ($ link )) {
149- die ("Loading ' $ fixture' failed on statemement # $ i with error: \n" .mysqli_error ($ link )."\n" );
150- }
151-
152- mysqli_close ($ link );
153-
154- } elseif ($ dbengine == 'SQLServer ' ) {
155-
156- $ connectionInfo = array ();
157- $ connectionInfo ['UID ' ]=$ username ;
158- $ connectionInfo ['PWD ' ]=$ password ;
159- $ connectionInfo ['Database ' ]=$ database ;
160- $ connectionInfo ['CharacterSet ' ]='UTF-8 ' ;
161- $ conn = sqlsrv_connect ( $ hostname , $ connectionInfo );
162- if (!$ conn ) {
163- die ("Connect failed: " .print_r ( sqlsrv_errors (), true ));
164- }
165- $ queries = preg_split ('/\n\s*GO\s*\n/ ' , file_get_contents ($ fixture ));
166- array_pop ($ queries );
167- foreach ($ queries as $ i =>$ query ) {
168- if (!sqlsrv_query ($ conn , $ query )) {
169- $ i ++;
170- die ("Loading ' $ fixture' failed on statemement # $ i with error: \n" .print_r ( sqlsrv_errors (), true )."\n" );
171- }
172- }
173- sqlsrv_close ($ conn );
174-
175- } elseif ($ dbengine == 'PostgreSQL ' ) {
176-
177- $ e = function ($ v ) { return str_replace (array ('\'' ,'\\' ),array ('\\\'' ,'\\\\' ),$ v ); };
178- $ hostname = $ e ($ hostname );
179- $ database = $ e ($ database );
180- $ username = $ e ($ username );
181- $ password = $ e ($ password );
182- $ conn_string = "host=' $ hostname' dbname=' $ database' user=' $ username' password=' $ password' options='--client_encoding=UTF8' " ;
183- $ db = pg_connect ($ conn_string );
184- if (!$ db ) {
185- die ("Connect failed: " . pg_last_error ());
186- }
187- $ queries = preg_split ('/;\s*\n/ ' , file_get_contents ($ fixture ));
188- array_pop ($ queries );
189- foreach ($ queries as $ i =>$ query ) {
190- if (!pg_query ($ db , $ query .'; ' )) {
191- $ i ++;
192- die ("Loading ' $ fixture' failed on statemement # $ i with error: \n" .print_r ( pg_last_error ($ db ), true )."\n" );
193- }
194- }
195- pg_close ($ db );
196-
197- } elseif ($ dbengine == 'SQLite ' ) {
198-
199- $ db = new SQLite3 ($ database );
200- if (!$ db ) {
201- die ("Could not open ' $ database' SQLite database: " .SQLite3::lastErrorMsg ().' ( ' .SQLite3::lastErrorCode ().") \n" );
202- }
203- $ queries = preg_split ('/;\s*\n/ ' , file_get_contents ($ fixture ));
204- array_pop ($ queries );
205- foreach ($ queries as $ i =>$ query ) {
206- if (!$ db ->query ($ query .'; ' )) {
207- $ i ++;
208- die ("Loading ' $ fixture' failed on statemement # $ i with error: \n" .$ db ->lastErrorCode ().': ' .$ db ->lastErrorMsg ()."\n" );
209- }
210- }
211- $ db ->close ();
212- }
213- }
122+ public abstract function seedDatabase ();
214123
215- public function setConfig ($ dbengine = '' )
124+ protected function setConfig ($ dbengine = '' )
216125 {
217126 foreach (PHP_CRUD_API_Config::$ config as $ database ) {
218127 if ($ database ['dbengine ' ] == $ dbengine ) {
219128 static ::$ config = $ database ;
220129 return true ;
221130 }
222131 }
223- self ::markTestSkipped ();
132+ self ::markTestSkipped (" Configuration for { $ dbengine } database not found. " );
224133 return false ;
225134 }
226135
227- /** @group only */
228136 public function testListPosts ()
229137 {
230138 $ test = new API ($ this , static ::$ config );
0 commit comments