Skip to content

Commit

Permalink
Removed external constants from classes, making code more testable
Browse files Browse the repository at this point in the history
  • Loading branch information
hernanrz committed Mar 6, 2016
1 parent 9e30a8e commit 1efcddc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion emulator/emulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

echo '* Reading configuration file ' . basename($configFileName) . '...' . PHP_EOL;

$confLoader = new Loader();
$confLoader = new Loader(EMU_PATH);

try {
$configuration = $confLoader->loadFromFile($configFileName);
Expand Down
15 changes: 13 additions & 2 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@
*/
class Configuration extends DefaultConfiguration
{

/**
* @var string The directory in which every file is located
*/
private $directory;

function __construct($directory)
{
$this->directory = $directory . DIRECTORY_SEPARATOR;
}

/**
* Checks if the dataLocation provided is valid (i.e. files exist)
*/
public function onDataLocationSet($dataLocation)
{
if(is_array($dataLocation)) {
$dataLocation = array_map(function($path) {
return EMU_PATH . DIRECTORY_SEPARATOR . $path;
return $this->directory . $path;
}, $dataLocation);

foreach($dataLocation as $location) {
Expand All @@ -24,7 +35,7 @@ public function onDataLocationSet($dataLocation)
}

}else {
$dataLocation = EMU_PATH . DIRECTORY_SEPARATOR . $dataLocation;
$dataLocation = $this->directory . $dataLocation;

if(!file_exists($dataLocation)) {
throw new \Exception('Could not find file ' . $dataLocation);
Expand Down
14 changes: 12 additions & 2 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ class Loader
];

private $missingOptions = [];


/**
* @var string The directory in which every file is located
*/
private $directory;

function __construct($directory = null)
{
$this->directory = (null == $directory) ? __DIR__ : $directory;
}

/**
* Loads a JSON file with settings
*
Expand All @@ -38,7 +48,7 @@ public function loadFromFile($filePath)
*/
public function loadFromArray(array $configOptions)
{
$configuration = new Configuration();
$configuration = new Configuration($this->directory);

foreach($configOptions as $key => $val) {
$configuration->set($key, $val);
Expand Down
3 changes: 2 additions & 1 deletion tests/testing.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"data": "yes",
"dataLocation": ["tests\\testfile.txt"],
"loadOnce": false
"loadOnce": false,
"sourceType": "file"
}

0 comments on commit 1efcddc

Please sign in to comment.