Permalink
Browse files

Add the ability to have a json configuration file

  • Loading branch information...
1 parent a187b03 commit 0cbd969187980406a51909df6d519086a0233f7d @benmatselby benmatselby committed Apr 14, 2012
@@ -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 '
@@ -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'];
+ }
+}
@@ -0,0 +1,3 @@
+{
+ "key": "value"
+}
No changes.

0 comments on commit 0cbd969

Please sign in to comment.