ResponseTest.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. <?php
  2. if (!defined('PHPUnit_MAIN_METHOD')) {
  3. define('PHPUnit_MAIN_METHOD', 'Zend_Amf_ResponseTest::main');
  4. }
  5. require_once dirname(__FILE__) . '/../../TestHelper.php';
  6. require_once 'Zend/Amf/Response.php';
  7. require_once 'Zend/Amf/Request.php';
  8. require_once 'Zend/Amf/Value/MessageBody.php';
  9. require_once 'Zend/Amf/Value/MessageHeader.php';
  10. require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
  11. require_once 'Zend/Amf/Parse/TypeLoader.php';
  12. require_once 'Contact.php';
  13. require_once 'ContactVO.php';
  14. require_once 'Zend/Date.php';
  15. /**
  16. * Test case for Zend_Amf_Response
  17. *
  18. * @package Zend_Amf
  19. * @subpackage UnitTests
  20. * @version $Id$
  21. */
  22. class Zend_Amf_ResponseTest extends PHPUnit_Framework_TestCase
  23. {
  24. // The message response status code.
  25. public $responseURI = "/2/onResult";
  26. /**
  27. * Zend_Amf_Request object
  28. * @var Zend_Amf_Request
  29. */
  30. protected $_response;
  31. /**
  32. * Runs the test methods of this class.
  33. *
  34. * @return void
  35. */
  36. public static function main()
  37. {
  38. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_ResponseTest");
  39. $result = PHPUnit_TextUI_TestRunner::run($suite);
  40. }
  41. /**
  42. * Setup environment
  43. */
  44. public function setUp()
  45. {
  46. date_default_timezone_set('America/Chicago');
  47. Zend_Locale::setDefault('en_US');
  48. Zend_Amf_Parse_TypeLoader::resetMap();
  49. $this->_response = new Zend_Amf_Response();
  50. }
  51. /**
  52. * Teardown environment
  53. */
  54. public function tearDown()
  55. {
  56. unset($this->_response);
  57. }
  58. /**
  59. * PHP String to Amf String
  60. *
  61. */
  62. public function testPhpStringSerializedToAmf3String()
  63. {
  64. // Create php object to serialize
  65. $data = "zyxwvutsrqpmlkjihgfedcba";
  66. // Create an acknowlege message for a response to a RemotingMessage
  67. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  68. $acknowledgeMessage->correlationId = 'C626EDB9-8CF4-C305-8915-096C8AA80E2E';
  69. $acknowledgeMessage->clientId = '49D6F1AF-ADFB-3A48-5B2D-00000A5D0301';
  70. $acknowledgeMessage->messageId = '5F58E888-58E8-12A9-7A85-00006D91CCB1';
  71. $acknowledgeMessage->destination = null;
  72. $acknowledgeMessage->timeToLive = 0;
  73. $acknowledgeMessage->timestamp = '124569861800';
  74. $acknowledgeMessage->body = $data;
  75. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI, null, $acknowledgeMessage);
  76. // serialize the data to an AMF output stream
  77. $this->_response->setObjectEncoding(0x03);
  78. $this->_response->addAmfBody($newBody);
  79. $this->_response->finalize();
  80. $testResponse = $this->_response->getResponse();
  81. // Load the expected response.
  82. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringAmf3Response.bin');
  83. // Check that the response matches the expected serialized value
  84. $this->assertEquals($mockResponse, $testResponse);
  85. }
  86. /**
  87. * PHP Arrat to Amf Array
  88. *
  89. */
  90. public function testPhpArraySerializedToAmf3Array()
  91. {
  92. // Create php object to serialize
  93. $data = array("g", "f", "e","d","c","b","a");
  94. // Create an acknowlege message for a response to a RemotingMessage
  95. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  96. $acknowledgeMessage->correlationId = 'D3695635-7308-35A2-8451-09F7CAAB868A';
  97. $acknowledgeMessage->clientId = '54A7E9A2-9C2A-9849-5A3D-000070318519';
  98. $acknowledgeMessage->messageId = '2E68D735-A68E-D208-9ACC-00006FBCDE26';
  99. $acknowledgeMessage->destination = null;
  100. $acknowledgeMessage->timeToLive = 0;
  101. $acknowledgeMessage->timestamp = '124570774300';
  102. $acknowledgeMessage->body = $data;
  103. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI, null, $acknowledgeMessage);
  104. // serialize the data to an AMF output stream
  105. $this->_response->setObjectEncoding(0x03);
  106. $this->_response->addAmfBody($newBody);
  107. $this->_response->finalize();
  108. $testResponse = $this->_response->getResponse();
  109. // Load the expected response.
  110. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/arrayAmf3Response.bin');
  111. // Check that the response matches the expected serialized value
  112. $this->assertEquals($mockResponse, $testResponse);
  113. }
  114. /**
  115. * PHP float to Amf3 Number
  116. *
  117. */
  118. public function testPhpFloatSerializedToAmf3Number()
  119. {
  120. $data = 31.57;
  121. // Create an acknowlege message for a response to a RemotingMessage
  122. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  123. $acknowledgeMessage->correlationId = '1D556448-6DF0-6D0B-79C7-09798CC54A93';
  124. $acknowledgeMessage->clientId = '03EB43E5-3ADA-0F69-DA96-00007A54194D';
  125. $acknowledgeMessage->messageId = '5E4C2B6B-ADAC-4C49-52B6-0000205BC451';
  126. $acknowledgeMessage->destination = null;
  127. $acknowledgeMessage->timeToLive = 0;
  128. $acknowledgeMessage->timestamp = '124569947000';
  129. $acknowledgeMessage->body = $data;
  130. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI, null, $acknowledgeMessage);
  131. // serialize the data to an AMF output stream
  132. $this->_response->setObjectEncoding(0x03);
  133. $this->_response->addAmfBody($newBody);
  134. $this->_response->finalize();
  135. $testResponse = $this->_response->getResponse();
  136. // Load the expected response.
  137. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/numberAmf3Response.bin');
  138. // Check that the response matches the expected serialized value
  139. $this->assertEquals($mockResponse, $testResponse);
  140. }
  141. /**
  142. * PHP DateTime to Amf Date
  143. *
  144. */
  145. public function testPhpDateTimeSerializedToAmf3Date()
  146. {
  147. // Create php object to serialize
  148. date_default_timezone_set('America/Chicago');
  149. $dateSrc = '1978-10-23 4:20 America/Chicago';
  150. $date = new DateTime($dateSrc, new DateTimeZone('America/Chicago'));
  151. $data = $date;
  152. // Create an acknowlege message for a response to a RemotingMessage
  153. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  154. $acknowledgeMessage->correlationId = '77D952FE-47FA-D789-83B6-097D43403C6C';
  155. $acknowledgeMessage->clientId = '2D043296-C81C-7189-4325-000007D62DA1';
  156. $acknowledgeMessage->messageId = '2A686BAF-7D69-11C8-9A0F-0000513C0958';
  157. $acknowledgeMessage->destination = null;
  158. $acknowledgeMessage->timeToLive = 0;
  159. $acknowledgeMessage->timestamp = '124569971300';
  160. $acknowledgeMessage->body = $data;
  161. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  162. // serialize the data to an AMF output stream
  163. $this->_response->setObjectEncoding(0x03);
  164. $this->_response->addAmfBody($newBody);
  165. $this->_response->finalize();
  166. $testResponse = $this->_response->getResponse();
  167. // Load the expected response.
  168. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf3Response.bin');
  169. // Check that the response matches the expected serialized value
  170. $this->assertEquals($mockResponse, $testResponse);
  171. }
  172. public function testZendDateTimeSerializedToAmf3Date()
  173. {
  174. // Create php object to serialize
  175. $date = new Zend_Date('October 23, 1978', null, 'en_US');
  176. $date->set('4:20:00',Zend_Date::TIMES);
  177. $data = $date;
  178. // Create an acknowlege message for a response to a RemotingMessage
  179. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  180. $acknowledgeMessage->correlationId = '77D952FE-47FA-D789-83B6-097D43403C6C';
  181. $acknowledgeMessage->clientId = '2D043296-C81C-7189-4325-000007D62DA1';
  182. $acknowledgeMessage->messageId = '2A686BAF-7D69-11C8-9A0F-0000513C0958';
  183. $acknowledgeMessage->destination = null;
  184. $acknowledgeMessage->timeToLive = 0;
  185. $acknowledgeMessage->timestamp = '124569971300';
  186. $acknowledgeMessage->body = $data;
  187. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  188. // serialize the data to an AMF output stream
  189. $this->_response->setObjectEncoding(0x03);
  190. $this->_response->addAmfBody($newBody);
  191. $this->_response->finalize();
  192. $testResponse = $this->_response->getResponse();
  193. // Load the expected response.
  194. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf3Response.bin');
  195. // Check that the response matches the expected serialized value
  196. $this->assertEquals($mockResponse, $testResponse);
  197. }
  198. /**
  199. * Test the largest Integer that AS in can handle
  200. *
  201. */
  202. public function testPhpLargeIntSerializedToAmf3Int()
  203. {
  204. // Create php object to serialize
  205. $data = 268435455;
  206. // Create an acknowlege message for a response to a RemotingMessage
  207. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  208. $acknowledgeMessage->correlationId = '1D191AC2-8628-2C9A-09B2-0981CBCCF2CC';
  209. $acknowledgeMessage->clientId = '13D9DF0B-CCD0-1149-53D2-0000696908C2';
  210. $acknowledgeMessage->messageId = '03387968-E9BA-E149-A230-00006366BE67';
  211. $acknowledgeMessage->destination = null;
  212. $acknowledgeMessage->timeToLive = 0;
  213. $acknowledgeMessage->timestamp = '124570001000';
  214. $acknowledgeMessage->body = $data;
  215. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  216. // serialize the data to an AMF output stream
  217. $this->_response->setObjectEncoding(0x03);
  218. $this->_response->addAmfBody($newBody);
  219. $this->_response->finalize();
  220. $testResponse = $this->_response->getResponse();
  221. // Load the expected response.
  222. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/largeIntAmf3Response.bin');
  223. // Check that the response matches the expected serialized value
  224. $this->assertEquals($mockResponse, $testResponse);
  225. }
  226. /**
  227. * Convert boolean true to php boolean true
  228. *
  229. */
  230. public function testPhpBoolTrueSerializedToAmf3BoolTrue()
  231. {
  232. // Create php object to serialize
  233. $data = true;
  234. // Create an acknowlege message for a response to a RemotingMessage
  235. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  236. $acknowledgeMessage->correlationId = '45B8A430-A13A-FE86-D62F-098900BDF482';
  237. $acknowledgeMessage->clientId = '4000C9FB-C97B-D609-DBAA-000048B69D81';
  238. $acknowledgeMessage->messageId = '5F9AA1BF-D474-BB69-12C6-0000775127E8';
  239. $acknowledgeMessage->destination = null;
  240. $acknowledgeMessage->timeToLive = 0;
  241. $acknowledgeMessage->timestamp = '124570048300';
  242. $acknowledgeMessage->body = $data;
  243. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  244. // serialize the data to an AMF output stream
  245. $this->_response->setObjectEncoding(0x03);
  246. $this->_response->addAmfBody($newBody);
  247. $this->_response->finalize();
  248. $testResponse = $this->_response->getResponse();
  249. // Load the expected response.
  250. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolTrueAmf3Response.bin');
  251. // Check that the response matches the expected serialized value
  252. $this->assertEquals($mockResponse, $testResponse);
  253. }
  254. /**
  255. * Covert boolean false to PHP boolean false
  256. *
  257. */
  258. public function testPhpBoolFalseSerializedToAmf3BoolFalse()
  259. {
  260. // Create php object to serialize
  261. $data = false;
  262. // Create an acknowlege message for a response to a RemotingMessage
  263. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  264. $acknowledgeMessage->correlationId = '9C5D0787-7301-432E-FD4F-098681A0EE30';
  265. $acknowledgeMessage->clientId = '5AC2D840-E652-86A8-CB7A-00000418AAA4';
  266. $acknowledgeMessage->messageId = '200337C4-0932-7D68-BB24-00005EBD5F95';
  267. $acknowledgeMessage->destination = null;
  268. $acknowledgeMessage->timeToLive = 0;
  269. $acknowledgeMessage->timestamp = '124570031900';
  270. $acknowledgeMessage->body = $data;
  271. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  272. // serialize the data to an AMF output stream
  273. $this->_response->setObjectEncoding(0x03);
  274. $this->_response->addAmfBody($newBody);
  275. $this->_response->finalize();
  276. $testResponse = $this->_response->getResponse();
  277. // Load the expected response.
  278. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolFalseAmf3Response.bin');
  279. // Check that the response matches the expected serialized value
  280. $this->assertEquals($mockResponse, $testResponse);
  281. }
  282. /**
  283. * test case for taking a PHP typed object and sending it back to flex as
  284. * a typed object. uses explicit type
  285. *
  286. */
  287. public function testPhpTypedObjectSerializedToAmf3TypedObjectExplicitType()
  288. {
  289. $data = array();
  290. $contact = new Contact();
  291. $contact->id = '15';
  292. $contact->firstname = 'Joe';
  293. $contact->lastname = 'Smith';
  294. $contact->email = 'jsmith@adobe.com';
  295. $contact->mobile = '123-456-7890';
  296. array_push( $data, $contact );
  297. $contact = new Contact();
  298. $contact->id = '23';
  299. $contact->firstname = 'Adobe';
  300. $contact->lastname = 'Flex';
  301. $contact->email = 'was@here.com';
  302. $contact->mobile = '123-456-7890';
  303. array_push( $data, $contact );
  304. // Create an acknowlege message for a response to a RemotingMessage
  305. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  306. $acknowledgeMessage->correlationId = 'AF307825-478F-C4CA-AC03-09C10CD02CCC';
  307. $acknowledgeMessage->clientId = '702B4B03-89F5-34C8-1B4E-0000049466FA';
  308. $acknowledgeMessage->messageId = '704B88DF-6D5E-A228-53E3-00001DA3041F';
  309. $acknowledgeMessage->destination = null;
  310. $acknowledgeMessage->timeToLive = 0;
  311. $acknowledgeMessage->timestamp = '124570415500';
  312. $acknowledgeMessage->body = $data;
  313. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  314. // serialize the data to an AMF output stream
  315. $this->_response->setObjectEncoding(0x03);
  316. $this->_response->addAmfBody($newBody);
  317. $this->_response->finalize();
  318. $testResponse = $this->_response->getResponse();
  319. // Load the expected response.
  320. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/classMapAmf3Response.bin');
  321. // Check that the response matches the expected serialized value
  322. $this->assertEquals($mockResponse, $testResponse);
  323. }
  324. /**
  325. * Test case for taking a PHP typed object and sending it back to flex as
  326. * a typed object. uses getAsClassName
  327. *
  328. */
  329. public function testPhpTypedObjectSerializedToAmf3TypedObjectGetAsClassName()
  330. {
  331. $data = array();
  332. $contact = new Contact();
  333. $contact->id = '15';
  334. $contact->firstname = 'Joe';
  335. $contact->lastname = 'Smith';
  336. $contact->email = 'jsmith@adobe.com';
  337. $contact->mobile = '123-456-7890';
  338. unset($contact->_explicitType);
  339. array_push( $data, $contact );
  340. $contact = new Contact();
  341. $contact->id = '23';
  342. $contact->firstname = 'Adobe';
  343. $contact->lastname = 'Flex';
  344. $contact->email = 'was@here.com';
  345. $contact->mobile = '123-456-7890';
  346. unset($contact->_explicitType);
  347. array_push( $data, $contact );
  348. // Create an acknowlege message for a response to a RemotingMessage
  349. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  350. $acknowledgeMessage->correlationId = 'AF307825-478F-C4CA-AC03-09C10CD02CCC';
  351. $acknowledgeMessage->clientId = '702B4B03-89F5-34C8-1B4E-0000049466FA';
  352. $acknowledgeMessage->messageId = '704B88DF-6D5E-A228-53E3-00001DA3041F';
  353. $acknowledgeMessage->destination = null;
  354. $acknowledgeMessage->timeToLive = 0;
  355. $acknowledgeMessage->timestamp = '124570415500';
  356. $acknowledgeMessage->body = $data;
  357. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  358. // serialize the data to an AMF output stream
  359. $this->_response->setObjectEncoding(0x03);
  360. $this->_response->addAmfBody($newBody);
  361. $this->_response->finalize();
  362. $testResponse = $this->_response->getResponse();
  363. // Load the expected response.
  364. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/classMapAmf3Response.bin');
  365. // Check that the response matches the expected serialized value
  366. $this->assertEquals($mockResponse, $testResponse);
  367. }
  368. /**
  369. * The feature test allows for php to just retun it's class name if nothing is specified. Using
  370. * _explicitType, setClassMap, getASClassName() should only be used now if you want to override the
  371. * PHP class name for specifying the return type.
  372. * @group ZF-6130
  373. */
  374. public function testPhpObjectNameSerializedToAmf3ClassName()
  375. {
  376. $data = array();
  377. $contact = new Contact();
  378. $contact->id = '15';
  379. $contact->firstname = 'Joe';
  380. $contact->lastname = 'Smith';
  381. $contact->email = 'jsmith@adobe.com';
  382. $contact->mobile = '123-456-7890';
  383. array_push( $data, $contact );
  384. $contact = new Contact();
  385. $contact->id = '23';
  386. $contact->firstname = 'Adobe';
  387. $contact->lastname = 'Flex';
  388. $contact->email = 'was@here.com';
  389. $contact->mobile = '123-456-7890';
  390. array_push( $data, $contact );
  391. // Create an acknowlege message for a response to a RemotingMessage
  392. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  393. $acknowledgeMessage->correlationId = 'AF307825-478F-C4CA-AC03-09C10CD02CCC';
  394. $acknowledgeMessage->clientId = '702B4B03-89F5-34C8-1B4E-0000049466FA';
  395. $acknowledgeMessage->messageId = '704B88DF-6D5E-A228-53E3-00001DA3041F';
  396. $acknowledgeMessage->destination = null;
  397. $acknowledgeMessage->timeToLive = 0;
  398. $acknowledgeMessage->timestamp = '124570415500';
  399. $acknowledgeMessage->body = $data;
  400. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  401. // serialize the data to an AMF output stream
  402. $this->_response->setObjectEncoding(0x03);
  403. $this->_response->addAmfBody($newBody);
  404. $this->_response->finalize();
  405. $testResponse = $this->_response->getResponse();
  406. // Load the expected response.
  407. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/classMapAmf3Response.bin');
  408. // Check that the response matches the expected serialized value
  409. $this->assertEquals($mockResponse, $testResponse);
  410. }
  411. /**
  412. * Returning a DOMDocument object to AMF is serialized into a XMString ready for E4X
  413. *
  414. * @group ZF-4999
  415. *
  416. public function testPhpDomDocumentSerializedToAmf3XmlString()
  417. {
  418. $sXML = '<root><element><key>a</key><value>b</value></element></root>';
  419. $data = new DOMDocument();
  420. $data->preserveWhiteSpace = false;
  421. $data->loadXML($sXML);
  422. // Create an acknowlege message for a response to a RemotingMessage
  423. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  424. $acknowledgeMessage->correlationId = 'B0B0E583-5A80-826B-C2D1-D67A63D2F5E1';
  425. $acknowledgeMessage->clientId = '3D281DFB-FAC8-E368-3267-0000696DA53F';
  426. $acknowledgeMessage->messageId = '436381AA-C8C1-9749-2B05-000067CEA2CD';
  427. $acknowledgeMessage->destination = null;
  428. $acknowledgeMessage->timeToLive = 0;
  429. $acknowledgeMessage->timestamp = '122766401600';
  430. $acknowledgeMessage->body = $data;
  431. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  432. // serialize the data to an AMF output stream
  433. $this->_response->setObjectEncoding(0x03);
  434. $this->_response->addAmfBody($newBody);
  435. $this->_response->finalize();
  436. $testResponse = $this->_response->getResponse();
  437. // Load the expected response.
  438. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/domdocumentAmf3Response.bin');
  439. exit;
  440. // Check that the response matches the expected serialized value
  441. $this->assertEquals($mockResponse, $testResponse);
  442. }
  443. /**
  444. * Returning a SimpleXML object to AMF is serialized into a XMString ready for E4X
  445. *
  446. * @group ZF-4999
  447. *
  448. public function testSimpleXmlSerializedToAmf3XmlString()
  449. {
  450. $sXML = '<root><element><key>a</key><value>b</value></element></root>';
  451. $data = new DOMDocument();
  452. $data->preserveWhiteSpace = false;
  453. $data->loadXML($sXML);
  454. $data = simplexml_import_dom($data);
  455. // Create an acknowlege message for a response to a RemotingMessage
  456. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  457. $acknowledgeMessage->correlationId = 'B0B0E583-5A80-826B-C2D1-D67A63D2F5E1';
  458. $acknowledgeMessage->clientId = '3D281DFB-FAC8-E368-3267-0000696DA53F';
  459. $acknowledgeMessage->messageId = '436381AA-C8C1-9749-2B05-000067CEA2CD';
  460. $acknowledgeMessage->destination = null;
  461. $acknowledgeMessage->timeToLive = 0;
  462. $acknowledgeMessage->timestamp = '122766401600';
  463. $acknowledgeMessage->body = $data;
  464. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  465. // serialize the data to an AMF output stream
  466. $this->_response->setObjectEncoding(0x03);
  467. $this->_response->addAmfBody($newBody);
  468. $this->_response->finalize();
  469. $testResponse = $this->_response->getResponse();
  470. // Load the expected response.
  471. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/domdocumentAmf3Response.bin');
  472. // Check that the response matches the expected serialized value
  473. $this->assertEquals($mockResponse, $testResponse);
  474. }
  475. /**
  476. * Check to make sure that cyclic references work inside of the AMF3 serializer
  477. * @group ZF-6205
  478. *
  479. public function testReferenceObjectsToAmf3()
  480. {
  481. $data = new ReferenceTest();
  482. $data = $data->getReference();
  483. // Create an acknowlege message for a response to a RemotingMessage
  484. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  485. $acknowledgeMessage->correlationId = '839B091C-8DDF-F6DD-2FF1-EAA82AE39608';
  486. $acknowledgeMessage->clientId = '21CC629C-58AF-2D68-A292-000006F8D883';
  487. $acknowledgeMessage->messageId = '05E70A68-FF7F-D289-1A94-00004CCECA98';
  488. $acknowledgeMessage->destination = null;
  489. $acknowledgeMessage->timeToLive = 0;
  490. $acknowledgeMessage->timestamp = '124518243200';
  491. $acknowledgeMessage->body = $data;
  492. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  493. // serialize the data to an AMF output stream
  494. $this->_response->setObjectEncoding(0x03);
  495. $this->_response->addAmfBody($newBody);
  496. $this->_response->finalize();
  497. $testResponse = $this->_response->getResponse();
  498. // Load the expected response.
  499. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/referenceObjectAmf3Response.bin');
  500. // Check that the response matches the expected serialized value
  501. $this->assertEquals($mockResponse, $testResponse);
  502. }
  503. /**
  504. * PHP string to Amf0 string
  505. *
  506. */
  507. public function testPhpStringSerializedToAmf0String()
  508. {
  509. $data = "zyxwvutsrqpmlkjihgfedcba";
  510. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  511. $this->_response->setObjectEncoding(0x00);
  512. $this->_response->addAmfBody($newBody);
  513. $this->_response->finalize();
  514. $testResponse = $this->_response->getResponse();
  515. // Load the expected response.
  516. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringAmf0Response.bin');
  517. // Check that the response matches the expected serialized value
  518. $this->assertEquals($mockResponse, $testResponse);
  519. }
  520. /**
  521. * PHP Array to Amf0 Array
  522. *
  523. */
  524. public function testPhpArraySerializedToAmf0Array()
  525. {
  526. $data = array("g", "f", "e","d","c","b","a");
  527. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  528. $this->_response->setObjectEncoding(0x00);
  529. $this->_response->addAmfBody($newBody);
  530. $this->_response->finalize();
  531. $testResponse = $this->_response->getResponse();
  532. // Load the expected response.
  533. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/arrayAmf0Response.bin');
  534. // Check that the response matches the expected serialized value
  535. $this->assertEquals($mockResponse, $testResponse);
  536. }
  537. /**
  538. * Check to make sure that we can place arrays in arrays.
  539. *
  540. * @group ZF-4712
  541. */
  542. public function testPhpNestedArraySerializedToAmf0Array()
  543. {
  544. $data = array("items"=>array("a","b"));
  545. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  546. $this->_response->setObjectEncoding(0x00);
  547. $this->_response->addAmfBody($newBody);
  548. $this->_response->finalize();
  549. $testResponse = $this->_response->getResponse();
  550. // Load the expected response.
  551. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nestedArrayAmf0Response.bin');
  552. // Check that the response matches the expected serialized value
  553. $this->assertEquals($mockResponse, $testResponse);
  554. }
  555. /**
  556. * Allow sparse arrays to be retruned to Actionscript without loosing the keys.
  557. *
  558. * @group ZF-5094
  559. */
  560. public function testPhpSparseArraySerializedToAmf0Array()
  561. {
  562. $data = array(1 => 'foo', 5 => 'bar');
  563. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  564. $this->_response->setObjectEncoding(0x00);
  565. $this->_response->addAmfBody($newBody);
  566. $this->_response->finalize();
  567. $testResponse = $this->_response->getResponse();
  568. // Load the expected response.
  569. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/sparseArrayAmf0Response.bin');
  570. // Check that the response matches the expected serialized value
  571. $this->assertEquals($mockResponse, $testResponse);
  572. }
  573. /**
  574. * Test to convert string keyed arrays are converted to objects so that we do not loose
  575. * the key refrence in the associative array.
  576. *
  577. * @group ZF-5094
  578. */
  579. public function testPhpStringKeyArrayToAmf0Object()
  580. {
  581. $data = array('foo' => 5, 'bar' => 23);
  582. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  583. $this->_response->setObjectEncoding(0x00);
  584. $this->_response->addAmfBody($newBody);
  585. $this->_response->finalize();
  586. $testResponse = $this->_response->getResponse();
  587. // Load the expected response.
  588. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringKeyArrayAmf0Response.bin');
  589. // Check that the response matches the expected serialized value
  590. $this->assertEquals($mockResponse, $testResponse);
  591. }
  592. /**
  593. * PHP Object to Amf0 Object
  594. *
  595. */
  596. public function testPhpObjectSerializedToAmf0Object()
  597. {
  598. $data = array('b'=>'bar',"a" =>'foo');
  599. $data = (object) $data;
  600. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  601. $this->_response->setObjectEncoding(0x00);
  602. $this->_response->addAmfBody($newBody);
  603. $this->_response->finalize();
  604. $testResponse = $this->_response->getResponse();
  605. // Load the expected response.
  606. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/objectAmf0Response.bin');
  607. // Check that the response matches the expected serialized value
  608. $this->assertEquals($mockResponse, $testResponse);
  609. }
  610. public function testPhpObjectSerializedToAmf0TypedObjectClassMap()
  611. {
  612. Zend_Amf_Parse_TypeLoader::setMapping("ContactVO","Contact");
  613. $data = array();
  614. $contact = new Contact();
  615. $contact->id = '15';
  616. $contact->firstname = 'Joe';
  617. $contact->lastname = 'Smith';
  618. $contact->email = 'jsmith@adobe.com';
  619. $contact->mobile = '123-456-7890';
  620. unset($contact->_explicitType);
  621. array_push( $data, $contact );
  622. $contact = new Contact();
  623. $contact->id = '23';
  624. $contact->firstname = 'Adobe';
  625. $contact->lastname = 'Flex';
  626. $contact->email = 'was@here.com';
  627. $contact->mobile = '123-456-7890';
  628. unset($contact->_explicitType);
  629. array_push( $data, $contact );
  630. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  631. $this->_response->setObjectEncoding(0x00);
  632. $this->_response->addAmfBody($newBody);
  633. $this->_response->finalize();
  634. $testResponse = $this->_response->getResponse();
  635. // Load the expected response.
  636. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  637. // Check that the response matches the expected serialized value
  638. $this->assertEquals($mockResponse, $testResponse);
  639. }
  640. public function testPhpObjectSerializedToAmf0TypedObjectExplicitType()
  641. {
  642. $data = array();
  643. $contact = new Contact();
  644. $contact->id = '15';
  645. $contact->firstname = 'Joe';
  646. $contact->lastname = 'Smith';
  647. $contact->email = 'jsmith@adobe.com';
  648. $contact->mobile = '123-456-7890';
  649. array_push( $data, $contact );
  650. $contact = new Contact();
  651. $contact->id = '23';
  652. $contact->firstname = 'Adobe';
  653. $contact->lastname = 'Flex';
  654. $contact->email = 'was@here.com';
  655. $contact->mobile = '123-456-7890';
  656. array_push( $data, $contact );
  657. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  658. $this->_response->setObjectEncoding(0x00);
  659. $this->_response->addAmfBody($newBody);
  660. $this->_response->finalize();
  661. $testResponse = $this->_response->getResponse();
  662. // Load the expected response.
  663. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  664. // Check that the response matches the expected serialized value
  665. $this->assertEquals($mockResponse, $testResponse);
  666. }
  667. public function testPhpObjectSerializedToAmf0TypedObjectGetAsClassName()
  668. {
  669. $data = array();
  670. $contact = new Contact();
  671. $contact->id = '15';
  672. $contact->firstname = 'Joe';
  673. $contact->lastname = 'Smith';
  674. $contact->email = 'jsmith@adobe.com';
  675. $contact->mobile = '123-456-7890';
  676. unset($contact->_explicitType);
  677. array_push( $data, $contact );
  678. $contact = new Contact();
  679. $contact->id = '23';
  680. $contact->firstname = 'Adobe';
  681. $contact->lastname = 'Flex';
  682. $contact->email = 'was@here.com';
  683. $contact->mobile = '123-456-7890';
  684. unset($contact->_explicitType);
  685. array_push( $data, $contact );
  686. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  687. $this->_response->setObjectEncoding(0x00);
  688. $this->_response->addAmfBody($newBody);
  689. $this->_response->finalize();
  690. $testResponse = $this->_response->getResponse();
  691. // Load the expected response.
  692. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  693. // Check that the response matches the expected serialized value
  694. $this->assertEquals($mockResponse, $testResponse);
  695. }
  696. /**
  697. * The feature test allows for php to just retun it's class name if nothing is specified. Using
  698. * _explicitType, setClassMap, getASClassName() should only be used now if you want to override the
  699. * PHP class name for specifying the return type.
  700. * @group ZF-6130
  701. */
  702. public function testPhpObjectNameSerializedToAmf0ClassName()
  703. {
  704. $data = array();
  705. $contact = new ContactVO();
  706. $contact->id = '15';
  707. $contact->firstname = 'Joe';
  708. $contact->lastname = 'Smith';
  709. $contact->email = 'jsmith@adobe.com';
  710. $contact->mobile = '123-456-7890';
  711. array_push( $data, $contact );
  712. $contact = new ContactVO();
  713. $contact->id = '23';
  714. $contact->firstname = 'Adobe';
  715. $contact->lastname = 'Flex';
  716. $contact->email = 'was@here.com';
  717. $contact->mobile = '123-456-7890';
  718. array_push( $data, $contact );
  719. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  720. $this->_response->setObjectEncoding(0x00);
  721. $this->_response->addAmfBody($newBody);
  722. $this->_response->finalize();
  723. $testResponse = $this->_response->getResponse();
  724. // Load the expected response.
  725. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  726. // Check that the response matches the expected serialized value
  727. $this->assertEquals($mockResponse, $testResponse);
  728. }
  729. /**
  730. * PHP float to Amf0 Number
  731. *
  732. */
  733. public function testPhpFloatSerializedToAmf0Number()
  734. {
  735. $data = 31.57;
  736. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  737. $this->_response->setObjectEncoding(0x00);
  738. $this->_response->addAmfBody($newBody);
  739. $this->_response->finalize();
  740. $testResponse = $this->_response->getResponse();
  741. // Load the expected response.
  742. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/numberAmf0Response.bin');
  743. // Check that the response matches the expected serialized value
  744. $this->assertEquals($mockResponse, $testResponse);
  745. }
  746. /**
  747. * PHP DateTime to Amf0 date
  748. *
  749. */
  750. public function testPhpDateTimeSerializedToAmf0Date()
  751. {
  752. date_default_timezone_set('America/Chicago');
  753. $dateSrc = '1978-10-23 4:20 America/Chicago';
  754. $date = new DateTime($dateSrc, new DateTimeZone('America/Chicago'));
  755. $data = $date;
  756. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  757. $this->_response->setObjectEncoding(0x00);
  758. $this->_response->addAmfBody($newBody);
  759. $this->_response->finalize();
  760. $testResponse = $this->_response->getResponse();
  761. // Load the expected response.
  762. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  763. // Check that the response matches the expected serialized value
  764. $this->assertEquals($mockResponse, $testResponse);
  765. }
  766. public function testZendDateSerializedToAmf0Date()
  767. {
  768. $date = new Zend_Date('October 23, 1978', null, 'en_US');
  769. $date->set('4:20:00',Zend_Date::TIMES);
  770. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$date);
  771. $this->_response->setObjectEncoding(0x00);
  772. $this->_response->addAmfBody($newBody);
  773. $this->_response->finalize();
  774. $testResponse = $this->_response->getResponse();
  775. // Load the expected response.
  776. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  777. // Check that the response matches the expected serialized value
  778. $this->assertEquals($mockResponse, $testResponse);
  779. }
  780. /**
  781. * PHP boolean true to Amf0 bool true.
  782. *
  783. */
  784. public function testPhpBoolTrueSerializedToAmf0Bool()
  785. {
  786. $data = true;
  787. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  788. $this->_response->setObjectEncoding(0x00);
  789. $this->_response->addAmfBody($newBody);
  790. $this->_response->finalize();
  791. $testResponse = $this->_response->getResponse();
  792. // Load the expected response.
  793. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolTrueAmf0Response.bin');
  794. // Check that the response matches the expected serialized value
  795. $this->assertEquals($mockResponse, $testResponse);
  796. }
  797. /**
  798. * PHP boolean true to Amf0 bool true.
  799. *
  800. */
  801. public function testPhpBoolFalseSerializedToAmf0Bool()
  802. {
  803. $data = false;
  804. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  805. $this->_response->setObjectEncoding(0x00);
  806. $this->_response->addAmfBody($newBody);
  807. $this->_response->finalize();
  808. $testResponse = $this->_response->getResponse();
  809. // Load the expected response.
  810. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolFalseAmf0Response.bin');
  811. // Check that the response matches the expected serialized value
  812. $this->assertEquals($mockResponse, $testResponse);
  813. }
  814. public function testPHPNullSerializedToAmf0Null()
  815. {
  816. $data = null;
  817. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  818. $this->_response->setObjectEncoding(0x00);
  819. $this->_response->addAmfBody($newBody);
  820. $this->_response->finalize();
  821. $testResponse = $this->_response->getResponse();
  822. // Load the expected response.
  823. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nullAmf0Response.bin');
  824. // Check that the response matches the expected serialized value
  825. $this->assertEquals($mockResponse, $testResponse);
  826. }
  827. public function testResponseShouldNotHaveMessageHeadersByDefault()
  828. {
  829. $headers = $this->_response->getAmfHeaders();
  830. $this->assertEquals(0, count($headers));
  831. }
  832. public function testResponseShouldAggregateMessageHeaders()
  833. {
  834. $this->header1 = new Zend_Amf_Value_MessageHeader('foo', false, 'bar');
  835. $this->header2 = new Zend_Amf_Value_MessageHeader('bar', true, 'baz');
  836. $this->_response->addAmfHeader($this->header1)
  837. ->addAmfHeader($this->header2);
  838. $headers = $this->_response->getAmfHeaders();
  839. $this->assertEquals(2, count($headers));
  840. $this->assertContains($this->header1, $headers);
  841. $this->assertContains($this->header2, $headers);
  842. }
  843. public function testResponseHeadersShouldBeSerializedWhenWritingMessage()
  844. {
  845. $this->testResponseShouldAggregateMessageHeaders();
  846. $this->_response->finalize();
  847. $response = $this->_response->getResponse();
  848. $request = new Zend_Amf_Request();
  849. $request->initialize($response);
  850. $headers = $request->getAmfHeaders();
  851. $this->assertEquals(2, count($headers));
  852. }
  853. public function testToStringShouldProxyToGetResponse()
  854. {
  855. $this->testResponseShouldAggregateMessageHeaders();
  856. $this->_response->finalize();
  857. $response = $this->_response->getResponse();
  858. $test = $this->_response->__toString();
  859. $this->assertSame($response, $test);
  860. }
  861. }
  862. /*
  863. * Used to test recursive cyclic references in the serializer.
  864. *@group ZF-6205
  865. */
  866. class ReferenceTest {
  867. public function getReference() {
  868. $o = new TestObject();
  869. $o->recursive = new TestObject();
  870. $o->recursive->recursive = $o;
  871. return $o;
  872. }
  873. }
  874. /**
  875. * @see ReferenceTest
  876. */
  877. class TestObject {
  878. public $recursive;
  879. }
  880. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_ResponseTest::main') {
  881. Zend_Amf_ResponseTest::main();
  882. }