Skip to content

Commit

Permalink
redcap test - configuration parser/config file stub
Browse files Browse the repository at this point in the history
  • Loading branch information
regisoc committed Nov 20, 2024
1 parent 537f84f commit 6b1d5d3
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 0 deletions.
30 changes: 30 additions & 0 deletions modules/redcap/test/config/configCorrect.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
<!-- REDCap Client config -->
<REDCap>
<!-- Should be aassigneeisting userID -->
<issuesAssignee>assignee</issuesAssignee>
<instance>
<name>aaa</name>
<url>https://someURL.com</url>
<!-- one instance can have multiple projects -->
<project>
<projectID>111</projectID>
<token>token_1</token>
</project>
<project>
<projectID>222</projectID>
<token>token_2</token>
</project>
</instance>
<instance>
<name>bbb</name>
<url>https://someOtherURL.com</url>
<!-- one instance can have multiple projects -->
<project>
<projectID>333</projectID>
<token>token_3</token>
</project>
</instance>
</REDCap>
</config>
13 changes: 13 additions & 0 deletions modules/redcap/test/config/configNoAssignee.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
<REDCap>
<instance>
<name>aaa</name>
<url>https://someURL.com</url>
<project>
<projectID>111</projectID>
<token>token_1</token>
</project>
</instance>
</REDCap>
</config>
6 changes: 6 additions & 0 deletions modules/redcap/test/config/configNoInstance.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
<REDCap>
<issuesAssignee>admin</issuesAssignee>
</REDCap>
</config>
10 changes: 10 additions & 0 deletions modules/redcap/test/config/configNoProject.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
<REDCap>
<issuesAssignee>admin</issuesAssignee>
<instance>
<name>aaa</name>
<url>https://someURL.com</url>
</instance>
</REDCap>
</config>
3 changes: 3 additions & 0 deletions modules/redcap/test/config/configNoREDCap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
</config>
226 changes: 226 additions & 0 deletions modules/redcap/test/redcapconfigurationparser_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php declare(strict_types=1);

/**
* This contains tests relevant to the REDCap module - configuration parser section
*
* PHP Version 8
*
* @category REDCap
* @package Tests
* @author Regis Ongaro-Carcy <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
namespace LORIS\api\Test;

require_once __DIR__ . "/../../../tools/generic_includes.php";

use \PHPUnit\Framework\TestCase;

use LORIS\redcap\configurations\RedcapConfiguration;
use LORIS\redcap\configurations\RedcapConfigurationParser;

/**
* PHPUnit class for REDCap Configuration tests
*
* @category REDCap
* @package Tests
* @author Regis Ongaro-Carcy <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
class redcapconfigurationparser_Test extends TestCase
{
private static ?\LORIS\LorisInstance $_loris;

private static string $RBConfigFile = __DIR__ . "/../../../raisinbread/config/config.xml";

/**
* Provide an autoloader for the redcap module namespace.
*
* @return void
*/
public static function setUpBeforeClass(): void
{
// force reset the configInstance for this test.
\NDB_Config::$configInstance = null;
// default RB config file
self::$_loris = new \LORIS\LorisInstance(
\NDB_Factory::singleton()->database(),
\NDB_Config::singleton(self::$RBConfigFile),
[
__DIR__ . "/../../../project/modules",
__DIR__ . "/../../../modules/",
],
);

// load redcap module to use the client
self::$_loris->getModule('redcap')->registerAutoloader();
}

/**
* Set up.
*
* @return void
*/
public function setUp(): void
{
// force reset the configInstance for this test.
\NDB_Config::$configInstance = null;
// default RB config file
self::$_loris = new \LORIS\LorisInstance(
\NDB_Factory::singleton()->database(),
\NDB_Config::singleton(self::$RBConfigFile),
[
__DIR__ . "/../../../project/modules",
__DIR__ . "/../../../modules/",
],
);
}

/**
* Tear down
*
* @return void
*/
public function tearDown(): void
{
self::$_loris = null;
}

public static function setConfigFile(string $configFile): \LORIS\LorisInstance
{
if (empty($configFile)) {
throw new \Error("[redcap][test] New config file cannot be empty.");
}
// force reset the configInstance for this test.
\NDB_Config::$configInstance = null;

// default RB config file
return new \LORIS\LorisInstance(
\NDB_Factory::singleton()->database(),
\NDB_Config::singleton($configFile),
[
__DIR__ . "/../../../project/modules",
__DIR__ . "/../../../modules/",
],
);
}

// --------------------------------------------------

/**
*
* @return void
*/
public function testNonInstanciable(): void
{
$this->expectException(\Error::class);
// default to false.
$rcp = new RedcapConfigurationParser(self::$_loris);
$rcp = new RedcapConfigurationParser(self::$_loris, false);
$rcp = new RedcapConfigurationParser(self::$_loris, true);
}

/**
*
* @return void
*/
public function testNoDefaultREDCapInstance(): void
{
// default file does not have any accessible REDCap
$this->expectException(\LorisException::class);
$this->expectExceptionMessage("[redcap][config] none of the REDCap"
. " configurations declared in 'config.xml' file can be accessed.");
RedcapConfigurationParser::factory(self::$_loris);
}

/**
*
* @return void
*/
public function testSingleton(): void
{
// default file does not have any accessible REDCap
$this->expectException(\LorisException::class);
$s1 = RedcapConfigurationParser::factory(self::$_loris);
$s2 = RedcapConfigurationParser::factory(self::$_loris);
$this->assertSame($s1, $s2);
}

/**
*
* @return void
*/
public function testSingletonVerbose(): void
{
$this->expectException(\LorisException::class);
$this->expectExceptionMessage("[redcap][config] none of the REDCap"
. " configurations declared in 'config.xml' file can be accessed.");
// verbose is not an attribute of this class.
$s1 = RedcapConfigurationParser::factory(self::$_loris, false);
$s2 = RedcapConfigurationParser::factory(self::$_loris, true);
$this->assertSame($s1, $s2);
}

/**
*
* @return void
*/
public function testNoREDCapTagConfigFile(): void
{
// TODO: this is not expected and should be changed in the NDB_Config file.
$this->expectException(\TypeError::class);
$this->expectExceptionMessage("Unsupported operand types: array + string");
$configFile = __DIR__ . "/config/configNoREDCap.xml";
$lorisInstance = self::setConfigFile($configFile);
//
RedcapConfigurationParser::factory($lorisInstance);
}

/**
*
* @return void
*/
public function testNoAssigneeConfigFile(): void
{
$this->expectException(\LorisException::class);
$this->expectExceptionMessage("[redcap][init] no REDCap 'issuesAssignee'"
. " in configuration.");
$configFile = __DIR__ . "/config/configNoAssignee.xml";
$lorisInstance = self::setConfigFile($configFile);
//
RedcapConfigurationParser::factory($lorisInstance);
}

/**
*
* @return void
*/
public function testNoInstanceConfigFile(): void
{
$this->expectException(\LorisException::class);
$this->expectExceptionMessage("[redcap][init] no REDCap instance in"
. " configuration.");
$configFile = __DIR__ . "/config/configNoInstance.xml";
$lorisInstance = self::setConfigFile($configFile);
//
RedcapConfigurationParser::factory($lorisInstance);
}

/**
*
* @return void
*/
public function testNoProjectConfigFile(): void
{
$this->expectException(\LorisException::class);
$this->expectExceptionMessage("[redcap][init] wrong REDCap configuration:"
. " no projects for instance 'aaa'.");
$configFile = __DIR__ . "/config/configNoProject.xml";
$lorisInstance = self::setConfigFile($configFile);
//
RedcapConfigurationParser::factory($lorisInstance);
}
}

0 comments on commit 6b1d5d3

Please sign in to comment.