Skip to content

Commit

Permalink
Fixes and Unittests for JRegistry package
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackwar committed Nov 12, 2011
1 parent 8e73e52 commit a99dc22
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 185 deletions.
8 changes: 4 additions & 4 deletions libraries/joomla/registry/format/php.php
Expand Up @@ -39,9 +39,9 @@ public function objectToString($object, $params = array())
{
$vars .= "\tpublic $" . $k . " = '" . addcslashes($v, '\\\'') . "';\n";
}
elseif (is_array($v))
elseif (is_array($v) || is_object($v))
{
$vars .= "\tpublic $" . $k . " = " . $this->getArrayString($v) . ";\n";
$vars .= "\tpublic $" . $k . " = " . $this->getArrayString((array)$v) . ";\n";
}
}

Expand Down Expand Up @@ -90,9 +90,9 @@ protected function getArrayString($a)
{
$s .= ($i) ? ', ' : '';
$s .= '"' . $k . '" => ';
if (is_array($v))
if (is_array($v) || is_object($v))
{
$s .= $this->getArrayString($v);
$s .= $this->getArrayString((array)$v);
}
else
{
Expand Down
18 changes: 1 addition & 17 deletions libraries/joomla/registry/format/xml.php
Expand Up @@ -40,23 +40,7 @@ public function objectToString($object, $options = array())
$root = simplexml_load_string('<' . $rootName . ' />');

// Iterate over the object members.
foreach ((array) $object as $k => $v)
{
if (is_scalar($v))
{
$n = $root->addChild($nodeName, $v);
$n->addAttribute('name', $k);
$n->addAttribute('type', gettype($v));
}
else
{
$n = $root->addChild($nodeName);
$n->addAttribute('name', $k);
$n->addAttribute('type', gettype($v));

$this->getXmlChildren($n, $v, $nodeName);
}
}
$this->getXmlChildren($root, $object, $nodeName);

return $root->asXML();
}
Expand Down
16 changes: 16 additions & 0 deletions libraries/joomla/registry/registry.php
Expand Up @@ -470,10 +470,12 @@ protected function asArray($data)
*/
public function loadXML($data, $namespace = null)
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::loadXML() is deprecated.', JLog::WARNING, 'deprecated');

return $this->loadString($data, 'XML');
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -491,10 +493,12 @@ public function loadXML($data, $namespace = null)
*/
public function loadINI($data, $namespace = null, $options = array())
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::loadINI() is deprecated.', JLog::WARNING, 'deprecated');

return $this->loadString($data, 'INI', $options);
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -510,10 +514,12 @@ public function loadINI($data, $namespace = null, $options = array())
*/
public function loadJSON($data)
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::loadJSON() is deprecated.', JLog::WARNING, 'deprecated');

return $this->loadString($data, 'JSON');
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -529,11 +535,13 @@ public function loadJSON($data)
*/
public function makeNameSpace($namespace)
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::makeNameSpace() is deprecated.', JLog::WARNING, 'deprecated');

//$this->_registry[$namespace] = array('data' => new stdClass());
return true;
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -547,11 +555,13 @@ public function makeNameSpace($namespace)
*/
public function getNameSpaces()
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::getNameSpaces() is deprecated.', JLog::WARNING, 'deprecated');

//return array_keys($this->_registry);
return array();
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -568,6 +578,7 @@ public function getNameSpaces()
*/
public function getValue($path, $default = null)
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::getValue() is deprecated.', JLog::WARNING, 'deprecated');

Expand All @@ -578,6 +589,7 @@ public function getValue($path, $default = null)
$path = implode('.', $parts);
}
return $this->get($path, $default);
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -594,6 +606,7 @@ public function getValue($path, $default = null)
*/
public function setValue($path, $value)
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::setValue() is deprecated.', JLog::WARNING, 'deprecated');

Expand All @@ -604,6 +617,7 @@ public function setValue($path, $value)
$path = implode('.', $parts);
}
return $this->set($path, $value);
// @codeCoverageIgnoreEnd
}

/**
Expand All @@ -619,9 +633,11 @@ public function setValue($path, $value)
*/
public function loadSetupFile()
{
// @codeCoverageIgnoreStart
// Deprecation warning.
JLog::add('JRegistry::loadXML() is deprecated.', JLog::WARNING, 'deprecated');

return true;
// @codeCoverageIgnoreEnd
}
}
37 changes: 12 additions & 25 deletions tests/suite/joomla/registry/JRegistryFormatTest.php
Expand Up @@ -15,15 +15,10 @@
*/
class JRegistryFormatTest extends PHPUnit_Framework_TestCase
{
/**
* @var JRegistryFormat
*/
protected $object;

/**
* Test the JRegistryFormat::getInstance method.
*/
public function testGetInstance01()
public function testGetInstance()
{
// Test INI format.
$object = JRegistryFormat::getInstance('INI');
Expand Down Expand Up @@ -52,24 +47,16 @@ public function testGetInstance01()
$object instanceof JRegistryFormatXml,
$this->isTrue()
);
}

/**
* Failing test of the JRegistryFormat::getInstance method.
*
* @return void
*
* @since 11.3
*
* @expectedException JException
*/
public function testGetInstance02()
{
// Test SQL format.
$object = JRegistryFormat::getInstance('SQL');
$this->assertThat(
$object instanceof JRegistryFormatSQL,
$this->isTrue()
);

// Test non-existing format.
try
{
$object = JRegistryFormat::getInstance('SQL');
}
catch(Exception $e)
{
return;
}
$this->fail('JRegistryFormat should throw an exception in case of non-existing formats');
}
}
66 changes: 9 additions & 57 deletions tests/suite/joomla/registry/JRegistryTest.php
Expand Up @@ -340,16 +340,12 @@ public function testLoadFile()
}

/**
* Test the JRegistry::loadIni method.
* Test the JRegistry::loadString() method.
*/
public function testLoadINI()
public function testLoadString()
{
//$string = "[section]\nfoo=\"testloadini\"";

$registry = new JRegistry;
$result = $registry->loadIni("foo=\"testloadini1\"");

// Result is always true, no error checking in method.
$result = $registry->loadString('foo="testloadini1"', 'INI');

// Test getting a known value.
$this->assertThat(
Expand All @@ -358,32 +354,26 @@ public function testLoadINI()
'Line: '.__LINE__.'.'
);

$result = $registry->loadIni("[section]\nfoo=\"testloadini2\"");
$result = $registry->loadString("[section]\nfoo=\"testloadini2\"", 'INI');
// Test getting a known value.
$this->assertThat(
$registry->get('foo'),
$this->equalTo('testloadini2'),
'Line: '.__LINE__.'.'
);

$result = $registry->loadIni("[section]\nfoo=\"testloadini3\"", null, true);
$result = $registry->loadString("[section]\nfoo=\"testloadini3\"", 'INI', true);
// Test getting a known value after processing sections.
$this->assertThat(
$registry->get('section.foo'),
$this->equalTo('testloadini3'),
'Line: '.__LINE__.'.'
);
}

/**
* Test the JRegistry::loadJson method.
*/
public function testLoadJSON()
{
$string = '{"foo":"testloadjson"}';

$string = '{"foo":"testloadjson"}';

$registry = new JRegistry;
$result = $registry->loadJson($string);
$result = $registry->loadString($string);

// Result is always true, no error checking in method.

Expand All @@ -393,6 +383,7 @@ public function testLoadJSON()
$this->equalTo('testloadjson'),
'Line: '.__LINE__.'.'
);

}

/**
Expand Down Expand Up @@ -424,45 +415,6 @@ public function testLoadObject()
$this->assertTrue($registry->loadObject($object), 'Line: '.__LINE__.'. Should load object successfully');
}

/**
* Test the JRegistry::loadXML method.
*/
public function testLoadXML()
{
// Cannot test since stringToObject is not implemented yet.
}

/**
* Test the JRegistry::makeNamespace method.
*/
public function testMakeNameSpace()
{
$a = new JRegistry;
$a->makeNameSpace('foo');

$this->assertThat(
//in_array('foo', $a->getNameSpaces()),
//$this->isTrue()
$a->getNameSpaces(),
$this->equalTo(array()),
'Line: '.__LINE__.'.'
);
}

/**
* Test the JRegistry::makeNamespace method.
*/
public function testLoadSetupFile()
{
$a = new JRegistry;

$this->assertThat(
$a->loadSetupFile(),
$this->equalTo(true),
'loadSetupFile does not exist or did not return true.'
);
}

/**
* Test the JRegistry::merge method.
*/
Expand Down

0 comments on commit a99dc22

Please sign in to comment.