Skip to content
Snippets Groups Projects
Commit c72f0506 authored by Demian Katz's avatar Demian Katz
Browse files

Refactor YAML loading to improve test coverage.

parent cc78da3f
No related merge requests found
...@@ -74,39 +74,75 @@ class SearchSpecsReader ...@@ -74,39 +74,75 @@ class SearchSpecsReader
{ {
// Load data if it is not already in the object's cache: // Load data if it is not already in the object's cache:
if (!isset($this->searchSpecs[$filename])) { if (!isset($this->searchSpecs[$filename])) {
// Connect to searchspecs cache: $this->searchSpecs[$filename] = $this->getFromPaths(
$cache = (null !== $this->cacheManager) Locator::getBaseConfigPath($filename),
? $this->cacheManager->getCache('searchspecs') : false; Locator::getLocalConfigPath($filename)
);
}
return $this->searchSpecs[$filename];
}
/**
* Given core and local filenames, retrieve the searchspecs data.
*
* @param string $defaultFile Full path to file containing default YAML
* @param string $customFile Full path to file containing local customizations
* (may be null if no local file exists).
*
* @return array
*/
protected function getFromPaths($defaultFile, $customFile = null)
{
// Connect to searchspecs cache:
$cache = (null !== $this->cacheManager)
? $this->cacheManager->getCache('searchspecs') : false;
// Determine full configuration file path: // Generate cache key:
$fullpath = Locator::getBaseConfigPath($filename); $cacheKey = basename($defaultFile) . '-'
$local = Locator::getLocalConfigPath($filename); . (file_exists($defaultFile) ? filemtime($defaultFile) : 0);
if (!empty($customFile)) {
$cacheKey .= '-local-' . filemtime($customFile);
}
$cacheKey = md5($cacheKey);
// Generate cache key: // Generate data if not found in cache:
$cacheKey = $filename . '-' if ($cache === false || !($results = $cache->getItem($cacheKey))) {
. (file_exists($fullpath) ? filemtime($fullpath) : 0); $results = $this->parseYaml($customFile, $defaultFile);
if (!empty($local)) { if ($cache !== false) {
$cacheKey .= '-local-' . filemtime($local); $cache->setItem($cacheKey, $results);
} }
$cacheKey = md5($cacheKey); }
// Generate data if not found in cache: return $results;
if ($cache === false || !($results = $cache->getItem($cacheKey))) { }
$results = file_exists($fullpath)
? Yaml::parse(file_get_contents($fullpath)) : []; /**
if (!empty($local)) { * Process a YAML file (and its parent, if necessary).
$localResults = Yaml::parse(file_get_contents($local)); *
foreach ($localResults as $key => $value) { * @param string $file YAML file to load (will evaluate to empty array
$results[$key] = $value; * if file does not exist).
} * @param string $defaultParent Parent YAML file from which $file should
} * inherit (unless overridden by a specific directive in $file). None by
if ($cache !== false) { * default.
$cache->setItem($cacheKey, $results); *
* @return array
*/
protected function parseYaml($file, $defaultParent = null)
{
// First load current file:
$results = (!empty($file) && file_exists($file))
? Yaml::parse(file_get_contents($file)) : [];
// Now load in missing sections from parent, if applicable:
if (null !== $defaultParent) {
foreach ($this->parseYaml($defaultParent) as $section => $contents) {
if (!isset($results[$section])) {
$results[$section] = $contents;
} }
} }
$this->searchSpecs[$filename] = $results;
} }
return $this->searchSpecs[$filename]; return $results;
} }
} }
top:
foo: "bar"
bottom:
goo: "gar"
\ No newline at end of file
top:
foo: "xyzzy"
middle:
moo: "cow"
\ No newline at end of file
...@@ -68,4 +68,50 @@ class SearchSpecsReaderTest extends \VuFindTest\Unit\TestCase ...@@ -68,4 +68,50 @@ class SearchSpecsReaderTest extends \VuFindTest\Unit\TestCase
$specs = $reader->get('notreallyasearchspecs.yaml'); $specs = $reader->get('notreallyasearchspecs.yaml');
$this->assertEquals([], $specs); $this->assertEquals([], $specs);
} }
/**
* Test direct loading of two single files.
*
* @return void
*/
public function testYamlLoad()
{
$reader = new SearchSpecsReader();
$core = __DIR__ . '/../../../../fixtures/configs/yaml/core.yaml';
$local = __DIR__ . '/../../../../fixtures/configs/yaml/local.yaml';
$this->assertEquals(
[
'top' => ['foo' => 'bar'],
'bottom' => ['goo' => 'gar'],
],
$this->callMethod($reader, 'getFromPaths', [$core])
);
$this->assertEquals(
[
'top' => ['foo' => 'xyzzy'],
'middle' => ['moo' => 'cow'],
],
$this->callMethod($reader, 'getFromPaths', [$local])
);
}
/**
* Test merging of two files.
*
* @return void
*/
public function testYamlMerge()
{
$reader = new SearchSpecsReader();
$core = __DIR__ . '/../../../../fixtures/configs/yaml/core.yaml';
$local = __DIR__ . '/../../../../fixtures/configs/yaml/local.yaml';
$this->assertEquals(
[
'top' => ['foo' => 'xyzzy'],
'middle' => ['moo' => 'cow'],
'bottom' => ['goo' => 'gar'],
],
$this->callMethod($reader, 'getFromPaths', [$core, $local])
);
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment