Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Merge pull request #440 from ianmacl/fetchconfigdata
Browse files Browse the repository at this point in the history
Changes to fetchConfigData to increase flexibility and avoid testing conflicts with multiple JConfig classes.
  • Loading branch information
LouisLandry committed Oct 19, 2011
2 parents 9cffa3c + 7b5c2ff commit ab1e703
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 111 deletions.
2 changes: 1 addition & 1 deletion libraries/joomla/application/application.php
Expand Up @@ -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;
Expand Down
40 changes: 22 additions & 18 deletions libraries/joomla/application/cli.php
Expand Up @@ -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.');
}
}

Expand Down
40 changes: 22 additions & 18 deletions libraries/joomla/application/web.php
Expand Up @@ -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.');
}
}

Expand Down
63 changes: 42 additions & 21 deletions tests/suite/joomla/application/JCliTest.php
Expand Up @@ -28,24 +28,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.
*
Expand Down Expand Up @@ -231,10 +213,27 @@ 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.
*
Expand All @@ -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.'
);
Expand Down
65 changes: 43 additions & 22 deletions tests/suite/joomla/application/JWebTest.php
Expand Up @@ -62,24 +62,6 @@ public function getDetectRequestUriData()
);
}

/**
* 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.jweb-wrongclass', false, array()),
'Custom file, snooping' => array('../test_application/config.jweb-snoopy', false, array()),
);
}

/**
* Data for fetchConfigurationData method.
*
Expand Down Expand Up @@ -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.
Expand All @@ -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.'
);
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/joomla/application/stubs/JCliInspector.php
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/joomla/application/stubs/JWebInspector.php
Expand Up @@ -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);
}

/**
Expand Down
13 changes: 0 additions & 13 deletions tests/tmp/config.jweb-array.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/tmp/config.jweb-snoopy.php

This file was deleted.

0 comments on commit ab1e703

Please sign in to comment.