Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added YAML configuration reader (#271)
- Loading branch information
1 parent
65c50fe
commit 07b8417
Showing
7 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* Aphiria | ||
* | ||
* @link https://www.aphiria.com | ||
* @copyright Copyright (C) 2023 David Young | ||
* @license https://github.com/aphiria/aphiria/blob/1.x/LICENSE.md | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aphiria\Application\Configuration; | ||
|
||
use Symfony\Component\Yaml\Exception\ParseException; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* Defines the configuration reader that reads YAML files | ||
* | ||
*/ | ||
class YamlConfigurationFileReader implements IConfigurationFileReader | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function readConfiguration(string $path, string $pathDelimiter = '.'): IConfiguration | ||
{ | ||
if (!\file_exists($path)) { | ||
throw new InvalidConfigurationFileException("$path does not exist"); | ||
} | ||
|
||
try { | ||
$hashTable = Yaml::parseFile($path); | ||
|
||
if (!\is_array($hashTable) || \array_is_list($hashTable)) { | ||
throw new InvalidConfigurationFileException("YAML in $path does not parse to an associative array"); | ||
} | ||
} catch (ParseException $ex) { | ||
throw new InvalidConfigurationFileException("Invalid YAML in $path"); | ||
} | ||
|
||
/** @var array<string, mixed> $hashTable */ | ||
return new HashTableConfiguration($hashTable, $pathDelimiter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/** | ||
* Aphiria | ||
* | ||
* @link https://www.aphiria.com | ||
* @copyright Copyright (C) 2023 David Young | ||
* @license https://github.com/aphiria/aphiria/blob/1.x/LICENSE.md | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aphiria\Application\Tests\Configuration; | ||
|
||
use Aphiria\Application\Configuration\InvalidConfigurationFileException; | ||
use Aphiria\Application\Configuration\YamlConfigurationFileReader; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class YamlConfigurationFileReaderTest extends TestCase | ||
{ | ||
private YamlConfigurationFileReader $reader; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->reader = new YamlConfigurationFileReader(); | ||
} | ||
|
||
public function testEmptyPathDelimiterThrowsException(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Path delimiter cannot be empty'); | ||
/** @psalm-suppress InvalidArgument Purposely testing an empty path delimiter */ | ||
$this->reader->readConfiguration(__DIR__ . '/files/configuration.yaml', ''); | ||
} | ||
|
||
public function testReadingConfigurationCreatesConfigurationFromContentsOfYamlFile(): void | ||
{ | ||
$configuration = $this->reader->readConfiguration(__DIR__ . '/files/configuration.yaml'); | ||
$this->assertSame('bar', $configuration->getString('foo')); | ||
} | ||
|
||
public function testReadingConfigurationWithCustomDelimiterAllowsAccessWithThatDelimiter(): void | ||
{ | ||
$configuration = $this->reader->readConfiguration(__DIR__ . '/files/configuration-delimiter.yaml', ':'); | ||
$this->assertSame('baz', $configuration->getString('foo:bar')); | ||
} | ||
|
||
public function testReadingInvalidYamlThrowsException(): void | ||
{ | ||
$path = __DIR__ . '/files/invalid-configuration.yaml'; | ||
$this->expectException(InvalidConfigurationFileException::class); | ||
$this->expectExceptionMessage("Invalid YAML in $path"); | ||
$this->reader->readConfiguration($path); | ||
} | ||
|
||
public function testReadingNonExistentPathThrowsException(): void | ||
{ | ||
$this->expectException(InvalidConfigurationFileException::class); | ||
$this->expectExceptionMessage('/doesnotexist does not exist'); | ||
$this->reader->readConfiguration('/doesnotexist'); | ||
} | ||
|
||
public function testReadingYamlThatDoesNotMapToAssociativeArrayThrowsException(): void | ||
{ | ||
$path = __DIR__ . '/files/non-hash-table.yaml'; | ||
$this->expectException(InvalidConfigurationFileException::class); | ||
$this->expectExceptionMessage("YAML in $path does not parse to an associative array"); | ||
$this->reader->readConfiguration($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
foo: | ||
bar: baz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo: bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |