Wrote some tests
This commit is contained in:
parent
a9e7b22989
commit
f9b36b68e5
|
@ -5,8 +5,7 @@
|
|||
"require": {
|
||||
"php": ">=7.1",
|
||||
"dflydev/dot-access-data": "^1.1",
|
||||
"symfony/filesystem": "^3.3"
|
||||
|
||||
"symfony/config": "^3.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
|
@ -5,7 +5,9 @@
|
|||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
|
||||
convertErrorsToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
failOnWarning="false"
|
||||
colors="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
|
|
|
@ -25,12 +25,14 @@ use Sikofitt\Config\Loader\JsonFileLoader;
|
|||
use Sikofitt\Config\Loader\NeonFileLoader;
|
||||
use Sikofitt\Config\Loader\PhpFileLoader;
|
||||
use Sikofitt\Config\Loader\YamlFileLoader;
|
||||
use Symfony\Component\Config\Exception\FileLoaderLoadException;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\Config\Loader\DelegatingLoader;
|
||||
use Symfony\Component\Config\Loader\GlobFileLoader;
|
||||
use Symfony\Component\Config\Loader\LoaderResolver;
|
||||
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Webmozart\Json\JsonDecoder;
|
||||
|
||||
/**
|
||||
* Class Configuration
|
||||
|
@ -56,6 +58,7 @@ class DotConfig
|
|||
$this->arrayConfig = [];
|
||||
|
||||
$locator = new FileLocator();
|
||||
|
||||
$loaderResolver = new LoaderResolver();
|
||||
$loaderResolver->addLoader(new PhpFileLoader($locator));
|
||||
$loaderResolver->addLoader(new JsonFileLoader($locator));
|
||||
|
@ -73,6 +76,7 @@ class DotConfig
|
|||
|
||||
if (is_array($configResource)) {
|
||||
$configResource = $this->loadDirectories($configResource);
|
||||
|
||||
$configResource = $this->loadFiles($configResource);
|
||||
|
||||
$configResource = array_shift($configResource);
|
||||
|
@ -85,7 +89,11 @@ class DotConfig
|
|||
}
|
||||
} else {
|
||||
if (is_file($configResource)) {
|
||||
$this->arrayConfig = $this->delegateLoader->load($configResource);
|
||||
try {
|
||||
$this->arrayConfig = $this->delegateLoader->load($configResource);
|
||||
} catch (FileLoaderLoadException $f) {
|
||||
$this->handleException($f, $configResource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -288,6 +296,32 @@ class DotConfig
|
|||
return $this->config->has($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function append($key, $value): void
|
||||
{
|
||||
$this->config->append($key, $value);
|
||||
}
|
||||
|
||||
private function handleException(\Throwable $exception, string $file): void
|
||||
{
|
||||
switch (pathinfo($file, PATHINFO_EXTENSION)) {
|
||||
case 'neon':
|
||||
$message = sprintf('%s. Perhaps you forgot to require nette/neon?', $exception->getMessage());
|
||||
break;
|
||||
case 'yaml':
|
||||
case 'yml':
|
||||
$message = sprintf('%s. Perhaps you forgot to require symfony/yaml?', $exception->getMessage());
|
||||
break;
|
||||
default:
|
||||
$message = sprintf('%s.', $exception->getMessage());
|
||||
}
|
||||
|
||||
trigger_error($message, E_USER_WARNING);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $directories
|
||||
*
|
||||
|
@ -298,14 +332,19 @@ class DotConfig
|
|||
{
|
||||
foreach ($directories as $key => $config) {
|
||||
if (is_string($config) && is_dir($config)) {
|
||||
$this->arrayConfig = array_merge_recursive(
|
||||
$this->arrayConfig,
|
||||
$this->delegateLoader->load($config . '/**', 'glob')
|
||||
);
|
||||
unset($directories[$key]);
|
||||
$this->arrayConfig = $this->mergeGlob($this->arrayConfig);
|
||||
try {
|
||||
$this->arrayConfig = array_merge_recursive(
|
||||
$this->arrayConfig,
|
||||
$this->delegateLoader->load($config . '/**', 'glob')
|
||||
);
|
||||
unset($directories[$key]);
|
||||
$this->arrayConfig = $this->mergeGlob($this->arrayConfig);
|
||||
} catch (FileLoaderLoadException $f) {
|
||||
$this->handleException($f, $config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $directories;
|
||||
}
|
||||
|
||||
|
@ -319,8 +358,15 @@ class DotConfig
|
|||
{
|
||||
foreach ($files as $key => $config) {
|
||||
if (is_string($config) && is_file($config)) {
|
||||
$this->arrayConfig = array_merge_recursive($this->arrayConfig, $this->delegateLoader->load($config));
|
||||
unset($files[$key]);
|
||||
try {
|
||||
$this->arrayConfig = array_merge_recursive(
|
||||
$this->arrayConfig,
|
||||
$this->delegateLoader->load($config)
|
||||
);
|
||||
unset($files[$key]);
|
||||
} catch (FileLoaderLoadException $f) {
|
||||
$this->handleException($f, $config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ class JsonDecodingException extends \ErrorException
|
|||
$severity = E_RECOVERABLE_ERROR,
|
||||
$fileName = __FILE__,
|
||||
$line = __LINE__,
|
||||
Throwable $previous
|
||||
Throwable $previous = null
|
||||
) {
|
||||
parent::__construct(
|
||||
json_last_error_msg(),
|
||||
|
|
|
@ -19,11 +19,14 @@
|
|||
|
||||
namespace Sikofitt\Config\Loader;
|
||||
|
||||
use Sikofitt\Config\Loader\Exception\JsonDecodingException;
|
||||
use Symfony\Component\Config\Loader\FileLoader;
|
||||
use Webmozart\Json\JsonDecoder;
|
||||
|
||||
class JsonFileLoader extends FileLoader
|
||||
{
|
||||
private $useWebMozartEncoder = true;
|
||||
|
||||
/**
|
||||
* Loads a resource.
|
||||
*
|
||||
|
@ -36,15 +39,27 @@ class JsonFileLoader extends FileLoader
|
|||
*/
|
||||
public function load($resource, $type = null)
|
||||
{
|
||||
if (class_exists('Webmozart\Json\JsonDecoder')) {
|
||||
$decoder = new JsonDecoder();
|
||||
|
||||
return (array)$decoder->decodeFile($resource);
|
||||
if (false === getenv('JSON_TEST')) {
|
||||
if ($this->useWebMozartEncoder && class_exists('Webmozart\Json\JsonDecoder')) {
|
||||
$decoder = new JsonDecoder();
|
||||
$decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY);
|
||||
return $decoder->decodeFile($resource);
|
||||
}
|
||||
}
|
||||
|
||||
return \json_decode(file_get_contents($resource), true);
|
||||
if (null === $json = \json_decode(file_get_contents($resource), true)) {
|
||||
throw new JsonDecodingException();
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
public function setUseWebMozartDecoder($enable = true)
|
||||
{
|
||||
$this->useWebMozartEncoder = $enable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Returns whether this class supports the given resource.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyleft (C) 2017 http://sikofitt.com sikofitt@gmail.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Sikofitt\Tests\Config;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Sikofitt\Config\DotConfig;
|
||||
use Sikofitt\Config\Loader\Exception\JsonDecodingException;
|
||||
|
||||
class JsonFileTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var DotConfig
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Composer\Autoload\ClassLoader;
|
||||
*/
|
||||
private $loader;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->loader = require __DIR__.'/../../../../vendor/autoload.php';
|
||||
|
||||
$this->config = new DotConfig(__DIR__.'/../../../fixtures/config.json');
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testSuccess(): void
|
||||
{
|
||||
$arrayConfig['config'] = [
|
||||
'value' => 'json',
|
||||
'json_test' => [
|
||||
'testing1',
|
||||
'testing2',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($arrayConfig, $this->config->all());
|
||||
$this->assertInternalType('array', $this->config->all());
|
||||
}
|
||||
|
||||
public function testFailure(): void
|
||||
{
|
||||
if (false === getenv('JSON_TEST') && class_exists(\Webmozart\Json\JsonDecoder::class)) {
|
||||
$this->expectException(\Webmozart\Json\DecodingFailedException::class);
|
||||
} else {
|
||||
$this->expectException(JsonDecodingException::class);
|
||||
}
|
||||
|
||||
$config = new DotConfig(__DIR__.'/../../../fixtures/failure/config.json');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyleft (C) 2017 http://sikofitt.com sikofitt@gmail.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Sikofitt\Tests\Config;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Sikofitt\Config\DotConfig;
|
||||
|
||||
class NeonFileTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var DotConfig
|
||||
*/
|
||||
private $config;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->config = new DotConfig(__DIR__.'/../../../fixtures/config.neon');
|
||||
$this->setUseErrorHandler(true);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testNeonConfig(): void
|
||||
{
|
||||
$arrayConfig['config'] = [
|
||||
'value' => 'neon',
|
||||
'neon_test' => [
|
||||
'testing1',
|
||||
'testing2',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($arrayConfig, $this->config->all());
|
||||
$this->assertInternalType('array', $this->config->all());
|
||||
}
|
||||
|
||||
public function testFailure(): void
|
||||
{
|
||||
$this->expectException('Nette\Neon\Exception');
|
||||
|
||||
$config = new DotConfig(__DIR__.'/../../../fixtures/failure/config.neon');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyleft (C) 2017 http://sikofitt.com sikofitt@gmail.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Sikofitt\Tests\Config;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Sikofitt\Config\DotConfig;
|
||||
|
||||
class PhpFileTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var DotConfig
|
||||
*/
|
||||
private $config;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->config = new DotConfig(__DIR__.'/../../../fixtures/config.php');
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testPhpConfig(): void
|
||||
{
|
||||
$arrayConfig['config'] = [
|
||||
'value' => 'php',
|
||||
'php_test' => [
|
||||
'testing1',
|
||||
'testing2',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($arrayConfig, $this->config->all());
|
||||
$this->assertInternalType('array', $this->config->all());
|
||||
}
|
||||
|
||||
public function testFailure(): void
|
||||
{
|
||||
$this->expectException(\TypeError::class);
|
||||
|
||||
$config = new DotConfig(__DIR__.'/../../../fixtures/failure/config.php');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
config:
|
||||
value: 'yaml'
|
||||
yaml_test:
|
||||
- testing
|
||||
- testing1
|
|
@ -0,0 +1,4 @@
|
|||
[config]
|
||||
value = ini
|
||||
ini_test[] = testing1
|
||||
ini_test[] = testing2
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
"config": {
|
||||
"value": "json",
|
||||
"json_test": [
|
||||
"testing1",
|
||||
"testing2"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
config:
|
||||
value: neon
|
||||
neon_test:
|
||||
- testing1
|
||||
- testing2
|
|
@ -0,0 +1,28 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyleft (C) 2017 http://sikofitt.com sikofitt@gmail.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
$arrayValue = [
|
||||
'config' => [
|
||||
'value' => 'php',
|
||||
'php_test' => [
|
||||
'testing1',
|
||||
'testing2',
|
||||
],
|
||||
],
|
||||
];
|
|
@ -0,0 +1,5 @@
|
|||
config:
|
||||
value: 'yaml'
|
||||
yaml_test:
|
||||
- testing
|
||||
- testing1
|
Loading…
Reference in New Issue