Author: chabotc
Date: Wed Nov 12 16:37:42 2008
New Revision: 713581

URL: http://svn.apache.org/viewvc?rev=713581&view=rev
Log:
Un-duplicate input xml/atom converter code by Pan Jie + fix & add input 
converter tests

Added:
    incubator/shindig/trunk/php/test/social/InputXmlConverterTest.php
Modified:
    incubator/shindig/trunk/php/src/social/converters/InputAtomConverter.php
    incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php

Modified: 
incubator/shindig/trunk/php/src/social/converters/InputAtomConverter.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/converters/InputAtomConverter.php?rev=713581&r1=713580&r2=713581&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/converters/InputAtomConverter.php 
(original)
+++ incubator/shindig/trunk/php/src/social/converters/InputAtomConverter.php 
Wed Nov 12 16:37:42 2008
@@ -29,39 +29,13 @@
 
        public function convertActivities($requestParam)
        {
-               $activity = array();
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA);
-               if (! isset($xml->title)) {
-                       throw new Exception("Mallformed activity xml");
-               }
-               // remember to either type cast to (string) or trim() the 
string so we don't get 
-               // SimpleXMLString types in the internal data representation. I 
often prefer
-               // using trim() since it cleans up the data too
-               $activity['id'] = isset($xml->id) ? trim($xml->id) : '';
-               $activity['title'] = trim($xml->title);
-               $activity['body'] = isset($xml->summary) ? trim($xml->summary) 
: '';
-               $activity['streamTitle'] = 
isset($xml->content->activity->streamTitle) ? 
trim($xml->content->activity->streamTitle) : '';
-               $activity['streamId'] = 
isset($xml->content->activity->streamId) ? 
trim($xml->content->activity->streamId) : '';
-               $activity['updated'] = isset($xml->updated) ? 
trim($xml->updated) : '';
-               if (isset($xml->content->activity->mediaItems)) {
-                       $activity['mediaItems'] = array();
-                       foreach ($xml->content->activity->mediaItems->MediaItem 
as $mediaItem) {
-                               $item = array();
-                               if (! isset($mediaItem->type) || ! 
isset($mediaItem->mimeType) || ! isset($mediaItem->url)) {
-                                       throw new Exception("Invalid media item 
in activity xml");
-                               }
-                               $item['type'] = trim($mediaItem->type);
-                               $item['mimeType'] = trim($mediaItem->mimeType);
-                               $item['url'] = trim($mediaItem->url);
-                               $activity['mediaItems'][] = $item;
-                       }
-               }
-               return $activity;
+               $xml = InputBasicXmlConverter::loadString($requestParam);
+               return InputBasicXmlConverter::convertActivities($xml, 
$xml->content->activity);
        }
 
        public function convertAppData($requestParam)
        {
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA);
+               $xml = InputBasicXmlConverter::loadString($requestParam);
                if (! isset($xml->content) || ! isset($xml->content->appdata)) {
                        throw new Exception("Mallformed AppData xml");
                }
@@ -74,23 +48,7 @@
 
        public function convertMessages($requestParam)
        {
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA);
-               $message = array();
-               if (! isset($xml->title) || ! isset($xml->content)) {
-                       throw new Exception("Invalid message structure");
-               }
-               $message['id'] = isset($xml->id) ? trim($xml->id) : null;
-               $message['title'] = trim($xml->title);
-               $message['body'] = trim($xml->content);
-               // retrieve recipients by looking at the osapi name space
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA, "http://opensocial.org/2008/opensocialapi";);
-               if (! isset($xml->recipient)) {
-                       throw new Exception("Invalid message structure");
-               }
-               $message['recipients'] = array();
-               foreach ($xml->recipient as $recipient) {
-                       $message['recipients'][] = trim($recipient);
-               }
-               return $message;
+               $xml = InputBasicXmlConverter::loadString($requestParam);
+               return InputBasicXmlConverter::convertMessages($requestParam, 
$xml, $xml->content);
        }
 }

Modified: 
incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php?rev=713581&r1=713580&r2=713581&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php 
(original)
+++ incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php Wed 
Nov 12 16:37:42 2008
@@ -29,39 +29,13 @@
 
        public function convertActivities($requestParam)
        {
-               $activity = array();
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA);
-               if (! isset($xml->title)) {
-                       throw new Exception("Mallformed activity xml");
-               }
-               // remember to either type cast to (string) or trim() the 
string so we don't get 
-               // SimpleXMLString types in the internal data representation. I 
often prefer
-               // using trim() since it cleans up the data too
-               $activity['id'] = isset($xml->id) ? trim($xml->id) : '';
-               $activity['title'] = trim($xml->title);
-               $activity['body'] = isset($xml->summary) ? trim($xml->summary) 
: '';
-               $activity['streamTitle'] = isset($xml->activity->streamTitle) ? 
trim($xml->activity->streamTitle) : '';
-               $activity['streamId'] = isset($xml->activity->streamId) ? 
trim($xml->activity->streamId) : '';
-               $activity['updated'] = isset($xml->updated) ? 
trim($xml->updated) : '';
-               if (isset($xml->activity->mediaItems)) {
-                       $activity['mediaItems'] = array();
-                       foreach ($xml->activity->mediaItems->MediaItem as 
$mediaItem) {
-                               $item = array();
-                               if (! isset($mediaItem->type) || ! 
isset($mediaItem->mimeType) || ! isset($mediaItem->url)) {
-                                       throw new Exception("Invalid media item 
in activity xml");
-                               }
-                               $item['type'] = trim($mediaItem->type);
-                               $item['mimeType'] = trim($mediaItem->mimeType);
-                               $item['url'] = trim($mediaItem->url);
-                               $activity['mediaItems'][] = $item;
-                       }
-               }
-               return $activity;
+               $xml = InputBasicXmlConverter::loadString($requestParam);
+               return InputBasicXmlConverter::convertActivities($xml, 
$xml->activity);
        }
 
        public function convertAppData($requestParam)
        {
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA);
+               $xml = InputBasicXmlConverter::loadString($requestParam);
                if (! isset($xml->entry)) {
                        throw new Exception("Mallformed AppData xml");
                }
@@ -76,23 +50,7 @@
 
        public function convertMessages($requestParam)
        {
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA);
-               $message = array();
-               if (! isset($xml->title) || ! isset($xml->body)) {
-                       throw new Exception("Invalid message structure");
-               }
-               $message['id'] = isset($xml->id) ? trim($xml->id) : null;
-               $message['title'] = trim($xml->title);
-               $message['body'] = trim($xml->body);
-               // retrieve recipients by looking at the osapi name space
-               $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA, "http://opensocial.org/2008/opensocialapi";);
-               if (! isset($xml->recipient)) {
-                       throw new Exception("Invalid message structure");
-               }
-               $message['recipients'] = array();
-               foreach ($xml->recipient as $recipient) {
-                       $message['recipients'][] = trim($recipient);
-               }
-               return $message;
+               $xml = InputBasicXmlConverter::loadString($requestParam);
+               return InputBasicXmlConverter::convertMessages($requestParam, 
$xml, $xml->body);
        }
 }

Added: incubator/shindig/trunk/php/test/social/InputXmlConverterTest.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/social/InputXmlConverterTest.php?rev=713581&view=auto
==============================================================================
--- incubator/shindig/trunk/php/test/social/InputXmlConverterTest.php (added)
+++ incubator/shindig/trunk/php/test/social/InputXmlConverterTest.php Wed Nov 
12 16:37:42 2008
@@ -0,0 +1,137 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+/**
+ * InputXmlConverter test case.
+ */
+class InputXmlConverterTest extends PHPUnit_Framework_TestCase {
+       
+       /**
+        * @var InputXmlConverter
+        */
+       private $InputXmlConverter;
+
+       /**
+        * Prepares the environment before running a test.
+        */
+       protected function setUp()
+       {
+               parent::setUp();
+               $this->InputXmlConverter = new InputXmlConverter(/* parameters 
*/);
+       }
+
+       /**
+        * Cleans up the environment after running a test.
+        */
+       protected function tearDown()
+       {
+               $this->InputXmlConverter = null;
+               parent::tearDown();
+       }
+
+       /**
+        * Tests InputXmlConverter->convertActivities()
+        */
+       public function testConvertActivities()
+       {
+               $xml = '<?xml version="1.0" encoding="UTF-8"?>
+<response>
+  <activity xmlns="http://ns.opensocial.org/2008/opensocial";>
+      <mediaItems>
+        <MediaItem>
+          <mimeType>IMAGE</mimeType>
+          <type>image</type>
+          <url>http://cdn.davesdaily.com/pictures/784-awesome-hands.jpg</url>
+          <types>
+            <AUDIO>audio</AUDIO>
+            <VIDEO>video</VIDEO>
+            <IMAGE>image</IMAGE>
+          </types>
+        </MediaItem>
+      </mediaItems>
+    <streamTitle>activities</streamTitle>
+    <streamId>1</streamId>
+    <userId>1</userId>
+  </activity>
+  <category term="status"/>
+  <updated>2008-08-05T10:31:04+02:00</updated>
+  <id>urn:guid:220</id>
+  <title>example title</title>
+  <summary>example summary</summary>
+</response>
+';
+               $activity = $this->InputXmlConverter->convertActivities($xml);
+               $this->assertEquals('urn:guid:220', $activity['id']);
+               $this->assertEquals('example title', $activity['title']);
+               $this->assertEquals('example summary', $activity['body']);
+               $this->assertEquals('1', $activity['streamId']);
+               $this->assertEquals('activities', $activity['streamTitle']);
+               $this->assertEquals('2008-08-05T10:31:04+02:00', 
$activity['updated']);
+               $this->assertEquals('image', 
$activity['mediaItems'][0]['type']);
+               $this->assertEquals('IMAGE', 
$activity['mediaItems'][0]['mimeType']);
+               
$this->assertEquals('http://cdn.davesdaily.com/pictures/784-awesome-hands.jpg', 
$activity['mediaItems'][0]['url']);
+       }
+
+       /**
+        * Tests InputXmlConverter->convertAppData()
+        */
+       public function testConvertAppData()
+       {
+               $xml = '<?xml version="1.0" encoding="UTF-8"?>
+<response>
+  <entry>
+    <key>sign</key>
+    <value>Virgo</value>
+  </entry>
+</response>';
+               $appdata = $this->InputXmlConverter->convertAppData($xml);
+               $expect = array('sign' => 'Virgo');
+               $this->assertEquals($expect, $appdata);
+       }
+
+       /**
+        * Tests InputXmlConverter->convertMessages()
+        */
+       public function testConvertMessages()
+       {
+               $xml = '<?xml version="1.0" encoding="UTF-8"?>
+<response xmlns:osapi="http://opensocial.org/2008/opensocialapi";>
+  <osapi:recipient>example.org:AD38B3886625AAF</osapi:recipient>
+  <osapi:recipient>example.org:997638BAA6F25AD</osapi:recipient>
+  <title>You have an invitation from Joe</title>
+  <id>{msgid}</id>
+  <body>Click &lt;a 
href="http://app.example.org/invites/{msgid}"&gt;here&lt;/a&gt; to review your 
invitation.</body>
+</response>';
+               $message = $this->InputXmlConverter->convertMessages($xml);
+               $this->assertEquals('{msgid}', $message['id']);
+               $this->assertEquals('You have an invitation from Joe', 
$message['title']);
+               $this->assertEquals('Click <a 
href="http://app.example.org/invites/{msgid}";>here</a> to review your 
invitation.', $message['body']);
+               $this->assertEquals('example.org:AD38B3886625AAF', 
$message['recipients'][0]);
+               $this->assertEquals('example.org:997638BAA6F25AD', 
$message['recipients'][1]);
+       }
+
+       /**
+        * Tests InputXmlConverter->convertPeople()
+        */
+       public function testConvertPeople()
+       {
+               $this->setExpectedException(Exception);
+               $this->InputXmlConverter->convertPeople('');
+       }
+}
\ No newline at end of file


Reply via email to