Skip to content

Commit

Permalink
Add the ability to have a json configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
benmatselby committed Apr 15, 2012
1 parent a187b03 commit 0cbd969
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Cilex/Provider/ConfigServiceProvider.php
Expand Up @@ -39,6 +39,9 @@ public function register(Application $app)
case 'xml': case 'xml':
$result = simplexml_load_file($app['config.path']); $result = simplexml_load_file($app['config.path']);
break; break;
case 'json':
$result = json_decode(file_get_contents($app['config.path']));
break;
default: default:
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Unable to load configuration; the provided file ' 'Unable to load configuration; the provided file '
Expand Down
95 changes: 95 additions & 0 deletions tests/Cilex/Tests/Provider/ConfigServerProviderTest.php
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of the Cilex framework.
*
* (c) Mike van Riel <mike.vanriel@naenius.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Ben Selby <benmatselby@gmail.com>
*/

namespace Cilex\Tests\Provider;

use Cilex\Application;
use Cilex\Provider\ConfigServiceProvider;

/**
* Test file for ConfigServiceProvider
*
* @author Ben Selby <benmatselby@gmail.com>
*/
class ConfigServiceProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* Test that an exception is thrown if the config path is not present
* or valid
*
* @return void
*/
public function testRegisterWillThrowExceptionIfConfigPathIsNotThere()
{
$this->setExpectedException(
'InvalidArgumentException',
__DIR__.'/../../../data/unknownfile is not a valid path to the configuration'
);

$app = new Application('Test');

$app->register(
new ConfigServiceProvider(),
array(
'config.path' => __DIR__.'/../../../data/unknownfile'
)
);

$config = $app['config'];
}

/**
* Test that the config provider can parse a json
* configuration file
*
* @return void
*/
public function testRegisterCanParseAJsonConfigFile()
{
$app = new Application('Test');

$app->register(
new ConfigServiceProvider(),
array(
'config.path' => __DIR__.'/../../../data/config.json'
)
);

$config = $app['config'];
$this->assertEquals($config->key, 'value');
}

/**
* Test that register will throw an exception if an unknown
* format is passed in
*
* @return void
*/
public function testRegisterThrowsExceptionIfAnUnknownFormatIsPassed()
{
$this->setExpectedException(
'InvalidArgumentException',
'Unable to load configuration; the provided file extension was not recognized. Only yml or xml allowed'
);

$app = new Application('Test');

$app->register(
new ConfigServiceProvider(),
array(
'config.path' => __DIR__.'/../../../data/config.unknownfiletype'
)
);

$config = $app['config'];
}
}
3 changes: 3 additions & 0 deletions tests/data/config.json
@@ -0,0 +1,3 @@
{
"key": "value"
}
Empty file.

0 comments on commit 0cbd969

Please sign in to comment.