44 * are usually singletons. Instead of directly calling class::singleton staticly,
55 * this factory should be used so that a mock class can be subbed in for testing.
66 *
7- * If the Factory is in testing mode (setTesting(true) was called), a mock will
8- * be returned. Otherwise, the normal NDB_ prefixed object will be returned.
7+ * Mocks are injected using the setDatabase/setConfig/etc methods.
98 *
109 * PHP Version 7
1110 *
@@ -25,11 +24,6 @@ use \LORIS\StudyEntities\Candidate\CandID;
2524 * @author Dave MacFarlane <david.macfarlane2@mcgill.ca>
2625 * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
2726 * @link https://www.github.com/aces/Loris/
28- *
29- * Phan currently has bad support for PHPUnit's Mock objects which is
30- * creating false positive with the below rule.
31- *
32- * @phan-file-suppress PhanUndeclaredClassMethod
3327 */
3428class NDB_Factory
3529{
@@ -43,9 +37,6 @@ class NDB_Factory
4337 private static $ _timepoints = [];
4438
4539 private $ _baseURL = "" ;
46- var $ Testing ; // Whether or not Mock objects should be returned instead of
47- // real ones
48- //
4940
5041 /**
5142 * Settings object
@@ -55,18 +46,6 @@ class NDB_Factory
5546 */
5647 private static $ _settings = null ;
5748
58- /**
59- * Sets whether the factory should return real objects or testing objects
60- *
61- * @param boolean $val Whether testing should be enabled
62- *
63- * @return void
64- */
65- function setTesting (bool $ val ): void
66- {
67- $ this ->Testing = $ val ;
68- }
69-
7049 /**
7150 * Returns a single factory object. This must be used instead of being
7251 * constructed directly so that the testing suite and Loris code are
@@ -117,34 +96,14 @@ class NDB_Factory
11796 */
11897 function config (?string $ configfile = '../project/config.xml ' ): \NDB_Config
11998 {
120- if (self ::$ config !== null ) {
121- // The below suppression is necessary to satisfy phan. It flags an
122- // error here because the function is declared to return
123- // NDB_Config and phan doesn't understand MockNDB_Config (as it is
124- // generated dynamically by PHPUnit).
125- // We know this will always return \NDB_Config since this is guaranteed
126- // by the return type of the function.
127- //
128- // @phan-suppress-next-line PhanTypeMismatchReturn
129- return self ::$ config ;
130- }
131- if ($ this ->Testing ) {
132- $ config = new MockNDB_Config ();
133- $ configfile = '../test/config.xml ' ;
134- } else {
135- $ config = NDB_Config::singleton ($ configfile );
99+ $ config = self ::$ config ;
100+ if ($ config !== null ) {
101+ return $ config ;
136102 }
137103
138- self :: $ config = $ config ;
104+ $ config = NDB_Config:: singleton ( $ configfile ) ;
139105
140- // The below suppression is necessary to satisfy phan. It flags an
141- // error here because the function is declared to return
142- // NDB_Config and phan doesn't understand MockNDB_Config (as it is
143- // generated dynamically by PHPUnit).
144- // We know this will always return \NDB_Config since this is guaranteed
145- // by the return type of the function.
146- //
147- // @phan-suppress-next-line PhanTypeMismatchReturn
106+ self ::$ config = $ config ;
148107 return $ config ;
149108 }
150109
@@ -169,22 +128,11 @@ class NDB_Factory
169128 */
170129 function user (): \User
171130 {
172- if (self ::$ _user !== null ) {
173- // The below suppression is necessary to satisfy phan. It flags an
174- // error here because the function is declared to return
175- // User and phan doesn't understand MockUser (as it is
176- // generated dynamically by PHPUnit).
177- // We know this will always return \User since this is guaranteed
178- // by the return type of the function.
179- // @phan-suppress-next-line PhanTypeMismatchReturn
180- return self ::$ _user ;
181- }
182- if ($ this ->Testing ) {
183- Mock::generate ("User " );
184- $ user = new MockUser ();
185- } else {
186- $ user = \User::singleton ();
131+ $ user = self ::$ _user ;
132+ if ($ user !== null ) {
133+ return $ user ;
187134 }
135+ $ user = \User::singleton ();
188136 self ::$ _user = $ user ;
189137 return $ user ;
190138 }
@@ -210,21 +158,11 @@ class NDB_Factory
210158 */
211159 function database (): \Database
212160 {
213- $ db_ref = null ;
214- if ($ this ->Testing ) {
215- $ db_ref = &self ::$ testdb ;
216- if ($ db_ref !== null ) {
217- return $ db_ref ;
218- }
219- //self::$testdb = Mock::generate("Database");
220- self ::$ testdb = new MockDatabase ();
221- } else {
222- $ db_ref = &self ::$ db ;
223- if ($ db_ref !== null ) {
224- return $ db_ref ;
225- }
226- self ::$ db = Database::singleton ();
161+ $ db_ref = &self ::$ db ;
162+ if ($ db_ref !== null ) {
163+ return $ db_ref ;
227164 }
165+
228166 $ config = $ this ->config ();
229167 $ dbc = $ config ->getSetting ('database ' );
230168 // This check was added to satisfy phan, our static analysis tool.
@@ -235,13 +173,16 @@ class NDB_Factory
235173 'Could not configure database for LORIS '
236174 );
237175 }
238- $ db_ref ->connect (
176+
177+ $ db_ref = \Database::singleton (
239178 $ dbc ['database ' ],
240179 $ dbc ['username ' ],
241180 $ dbc ['password ' ],
242181 $ dbc ['host ' ],
243182 true
244183 );
184+
185+ self ::$ db = $ db_ref ;
245186 return $ db_ref ;
246187 }
247188
@@ -339,29 +280,16 @@ class NDB_Factory
339280 if (!empty (self ::$ _couchdb [$ database ])) {
340281 return self ::$ _couchdb [$ database ];
341282 }
342- if ($ this ->Testing ) {
343- Mock::generatePartial (
344- 'CouchDB ' ,
345- 'MockCouchDBWrap ' ,
346- /* mock out the functions that make HTTP requests */
347- [
348- '_getRelativeURL ' ,
349- '_postRelativeURL ' ,
350- '_postURL ' ,
351- '_getURL ' ,
352- ]
353- );
354- self ::$ _couchdb [$ database ] = new MockCouchDBWrap ();
355- } else {
356- self ::$ _couchdb [$ database ] = CouchDB::getInstance (
357- $ database ,
358- $ host ,
359- $ port ,
360- $ user ,
361- $ password
362- );
363- }
364- return self ::$ _couchdb [$ database ];
283+
284+ $ couch = CouchDB::getInstance (
285+ $ database ,
286+ $ host ,
287+ $ port ,
288+ $ user ,
289+ $ password
290+ );
291+ self ::$ _couchdb [$ database ] = $ couch ;
292+ return $ couch ;
365293 }
366294
367295 /**
@@ -419,15 +347,7 @@ class NDB_Factory
419347 */
420348 public function project (string $ projectName ): \Project
421349 {
422- $ project = null ;
423-
424- if ($ this ->Testing ) {
425- $ project = new MockProject ($ projectName );
426- } else {
427- $ project = \Project::singleton ($ projectName );
428- }
429-
430- return $ project ;
350+ return \Project::singleton ($ projectName );
431351 }
432352
433353 /**
@@ -443,11 +363,7 @@ class NDB_Factory
443363 if (isset (self ::$ _candidates [$ key ])) {
444364 return self ::$ _candidates [$ key ];
445365 }
446- if ($ this ->Testing ) {
447- self ::$ _candidates [$ key ] = new MockCandidate ($ CandID );
448- } else {
449- self ::$ _candidates [$ key ] = Candidate::singleton ($ CandID );
450- }
366+ self ::$ _candidates [$ key ] = Candidate::singleton ($ CandID );
451367 return self ::$ _candidates [$ key ];
452368
453369 }
@@ -465,15 +381,9 @@ class NDB_Factory
465381 if (isset (self ::$ _timepoints [(string ) $ sessionID ])) {
466382 return self ::$ _timepoints [(string ) $ sessionID ];
467383 }
468- if ($ this ->Testing ) {
469- self ::$ _timepoints [(string ) $ sessionID ] = new MockTimepoint (
470- $ sessionID
471- );
472- } else {
473- self ::$ _timepoints [(string ) $ sessionID ] = \TimePoint::singleton (
474- $ sessionID
475- );
476- }
384+ self ::$ _timepoints [(string ) $ sessionID ] = \TimePoint::singleton (
385+ $ sessionID
386+ );
477387 return self ::$ _timepoints [(string ) $ sessionID ];
478388 }
479389}
0 commit comments