Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions php/libraries/NDB_Client.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ class NDB_Client
$DBSettings['host'],
true
);
// Now make sure the factory reference is the same as the
// singleton reference
$factory->setDatabase($DB);
}

// Now make sure the factory reference is the same as the
// singleton reference
$DB = $factory->database();

// add extra include paths. This must be done
Expand Down
156 changes: 33 additions & 123 deletions php/libraries/NDB_Factory.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* are usually singletons. Instead of directly calling class::singleton staticly,
* this factory should be used so that a mock class can be subbed in for testing.
*
* If the Factory is in testing mode (setTesting(true) was called), a mock will
* be returned. Otherwise, the normal NDB_ prefixed object will be returned.
* Mocks are injected using the setDatabase/setConfig/etc methods.
*
* PHP Version 7
*
Expand All @@ -25,11 +24,6 @@ use \LORIS\StudyEntities\Candidate\CandID;
* @author Dave MacFarlane <david.macfarlane2@mcgill.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*
* Phan currently has bad support for PHPUnit's Mock objects which is
* creating false positive with the below rule.
*
* @phan-file-suppress PhanUndeclaredClassMethod
*/
class NDB_Factory
{
Expand All @@ -43,9 +37,6 @@ class NDB_Factory
private static $_timepoints = [];

private $_baseURL = "";
var $Testing; // Whether or not Mock objects should be returned instead of
// real ones
//

/**
* Settings object
Expand All @@ -55,18 +46,6 @@ class NDB_Factory
*/
private static $_settings = null;

/**
* Sets whether the factory should return real objects or testing objects
*
* @param boolean $val Whether testing should be enabled
*
* @return void
*/
function setTesting(bool $val): void
{
$this->Testing = $val;
}

/**
* Returns a single factory object. This must be used instead of being
* constructed directly so that the testing suite and Loris code are
Expand Down Expand Up @@ -117,34 +96,14 @@ class NDB_Factory
*/
function config(?string $configfile = '../project/config.xml'): \NDB_Config
{
if (self::$config !== null) {
// The below suppression is necessary to satisfy phan. It flags an
// error here because the function is declared to return
// NDB_Config and phan doesn't understand MockNDB_Config (as it is
// generated dynamically by PHPUnit).
// We know this will always return \NDB_Config since this is guaranteed
// by the return type of the function.
//
// @phan-suppress-next-line PhanTypeMismatchReturn
return self::$config;
}
if ($this->Testing) {
$config = new MockNDB_Config();
$configfile = '../test/config.xml';
} else {
$config = NDB_Config::singleton($configfile);
$config = self::$config;
if ($config !== null) {
return $config;
}

self::$config = $config;
$config = NDB_Config::singleton($configfile);

// The below suppression is necessary to satisfy phan. It flags an
// error here because the function is declared to return
// NDB_Config and phan doesn't understand MockNDB_Config (as it is
// generated dynamically by PHPUnit).
// We know this will always return \NDB_Config since this is guaranteed
// by the return type of the function.
//
// @phan-suppress-next-line PhanTypeMismatchReturn
self::$config = $config;
return $config;
}

Expand All @@ -169,22 +128,11 @@ class NDB_Factory
*/
function user(): \User
{
if (self::$_user !== null) {
// The below suppression is necessary to satisfy phan. It flags an
// error here because the function is declared to return
// User and phan doesn't understand MockUser (as it is
// generated dynamically by PHPUnit).
// We know this will always return \User since this is guaranteed
// by the return type of the function.
// @phan-suppress-next-line PhanTypeMismatchReturn
return self::$_user;
}
if ($this->Testing) {
Mock::generate("User");
$user = new MockUser();
} else {
$user = \User::singleton();
$user = self::$_user;
if ($user !== null) {
return $user;
}
$user = \User::singleton();
self::$_user = $user;
return $user;
}
Expand All @@ -210,21 +158,11 @@ class NDB_Factory
*/
function database(): \Database
{
$db_ref = null;
if ($this->Testing) {
$db_ref = &self::$testdb;
if ($db_ref !== null) {
return $db_ref;
}
//self::$testdb = Mock::generate("Database");
self::$testdb = new MockDatabase();
} else {
$db_ref = &self::$db;
if ($db_ref !== null) {
return $db_ref;
}
self::$db = Database::singleton();
$db_ref = &self::$db;
if ($db_ref !== null) {
return $db_ref;
}

$config = $this->config();
$dbc = $config->getSetting('database');
// This check was added to satisfy phan, our static analysis tool.
Expand All @@ -235,13 +173,16 @@ class NDB_Factory
'Could not configure database for LORIS'
);
}
$db_ref->connect(

$db_ref = \Database::singleton(
$dbc['database'],
$dbc['username'],
$dbc['password'],
$dbc['host'],
true
);

self::$db = $db_ref;
return $db_ref;
}

Expand Down Expand Up @@ -339,29 +280,16 @@ class NDB_Factory
if (!empty(self::$_couchdb[$database])) {
return self::$_couchdb[$database];
}
if ($this->Testing) {
Mock::generatePartial(
'CouchDB',
'MockCouchDBWrap',
/* mock out the functions that make HTTP requests */
[
'_getRelativeURL',
'_postRelativeURL',
'_postURL',
'_getURL',
]
);
self::$_couchdb[$database] = new MockCouchDBWrap();
} else {
self::$_couchdb[$database] = CouchDB::getInstance(
$database,
$host,
$port,
$user,
$password
);
}
return self::$_couchdb[$database];

$couch = CouchDB::getInstance(
$database,
$host,
$port,
$user,
$password
);
self::$_couchdb[$database] = $couch;
return $couch;
}

/**
Expand Down Expand Up @@ -419,15 +347,7 @@ class NDB_Factory
*/
public function project(string $projectName): \Project
{
$project = null;

if ($this->Testing) {
$project = new MockProject($projectName);
} else {
$project = \Project::singleton($projectName);
}

return $project;
return \Project::singleton($projectName);
}

/**
Expand All @@ -443,11 +363,7 @@ class NDB_Factory
if (isset(self::$_candidates[$key])) {
return self::$_candidates[$key];
}
if ($this->Testing) {
self::$_candidates[$key] = new MockCandidate($CandID);
} else {
self::$_candidates[$key] = Candidate::singleton($CandID);
}
self::$_candidates[$key] = Candidate::singleton($CandID);
return self::$_candidates[$key];

}
Expand All @@ -465,15 +381,9 @@ class NDB_Factory
if (isset(self::$_timepoints[(string) $sessionID])) {
return self::$_timepoints[(string) $sessionID];
}
if ($this->Testing) {
self::$_timepoints[(string) $sessionID] = new MockTimepoint(
$sessionID
);
} else {
self::$_timepoints[(string) $sessionID] = \TimePoint::singleton(
$sessionID
);
}
self::$_timepoints[(string) $sessionID] = \TimePoint::singleton(
$sessionID
);
return self::$_timepoints[(string) $sessionID];
}
}
Expand Down
4 changes: 3 additions & 1 deletion php/libraries/Utility.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,10 @@ class Utility
"Input must be a valid date/time string: $e"
);
}

$factory = \NDB_Factory::singleton();
return $dt->format(
\NDB_Config::singleton()->getSetting('dateDisplayFormat')
$factory->config()->getSetting('dateDisplayFormat')
?? DateTime::ATOM
);
}
Expand Down
7 changes: 5 additions & 2 deletions test/integrationtests/LorisIntegrationTest.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@ abstract class LorisIntegrationTest extends TestCase
// Set up database wrapper and config
$this->factory = NDB_Factory::singleton();
$this->factory->reset();
$this->factory->setTesting(false);

$this->config = $this->factory->Config(CONFIG_XML);

$database = $this->config->getSetting('database');

$this->DB = Database::singleton(
$this->DB = Database::singleton(
$database['database'],
$database['username'],
$database['password'],
$database['host'],
1
);

$this->factory->setDatabase($this->DB);
$this->factory->setConfig($this->config);

$this->url = getenv('DOCKER_WEB_SERVER');
$password = new \Password($this->validPassword);

Expand Down
5 changes: 4 additions & 1 deletion test/unittests/CandidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ protected function setUp(): void
$this->_configMock = $this->getMockBuilder('NDB_Config')->getMock();
$this->_dbMock = $this->getMockBuilder('Database')->getMock();
$this->_factory = NDB_Factory::singleton();

$this->_factory->setConfig($this->_configMock);
$this->_factory->setDatabase($this->_dbMock);

Expand Down Expand Up @@ -1311,7 +1312,6 @@ private function _setUpMockDB()
{
$this->_factoryForDB = NDB_Factory::singleton();
$this->_factoryForDB->reset();
$this->_factoryForDB->setTesting(false);
$this->_config = $this->_factoryForDB->Config(CONFIG_XML);
$database = $this->_config->getSetting('database');
$this->_DB = Database::singleton(
Expand All @@ -1321,5 +1321,8 @@ private function _setUpMockDB()
$database['host'],
1
);

$this->_factoryForDB->setDatabase($this->_DB);
$this->_factoryForDB->setConfig($this->_config);
}
}
4 changes: 3 additions & 1 deletion test/unittests/Database_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ protected function setUp(): void
{
$this->factory = NDB_Factory::singleton();
$this->factory->reset();
$this->factory->setTesting(false);
$this->config = $this->factory->Config(CONFIG_XML);
$database = $this->config->getSetting('database');
$this->DB = Database::singleton(
Expand All @@ -96,6 +95,9 @@ protected function setUp(): void
$database['host'],
1
);

$this->factory->setDatabase($this->DB);
$this->factory->setConfig($this->config);
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function setUp(): void
];

$factory = \NDB_Factory::singleton();
$factory->setTesting(true);

$mockdb = $this->getMockBuilder("\Database")->getMock();
$mockconfig = $this->getMockBuilder("\NDB_Config")->getMock();
Expand Down
11 changes: 6 additions & 5 deletions test/unittests/NDB_BVL_Instrument_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ function setUp(): void
];

$this->_factory = \NDB_Factory::singleton();
$this->_factory->setTesting(true);

$this->_mockDB = $this->getMockBuilder("\Database")->getMock();
$this->_mockConfig = $this->getMockBuilder("\NDB_Config")->getMock();

\NDB_Factory::$db = $this->_mockDB;
\NDB_Factory::$testdb = $this->_mockDB;
\NDB_Factory::$config = $this->_mockConfig;
$this->_factory->setDatabase($this->_mockDB);
$this->_factory->setConfig($this->_mockConfig);

$this->quickForm = new \LorisForm();

Expand Down Expand Up @@ -1986,7 +1984,7 @@ private function _setUpMockDB()
{
$this->_factoryForDB = \NDB_Factory::singleton();
$this->_factoryForDB->reset();
$this->_factoryForDB->setTesting(false);

$this->_config = $this->_factoryForDB->Config(CONFIG_XML);
$database = $this->_config->getSetting('database');
$this->_DB = \Database::singleton(
Expand All @@ -1996,6 +1994,9 @@ private function _setUpMockDB()
$database['host'],
1
);

$this->_factoryForDB->setDatabase($this->_DB);
$this->_factoryForDB->setConfig($this->_config);
}
}

Loading