Add OutputFilter::_EncodeXML and create a test
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 17:28:28 +0000 (13:28 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 17:28:28 +0000 (13:28 -0400)
http/output_filter.php
testing/tests/http/output_filter_test.php [new file with mode: 0644]

index 2eeb0e53087fbdfcbe99e7388b587f0657d04f87..f634b2bba12d7b91353d0339d2d63c189ba448fe 100644 (file)
@@ -81,4 +81,26 @@ class OutputFilter
   protected function _CreateBodyForResponse(Request $request,
                                             Response $response)
   {}
+
+  /*!
+    Creates an XML tree from an array. Equivalent to json_encode.
+  */
+  protected function _EncodeXML($data)
+  {
+    $response = new \SimpleXMLElement('<response/>');
+
+    $writer = function($elm, $parent) use (&$writer) {
+      foreach ($elm as $key => $value) {
+        if (is_scalar($value)) {
+          $parent->AddChild($key, $value);
+        } else {
+          $new_parent = $parent->AddChild($key);
+          $writer($value, $new_parent);
+        }
+      }
+    };
+
+    $writer($data, $response);
+    return $response->AsXML();
+  }
 }
diff --git a/testing/tests/http/output_filter_test.php b/testing/tests/http/output_filter_test.php
new file mode 100644 (file)
index 0000000..b101e24
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+// Hoplite
+// Copyright (c) 2011 Blue Static
+// 
+// This program is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation, either version 3 of the License, or any later version.
+// 
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program.  If not, see <http://www.gnu.org/licenses/>.
+
+namespace hoplite\test;
+use hoplite\http as http;
+
+require_once HOPLITE_ROOT . '/http/output_filter.php';
+
+class TestOutputFilter extends http\OutputFilter
+{
+  public function T_EncodeXML($d)
+  {
+    return $this->_EncodeXML($d);
+  }
+}
+
+class OutputFilterTest extends \PHPUnit_Framework_TestCase
+{
+  public function setUp()
+  {
+    $this->fixture = new TestOutputFilter(new http\RootController(array()));
+  }
+
+  public function testEncodeXML()
+  {
+    $array = array(
+      'test' => 1,
+      'foo' => 'bar',
+      'bar' => '<strong>baz</strong>',
+      'baz' => array(
+        'poo' => 'moo',
+        'moo' => 'baa'
+      )
+    );
+    $expected = <<<XML
+<?xml version="1.0"?>
+<response><test>1</test><foo>bar</foo><bar>&lt;strong&gt;baz&lt;/strong&gt;</bar><baz><poo>moo</poo><moo>baa</moo></baz></response>
+
+XML;
+    
+    $this->assertEquals($expected, $this->fixture->T_EncodeXML($array));
+
+    $obj = new \stdClass();
+    $obj->int = 2;
+    $obj->obj = new \stdClass();
+    $obj->obj->string = 'Foo';
+    $expected = <<<XML
+<?xml version="1.0"?>
+<response><int>2</int><obj><string>Foo</string></obj></response>
+
+XML;
+    $this->assertEquals($expected, $this->fixture->T_EncodeXML($obj));
+  }
+}