Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes and Unittests for JRegistry package

  • Loading branch information...
commit a99dc227f49c3d1d0d75eb9b860d3182c2c66056 1 parent 8e73e52
@Hackwar authored
View
8 libraries/joomla/registry/format/php.php
@@ -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";
}
}
@@ -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
{
View
18 libraries/joomla/registry/format/xml.php
@@ -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();
}
View
16 libraries/joomla/registry/registry.php
@@ -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
}
/**
@@ -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
}
/**
@@ -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
}
/**
@@ -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
}
/**
@@ -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
}
/**
@@ -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');
@@ -578,6 +589,7 @@ public function getValue($path, $default = null)
$path = implode('.', $parts);
}
return $this->get($path, $default);
+ // @codeCoverageIgnoreEnd
}
/**
@@ -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');
@@ -604,6 +617,7 @@ public function setValue($path, $value)
$path = implode('.', $parts);
}
return $this->set($path, $value);
+ // @codeCoverageIgnoreEnd
}
/**
@@ -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
}
}
View
37 tests/suite/joomla/registry/JRegistryFormatTest.php
@@ -16,14 +16,9 @@
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');
@@ -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');
}
}
View
66 tests/suite/joomla/registry/JRegistryTest.php
@@ -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(
@@ -358,7 +354,7 @@ 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'),
@@ -366,24 +362,18 @@ public function testLoadINI()
'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.
@@ -393,6 +383,7 @@ public function testLoadJSON()
$this->equalTo('testloadjson'),
'Line: '.__LINE__.'.'
);
+
}
/**
@@ -425,45 +416,6 @@ public function testLoadObject()
}
/**
- * 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.
*/
public function testMerge()
View
46 tests/suite/joomla/registry/format/JRegistryFormatIniTest.php
@@ -8,7 +8,6 @@
*/
require_once JPATH_PLATFORM.'/joomla/registry/format.php';
-require_once JPATH_PLATFORM.'/joomla/registry/format/ini.php';
/**
* Test class for JRegistryFormatINI.
@@ -21,16 +20,22 @@ class JRegistryFormatINITest extends PHPUnit_Framework_TestCase
*/
public function testObjectToString()
{
- $class = new JRegistryFormatINI;
+ $class = JRegistryFormat::getInstance('INI');
$options = null;
$object = new stdClass;
$object->foo = 'bar';
+ $object->booleantrue = true;
+ $object->booleanfalse = false;
+ $object->numericint = 42;
+ $object->numericfloat = 3.1415;
+ $object->section = new stdClass();
+ $object->section->key = 'value';
// Test basic object to string.
$string = $class->objectToString($object, $options);
$this->assertThat(
trim($string),
- $this->equalTo('foo="bar"')
+ $this->equalTo("foo=\"bar\"\nbooleantrue=true\nbooleanfalse=false\nnumericint=42\nnumericfloat=3.1415\n\n[section]\nkey=\"value\"")
);
}
@@ -39,7 +44,7 @@ public function testObjectToString()
*/
public function testStringToObject()
{
- $class = new JRegistryFormatINI;
+ $class = JRegistryFormat::getInstance('INI');
$string2 = "[section]\nfoo=bar";
@@ -63,8 +68,37 @@ public function testStringToObject()
$this->equalTo($object2)
);
- $this->markTestIncomplete(
- 'Need to test for bad input.'
+ //Test empty string
+ $this->assertThat(
+ $class->stringToObject(null),
+ $this->equalTo(new stdClass())
+ );
+
+ $string3 = "[section]\nfoo=bar\n;Testcomment\nkey=value\n\n/brokenkey=)brokenvalue";
+ $object2->section->key = 'value';
+
+ $this->assertThat(
+ $class->stringToObject($string3, true),
+ $this->equalTo($object2)
+ );
+
+ $string4 = "boolfalse=false\nbooltrue=true\nkeywithoutvalue\nnumericfloat=3.1415\nnumericint=42\nkey=\"value\"";
+ $object3 = new stdClass();
+ $object3->boolfalse = false;
+ $object3->booltrue = true;
+ $object3->numericfloat = 3.1415;
+ $object3->numericint = 42;
+ $object3->key = 'value';
+
+ $this->assertThat(
+ $class->stringToObject($string4),
+ $this->equalTo($object3)
+ );
+
+ //Trigger the cache - Doing this only to achieve 100% code coverage. ;-)
+ $this->assertThat(
+ $class->stringToObject($string4),
+ $this->equalTo($object3)
);
}
}
View
42 tests/suite/joomla/registry/format/JRegistryFormatJsonTest.php
@@ -8,7 +8,6 @@
*/
require_once JPATH_PLATFORM.'/joomla/registry/format.php';
-require_once JPATH_PLATFORM.'/joomla/registry/format/json.php';
/**
* Test class for JRegistryFormatJSON.
@@ -17,25 +16,34 @@
class JRegistryFormatJSONTest extends PHPUnit_Framework_TestCase
{
/**
- * @var JRegistryFormatJSON
- */
- protected $object;
-
- /**
* Test the JRegistryFormatJSON::objectToString method.
*/
public function testObjectToString()
{
- $class = new JRegistryFormatJSON;
+ $class = JRegistryFormat::getInstance('JSON');
$options = null;
$object = new stdClass;
$object->foo = 'bar';
-
+ $object->quoted = '"stringwithquotes"';
+ $object->booleantrue = true;
+ $object->booleanfalse = false;
+ $object->numericint = 42;
+ $object->numericfloat = 3.1415;
+ $object->section = new stdClass(); //The PHP registry format does not support nested objects
+ $object->section->key = 'value';
+ $object->array = array('nestedarray' => array('test1' => 'value1'));
+
+ $string = '{"foo":"bar","quoted":"\"stringwithquotes\"",'.
+ '"booleantrue":true,"booleanfalse":false,'.
+ '"numericint":42,"numericfloat":3.1415,'.
+ '"section":{"key":"value"},'.
+ '"array":{"nestedarray":{"test1":"value1"}}'.
+ '}';
+
// Test basic object to string.
- $string = $class->objectToString($object, $options);
$this->assertThat(
- $string,
- $this->equalTo('{"foo":"bar"}')
+ $class->objectToString($object, $options),
+ $this->equalTo($string)
);
}
@@ -88,9 +96,15 @@ public function testStringToObject()
$this->equalTo($object2),
'Line:'.__LINE__.' The INI string should covert into an object with sections.'
);
-
- $this->markTestIncomplete(
- 'Need to test for bad input.'
+
+ /**
+ * Test for bad input
+ * Everything that is not starting with { is handled by
+ * JRegistryFormatIni, which we test seperately
+ */
+ $this->assertThat(
+ $class->stringToObject('{key:\'value\''),
+ $this->equalTo(false)
);
}
}
View
88 tests/suite/joomla/registry/format/JRegistryFormatPhpTest.php
@@ -8,7 +8,6 @@
*/
require_once JPATH_PLATFORM.'/joomla/registry/format.php';
-require_once JPATH_PLATFORM.'/joomla/registry/format/php.php';
/**
* Test class for JRegistryFormatPHP.
@@ -17,63 +16,38 @@
class JRegistryFormatPHPTest extends PHPUnit_Framework_TestCase
{
/**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- function setUp()
- {
- $this->instance = new JRegistryFormatPHP;
- }
-
- /**
- * Convert an array into an object.
- *
- * @param array
- * @return object
- */
- private static function _objectFactory($properties)
- {
- $obj = new stdClass();
- foreach ($properties as $k => $v) {
- $obj->{$k} = $v;
- }
- return $obj;
- }
-
- /**
- * Get the objects to run tests on.
- */
- public function getObjects()
- {
- $tests = array(
- 'Regular Object' => array(
- self::_objectFactory(array('test1' => 'value1', 'test2' => 'value2')),
- array('class' => 'myClass'),
- '<?php'."\n".'class myClass {'."\n\t".'public $test1 = \'value1\';'."\n\t".'public $test2 = \'value2\';'."\n}\n".'?>'
- ),
- 'Object with Double Quote' => array(
- self::_objectFactory(array('test1' => 'value1"', 'test2' => 'value2')),
- array('class' => 'myClass'),
- '<?php'."\n".'class myClass {'."\n\t".'public $test1 = \'value1"\';'."\n\t".'public $test2 = \'value2\';'."\n}\n".'?>'
- )
-
- );
-
- return $tests;
- }
-
- /**
* Test the JRegistryFormatPHP::objectToString method.
- *
- * @dataProvider getObjects
- *
- * @param string The type of input
- * @param string The input
- * @param string The expected result for this test.
*/
- function testObjectToString($object, $params, $expect)
+ function testObjectToString()
{
- $this->assertEquals($expect, $this->instance->objectToString($object, $params));
+ $class = JRegistryFormat::getInstance('PHP');
+ $options = array('class' => 'myClass');
+ $object = new stdClass;
+ $object->foo = 'bar';
+ $object->quoted = '"stringwithquotes"';
+ $object->booleantrue = true;
+ $object->booleanfalse = false;
+ $object->numericint = 42;
+ $object->numericfloat = 3.1415;
+ $object->section = new stdClass(); //The PHP registry format does not support nested objects
+ $object->section->key = 'value';
+ $object->array = array('nestedarray' => array('test1' => 'value1'));
+
+ $string = "<?php\n".
+ "class myClass {\n".
+ "\tpublic \$foo = 'bar';\n".
+ "\tpublic \$quoted = '\"stringwithquotes\"';\n".
+ "\tpublic \$booleantrue = '1';\n".
+ "\tpublic \$booleanfalse = '';\n".
+ "\tpublic \$numericint = '42';\n".
+ "\tpublic \$numericfloat = '3.1415';\n".
+ "\tpublic \$section = array(\"key\" => \"value\");\n".
+ "\tpublic \$array = array(\"nestedarray\" => array(\"test1\" => \"value1\"));\n".
+ "}\n?>";
+ $this->assertThat(
+ $class->objectToString($object, $options),
+ $this->equalTo($string)
+ );
}
/**
@@ -81,6 +55,8 @@ function testObjectToString($object, $params, $expect)
*/
public function testStringToObject()
{
- // This method is not implemented in the class.
+ $class = JRegistryFormat::getInstance('PHP');
+ // This method is not implemented in the class. The test is to achieve 100% code coverage
+ $this->assertTrue($class->stringToObject(''));
}
}
View
64 tests/suite/joomla/registry/format/JRegistryFormatXmlTest.php
@@ -8,7 +8,6 @@
*/
require_once JPATH_PLATFORM.'/joomla/registry/format.php';
-require_once JPATH_PLATFORM.'/joomla/registry/format/xml.php';
/**
* Test class for JRegistryFormatXML.
@@ -21,16 +20,40 @@ class JRegistryFormatXMLTest extends PHPUnit_Framework_TestCase
*/
public function testObjectToString()
{
- $class = new JRegistryFormatXML;
+ $class = JRegistryFormat::getInstance('XML');
$options = null;
$object = new stdClass;
$object->foo = 'bar';
+ $object->quoted = '"stringwithquotes"';
+ $object->booleantrue = true;
+ $object->booleanfalse = false;
+ $object->numericint = 42;
+ $object->numericfloat = 3.1415;
+ $object->section = new stdClass();
+ $object->section->key = 'value';
+ $object->array = array('nestedarray' => array('test1' => 'value1'));
+ $string = "<?xml version=\"1.0\"?>\n<registry>".
+ "<node name=\"foo\" type=\"string\">bar</node>".
+ "<node name=\"quoted\" type=\"string\">\"stringwithquotes\"</node>".
+ "<node name=\"booleantrue\" type=\"boolean\">1</node>".
+ "<node name=\"booleanfalse\" type=\"boolean\"></node>".
+ "<node name=\"numericint\" type=\"integer\">42</node>".
+ "<node name=\"numericfloat\" type=\"double\">3.1415</node>".
+ "<node name=\"section\" type=\"object\">".
+ "<node name=\"key\" type=\"string\">value</node>".
+ "</node>".
+ "<node name=\"array\" type=\"array\">".
+ "<node name=\"nestedarray\" type=\"array\">".
+ "<node name=\"test1\" type=\"string\">value1</node>".
+ "</node>".
+ "</node>".
+ "</registry>\n";
+
// Test basic object to string.
- $string = trim($class->objectToString($object, $options));
$this->assertThat(
- $string,
- $this->equalTo("<?xml version=\"1.0\"?>\n<registry><node name=\"foo\" type=\"string\">bar</node></registry>")
+ $class->objectToString($object, $options),
+ $this->equalTo($string)
);
}
@@ -39,7 +62,36 @@ public function testObjectToString()
*/
public function testStringToObject()
{
- // This method is not implemented in the class.
+ $class = JRegistryFormat::getInstance('XML');
+ $object = new stdClass;
+ $object->foo = 'bar';
+ $object->booleantrue = true;
+ $object->booleanfalse = false;
+ $object->numericint = 42;
+ $object->numericfloat = 3.1415;
+ $object->section = new stdClass();
+ $object->section->key = 'value';
+ $object->array = array('test1' => 'value1');
+
+ $string = "<?xml version=\"1.0\"?>\n<registry>".
+ "<node name=\"foo\" type=\"string\">bar</node>".
+ "<node name=\"booleantrue\" type=\"boolean\">1</node>".
+ "<node name=\"booleanfalse\" type=\"boolean\"></node>".
+ "<node name=\"numericint\" type=\"integer\">42</node>".
+ "<node name=\"numericfloat\" type=\"double\">3.1415</node>".
+ "<node name=\"section\" type=\"object\">".
+ "<node name=\"key\" type=\"string\">value</node>".
+ "</node>".
+ "<node name=\"array\" type=\"array\">".
+ "<node name=\"test1\" type=\"string\">value1</node>".
+ "</node>".
+ "</registry>\n";
+
+ // Test basic object to string.
+ $this->assertThat(
+ $class->stringToObject($string),
+ $this->equalTo($object)
+ );
}
}

0 comments on commit a99dc22

Please sign in to comment.
Something went wrong with that request. Please try again.