From 0cbd969187980406a51909df6d519086a0233f7d Mon Sep 17 00:00:00 2001 From: Ben Selby Date: Sat, 14 Apr 2012 21:48:39 +0100 Subject: [PATCH] Add the ability to have a json configuration file --- src/Cilex/Provider/ConfigServiceProvider.php | 3 + .../Provider/ConfigServerProviderTest.php | 95 +++++++++++++++++++ tests/data/config.json | 3 + tests/data/config.unknownfiletype | 0 4 files changed, 101 insertions(+) create mode 100644 tests/Cilex/Tests/Provider/ConfigServerProviderTest.php create mode 100644 tests/data/config.json create mode 100644 tests/data/config.unknownfiletype diff --git a/src/Cilex/Provider/ConfigServiceProvider.php b/src/Cilex/Provider/ConfigServiceProvider.php index 324d7fc..045a9fb 100644 --- a/src/Cilex/Provider/ConfigServiceProvider.php +++ b/src/Cilex/Provider/ConfigServiceProvider.php @@ -39,6 +39,9 @@ public function register(Application $app) case 'xml': $result = simplexml_load_file($app['config.path']); break; + case 'json': + $result = json_decode(file_get_contents($app['config.path'])); + break; default: throw new \InvalidArgumentException( 'Unable to load configuration; the provided file ' diff --git a/tests/Cilex/Tests/Provider/ConfigServerProviderTest.php b/tests/Cilex/Tests/Provider/ConfigServerProviderTest.php new file mode 100644 index 0000000..d4fee80 --- /dev/null +++ b/tests/Cilex/Tests/Provider/ConfigServerProviderTest.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @author Ben Selby + */ + +namespace Cilex\Tests\Provider; + +use Cilex\Application; +use Cilex\Provider\ConfigServiceProvider; + +/** + * Test file for ConfigServiceProvider + * + * @author Ben Selby + */ +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']; + } +} diff --git a/tests/data/config.json b/tests/data/config.json new file mode 100644 index 0000000..a977d99 --- /dev/null +++ b/tests/data/config.json @@ -0,0 +1,3 @@ +{ + "key": "value" +} \ No newline at end of file diff --git a/tests/data/config.unknownfiletype b/tests/data/config.unknownfiletype new file mode 100644 index 0000000..e69de29