예제 #1
0
 /**
  * Test the Json::stringToObject method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testStringToObject()
 {
     $class = AbstractRegistryFormat::getInstance('JSON');
     $string1 = '{"title":"Joomla Framework","author":"Me","params":{"show_title":1,"show_abstract":0,"show_author":1,"categories":[1,2]}}';
     $string2 = "[section]\nfoo=bar";
     $object1 = new \stdClass();
     $object1->title = 'Joomla Framework';
     $object1->author = 'Me';
     $object1->params = new \stdClass();
     $object1->params->show_title = 1;
     $object1->params->show_abstract = 0;
     $object1->params->show_author = 1;
     $object1->params->categories = array(1, 2);
     $object2 = new \stdClass();
     $object2->section = new \stdClass();
     $object2->section->foo = 'bar';
     $object3 = new \stdClass();
     $object3->foo = 'bar';
     // Test basic JSON string to object.
     $object = $class->stringToObject($string1, array('processSections' => false));
     $this->assertThat($object, $this->equalTo($object1), 'Line:' . __LINE__ . ' The complex JSON string should convert into the appropriate object.');
     // Test INI format string without sections.
     $object = $class->stringToObject($string2, array('processSections' => false));
     $this->assertThat($object, $this->equalTo($object3), 'Line:' . __LINE__ . ' The INI string should convert into an object without sections.');
     // Test INI format string with sections.
     $object = $class->stringToObject($string2, array('processSections' => true));
     $this->assertThat($object, $this->equalTo($object2), 'Line:' . __LINE__ . ' The INI string should covert into an object with sections.');
     /**
      * Test for bad input
      * Everything that is not starting with { is handled by
      * Format\Ini, which we test seperately
      */
     $this->assertThat($class->stringToObject('{key:\'value\''), $this->equalTo(false));
 }
예제 #2
0
 /**
  * Test the Ini::stringToObject method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testStringToObject()
 {
     $class = AbstractRegistryFormat::getInstance('INI');
     $string2 = "[section]\nfoo=bar";
     $object1 = new stdClass();
     $object1->foo = 'bar';
     $object2 = new stdClass();
     $object2->section = $object1;
     // Test INI format string without sections.
     $object = $class->stringToObject($string2, array('processSections' => false));
     $this->assertThat($object, $this->equalTo($object1));
     // Test INI format string with sections.
     $object = $class->stringToObject($string2, array('processSections' => true));
     $this->assertThat($object, $this->equalTo($object2));
     // 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, array('processSections' => 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));
 }
예제 #3
0
파일: Json.php 프로젝트: adjaika/J3Base
 /**
  * Parse a JSON formatted string and convert it into an object.
  *
  * If the string is not in JSON format, this method will attempt to parse it as INI format.
  *
  * @param   string  $data     JSON formatted string to convert.
  * @param   array   $options  Options used by the formatter.
  *
  * @return  object   Data object.
  *
  * @since   1.0
  */
 public function stringToObject($data, array $options = array('processSections' => false))
 {
     $data = trim($data);
     if (substr($data, 0, 1) != '{' && substr($data, -1, 1) != '}') {
         return AbstractRegistryFormat::getInstance('Ini')->stringToObject($data, $options);
     }
     return json_decode($data);
 }
예제 #4
0
 /**
  * Test input and output data equality.
  *
  * @return  void
  *
  * @since   1.3.0
  */
 public function testDataEquality()
 {
     $class = AbstractRegistryFormat::getInstance('INI');
     $input = "[section1]\nboolfalse=false\nbooltrue=true\nnumericfloat=3.1415\nnumericint=42\nkey=\"value\"\n" . "arrayitem[]=\"item1\"\narrayitem[]=\"item2\"\n\n" . "[section2]\nboolfalse=false\nbooltrue=true\nnumericfloat=3.1415\nnumericint=42\nkey=\"value\"";
     $object = $class->stringToObject($input, array('processSections' => true, 'supportArrayValues' => true));
     $output = $class->objectToString($object, array('processSections' => true, 'supportArrayValues' => true));
     $this->assertEquals($input, $output, 'Line:' . __LINE__ . ' Input and output data must be equal.');
 }
예제 #5
0
 /**
  * Test input and output data equality.
  *
  * @return  void
  *
  * @since   1.3.0
  */
 public function testDataEquality()
 {
     $class = AbstractRegistryFormat::getInstance('XML');
     // Check for different PHP behavior of displaying boolean false in XML.
     $checkFalse = '<check/>' == simplexml_load_string('<test/>')->addChild('check', false)->asXML() ? '/>' : '></node>';
     $input = "<?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\"" . $checkFalse . "<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";
     $object = $class->stringToObject($input);
     $output = $class->objectToString($object);
     $this->assertEquals($input, $output, 'Line:' . __LINE__ . ' Input and output data must be equal.');
 }
 /**
  * Parse a JSON formatted string and convert it into an object.
  *
  * If the string is not in JSON format, this method will attempt to parse it as INI format.
  *
  * @param   string  $data     JSON formatted string to convert.
  * @param   array   $options  Options used by the formatter.
  *
  * @return  object   Data object.
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public function stringToObject($data, array $options = array('processSections' => false))
 {
     $data = trim($data);
     if (substr($data, 0, 1) != '{' && substr($data, -1, 1) != '}') {
         return AbstractRegistryFormat::getInstance('Ini')->stringToObject($data, $options);
     }
     $decoded = json_decode($data);
     // Check for an error decoding the data
     if ($decoded === null) {
         throw new \RuntimeException(sprintf('Error decoding JSON data: %s', json_last_error_msg()));
     }
     return $decoded;
 }
예제 #7
0
 /**
  * Test the Xml::stringToObject method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testStringToObject()
 {
     $class = AbstractRegistryFormat::getInstance('XML');
     $object = new stdClass();
     $object->foo = 'bar';
     $object->booleantrue = true;
     $object->booleanfalse1 = false;
     $object->booleanfalse2 = 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=\"booleanfalse1\" type=\"boolean\"></node>" . "<node name=\"booleanfalse2\" type=\"boolean\"/>" . "<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));
 }
예제 #8
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function setUp()
 {
     $this->fixture = AbstractRegistryFormat::getInstance('Yaml');
 }
예제 #9
0
 /**
  * Get a namespace in a given string format
  *
  * @param   string  $format   Format to return the string in
  * @param   mixed   $options  Parameters used by the formatter, see formatters for more info
  *
  * @return  string   Namespace in string format
  *
  * @since   1.0
  */
 public function toString($format = 'JSON', $options = array())
 {
     // Return a namespace in a given format
     $handler = AbstractRegistryFormat::getInstance($format, $options);
     return $handler->objectToString($this->data, $options);
 }
예제 #10
0
 /**
  * Test getInstance with a non-existent format.
  *
  * @return  void
  *
  * @expectedException  \InvalidArgumentException
  * @since              1.0
  */
 public function testGetInstanceNonExistent()
 {
     AbstractRegistryFormat::getInstance('SQL');
 }
예제 #11
0
 /**
  * Test input and output data equality.
  *
  * @return  void
  *
  * @since   1.3.0
  */
 public function testDataEquality()
 {
     $this->MarkTestIncomplete('Method is not implemented in the class');
     $class = AbstractRegistryFormat::getInstance('PHP');
     $input = "<?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?>";
     $object = $class->stringToObject($input);
     $output = $class->objectToString($object);
     $this->assertEquals($input, $output, 'Line:' . __LINE__ . ' Input and output data must be equal.');
 }
예제 #12
0
 /**
  * Test the Php::stringToObject method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testStringToObject()
 {
     $class = AbstractRegistryFormat::getInstance('PHP');
     // This method is not implemented in the class. The test is to achieve 100% code coverage
     $this->assertTrue($class->stringToObject(''));
 }
예제 #13
0
 /**
  * Test input and output data equality.
  *
  * @return  void
  *
  * @since   1.3.0
  */
 public function testDataEquality()
 {
     $class = AbstractRegistryFormat::getInstance('JSON');
     $input = '{"title":"Joomla Framework","author":"Me","params":{"show_title":1,"show_abstract":0,"show_author":1,"categories":[1,2]}}';
     $object = $class->stringToObject($input);
     $output = $class->objectToString($object);
     $this->assertEquals($input, $output, 'Line:' . __LINE__ . ' Input and output data must be equal.');
 }