Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Changes to fetchConfigData to increase flexibility and avoid testing conflicts with multiple JConfig classes. #440

Merged
merged 1 commit into from

2 participants

ianmacl Louis Landry
ianmacl

Currently tests bail depending on the order the tests are executed in because of the existence of multiple JConfig classes. This change improves the flexibility of the classes that use it so we can avoid the problem.

Louis Landry LouisLandry merged commit ab1e703 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 19, 2011
  1. ianmacl

    Modified behaviour of fetchConfigurationData to improve flexibility a…

    ianmacl authored
    …nd avoid conflicts within unit tests.
This page is out of date. Refresh to see the latest.
2  libraries/joomla/application/application.php
View
@@ -978,7 +978,7 @@ protected function _createConfiguration($file)
{
jimport('joomla.registry.registry');
- include_once $file;
+ JLoader::register('JConfig', $file);
// Create the JConfig object.
$config = new JConfig;
40 libraries/joomla/application/cli.php
View
@@ -334,38 +334,42 @@ public function set($key, $value = null)
* will extend this method in child classes to provide configuration data from whatever data source is relevant
* for your specific application.
*
- * @param string $fileName The name of the configuration file (default is 'configuration').
- * Note that .php is appended to this name
+ * @param string $file The path and filename of the configuration file. If not provided, configuration.php
+ * in JPATH_BASE will be used.
+ * @param string $class The class name to instantiate.
*
* @return mixed Either an array or object to be loaded into the configuration object.
*
* @since 11.1
*/
- protected function fetchConfigurationData($fileName = 'configuration')
+ protected function fetchConfigurationData($file = '', $class = 'JConfig')
{
// Instantiate variables.
$config = array();
- if (empty($fileName))
+ if (empty($file) && defined('JPATH_BASE'))
{
- $fileName = 'configuration';
+ $file = JPATH_BASE . '/configuration.php';
+
+ // Applications can choose not to have any configuration data
+ // by not implementing this method and not having a config file.
+ if (!file_exists($file))
+ {
+ $file = '';
+ }
}
- // Handle the convention-based default case for configuration file.
- if (defined('JPATH_BASE'))
+ if (!empty($file))
{
- // Set the configuration file name and check to see if it exists.
- $file = JPATH_BASE . '/' . preg_replace('#[^A-Z0-9-_.]#i', '', $fileName) . '.php';
- if (is_file($file))
+ JLoader::register($class, $file);
+
+ if (class_exists($class))
+ {
+ $config = new $class;
+ }
+ else
{
- // Import the configuration file.
- include_once $file;
-
- // Instantiate the configuration object if it exists.
- if (class_exists('JConfig'))
- {
- $config = new JConfig;
- }
+ throw new Exception('Configuration class does not exist.');
}
}
40 libraries/joomla/application/web.php
View
@@ -977,38 +977,42 @@ protected function detectRequestUri()
* will extend this method in child classes to provide configuration data from whatever data source is relevant
* for your specific application.
*
- * @param string $fileName The name of the configuration file (default is 'configuration').
- * Note that .php is appended to this name
+ * @param string $file The path and filename of the configuration file. If not provided, configuration.php
+ * in JPATH_BASE will be used.
+ * @param string $class The class name to instantiate.
*
* @return mixed Either an array or object to be loaded into the configuration object.
*
* @since 11.3
*/
- protected function fetchConfigurationData($fileName = 'configuration')
+ protected function fetchConfigurationData($file = '', $class = 'JConfig')
{
// Instantiate variables.
$config = array();
- if (empty($fileName))
+ if (empty($file) && defined('JPATH_BASE'))
{
- $fileName = 'configuration';
+ $file = JPATH_BASE . '/configuration.php';
+
+ // Applications can choose not to have any configuration data
+ // by not implementing this method and not having a config file.
+ if (!file_exists($file))
+ {
+ $file = '';
+ }
}
- // Handle the convention-based default case for configuration file.
- if (defined('JPATH_BASE'))
+ if (!empty($file))
{
- // Set the configuration file name and check to see if it exists.
- $file = JPATH_BASE . '/' . preg_replace('#[^A-Z0-9-_.]#i', '', $fileName) . '.php';
- if (is_file($file))
- {
- // Import the configuration file.
- include_once $file;
+ JLoader::register($class, $file);
- // Instantiate the configuration object if it exists.
- if (class_exists('JConfig'))
- {
- $config = new JConfig;
- }
+ if (class_exists($class))
+ {
+ $config = new $class;
+ }
+ else
+ {
+ throw new Exception('Configuration class does not exist.');
}
}
63 tests/suite/joomla/application/JCliTest.php
View
@@ -29,24 +29,6 @@ class JCliTest extends JoomlaTestCase
protected $inspector;
/**
- * Data for fetchConfigurationData method.
- *
- * @return array
- *
- * @since 11.3
- */
- public function getFetchConfigurationData()
- {
- return array(
- // fileName, expectsClass, (expected result array)
- 'Default configuration class' => array(null, true, array('foo' => 'bar')),
- 'Custom file with array' => array('config.jweb-array', false, array('foo' => 'bar')),
-// 'Custom file, invalid class' => array('config.JCli-wrongclass', false, array()),
- 'Custom file, snooping' => array('../test_application/config.jcli-snoopy', false, array()),
- );
- }
-
- /**
* Setup for testing.
*
* @return void
@@ -232,9 +214,26 @@ public function testExecute()
}
/**
+ * Data for fetchConfigurationData method.
+ *
+ * @return array
+ *
+ * @since 11.3
+ */
+ public function getFetchConfigurationData()
+ {
+ return array(
+ // file, class, expectsClass, (expected result array), whether there should be an exception
+ 'Default configuration class' => array(null, null, 'JConfig', 'ConfigEval'),
+ 'Custom file, invalid class' => array(JPATH_BASE . '/config.JCli-wrongclass.php', 'noclass', false, array(), true),
+ );
+ }
+
+ /**
* Tests the JCli::fetchConfigurationData method.
*
* @param string $fileName The name of the configuration file.
+ * @param string $fileName The name of the configuration file.
* @param boolean $expectsClass The result is expected to be a class.
* @param array $expects The expected result as an array.
*
@@ -243,14 +242,36 @@ public function testExecute()
* @dataProvider getFetchConfigurationData
* @since 11.3
*/
- public function testFetchConfigurationData($fileName, $expectsClass, $expects)
+ public function testFetchConfigurationData($file, $class, $expectsClass, $expects, $expectedException = false)
{
- $config = $this->inspector->fetchConfigurationData($fileName);
+ if ($expectedException)
+ {
+ $this->setExpectedException('Exception');
+ }
+
+ if (is_null($file) && is_null($class))
+ {
+ $config = $this->inspector->fetchConfigurationData();
+ }
+ elseif (is_null($class))
+ {
+ $config = $this->inspector->fetchConfigurationData($file);
+ }
+ else
+ {
+ $config = $this->inspector->fetchConfigurationData($file, $class);
+ }
+
+ if ($expects == 'ConfigEval')
+ {
+ $expects = new JConfig;
+ $expects = (array)$expects;
+ }
if ($expectsClass)
{
$this->assertInstanceOf(
- 'JConfig',
+ $expectsClass,
$config,
'Checks the configuration object is the appropriate class.'
);
65 tests/suite/joomla/application/JWebTest.php
View
@@ -69,24 +69,6 @@ public function getDetectRequestUriData()
*
* @since 11.3
*/
- public function getFetchConfigurationData()
- {
- return array(
- // fileName, expectsClass, (expected result array)
- 'Default configuration class' => array(null, true, array('foo' => 'bar')),
- 'Custom file with array' => array('config.jweb-array', false, array('foo' => 'bar')),
-// 'Custom file, invalid class' => array('config.jweb-wrongclass', false, array()),
- 'Custom file, snooping' => array('../test_application/config.jweb-snoopy', false, array()),
- );
- }
-
- /**
- * Data for fetchConfigurationData method.
- *
- * @return array
- *
- * @since 11.3
- */
public function getRedirectData()
{
return array(
@@ -742,8 +724,25 @@ public function testExecuteWithDocument()
}
/**
- * Tests the JWeb::fetchConfigurationData method.
+ * Data for fetchConfigurationData method.
+ *
+ * @return array
*
+ * @since 11.3
+ */
+ public function getFetchConfigurationData()
+ {
+ return array(
+ // file, class, expectsClass, (expected result array), whether there should be an exception
+ 'Default configuration class' => array(null, null, 'JConfig', 'ConfigEval'),
+ 'Custom file, invalid class' => array(JPATH_BASE . '/config.JCli-wrongclass.php', 'noclass', false, array(), true),
+ );
+ }
+
+ /**
+ * Tests the JCli::fetchConfigurationData method.
+ *
+ * @param string $fileName The name of the configuration file.
* @param string $fileName The name of the configuration file.
* @param boolean $expectsClass The result is expected to be a class.
* @param array $expects The expected result as an array.
@@ -753,14 +752,36 @@ public function testExecuteWithDocument()
* @dataProvider getFetchConfigurationData
* @since 11.3
*/
- public function testFetchConfigurationData($fileName, $expectsClass, $expects)
+ public function testFetchConfigurationData($file, $class, $expectsClass, $expects, $expectedException = false)
{
- $config = $this->inspector->fetchConfigurationData($fileName);
+ if ($expectedException)
+ {
+ $this->setExpectedException('Exception');
+ }
+
+ if (is_null($file) && is_null($class))
+ {
+ $config = $this->inspector->fetchConfigurationData();
+ }
+ elseif (is_null($class))
+ {
+ $config = $this->inspector->fetchConfigurationData($file);
+ }
+ else
+ {
+ $config = $this->inspector->fetchConfigurationData($file, $class);
+ }
+
+ if ($expects == 'ConfigEval')
+ {
+ $expects = new JConfig;
+ $expects = (array)$expects;
+ }
if ($expectsClass)
{
$this->assertInstanceOf(
- 'JConfig',
+ $expectsClass,
$config,
'Checks the configuration object is the appropriate class.'
);
4 tests/suite/joomla/application/stubs/JCliInspector.php
View
@@ -105,9 +105,9 @@ public function setClassProperty($name, $value)
*
* @since 11.3
*/
- public function fetchConfigurationData($fileName = null)
+ public function fetchConfigurationData($file = '', $class = 'JConfig')
{
- return parent::fetchConfigurationData($fileName);
+ return parent::fetchConfigurationData($file, $class);
}
/**
4 tests/suite/joomla/application/stubs/JWebInspector.php
View
@@ -181,9 +181,9 @@ public function doExecute()
*
* @since 11.3
*/
- public function fetchConfigurationData($fileName = null)
+ public function fetchConfigurationData($file = '', $class = 'JConfig')
{
- return parent::fetchConfigurationData($fileName);
+ return parent::fetchConfigurationData($file, $class);
}
/**
13 tests/tmp/config.jweb-array.php
View
@@ -1,13 +0,0 @@
-<?php
-/**
- * @package Joomla.UnitTest
- *
- * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- */
-
-defined('JPATH_BASE') or die;
-
-$config = array(
- 'foo' => 'bar',
-);
14 tests/tmp/config.jweb-snoopy.php
View
@@ -1,14 +0,0 @@
-<?php
-/**
- * @package Joomla.UnitTest
- *
- * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE
- */
-
-defined('JPATH_BASE') or die;
-
-class JConfig
-{
- public $foo = 'bar';
-}
Something went wrong with that request. Please try again.