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 = '1AE5794F-C53D-FB03-5D2A-BEE6ADCD953C';
  69. $acknowledgeMessage->clientId = '6FC3B309-11DF-CB49-9A4D-0000579EAF16';
  70. $acknowledgeMessage->messageId = '1CCDEA74-75CF-ACE8-0B46-00002C38B1A4';
  71. $acknowledgeMessage->destination = null;
  72. $acknowledgeMessage->timeToLive = 0;
  73. $acknowledgeMessage->timestamp = '122297350100';
  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 = '014167F1-FCEB-6346-DCEF-BF03441367F5';
  97. $acknowledgeMessage->clientId = '6DEB5BBA-AFEE-CCA9-FB3C-00005662BA16';
  98. $acknowledgeMessage->messageId = '1822F838-FE49-11E8-730F-00000705B926';
  99. $acknowledgeMessage->destination = null;
  100. $acknowledgeMessage->timeToLive = 0;
  101. $acknowledgeMessage->timestamp = '122297537400';
  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 = '712ECAE3-2888-990E-D91C-E29E11AC7D0E';
  124. $acknowledgeMessage->clientId = '5E55BB37-59AA-A969-7373-0000158CEBB7';
  125. $acknowledgeMessage->messageId = '67B9E08C-0E35-9168-BA7B-000066ED5FF4';
  126. $acknowledgeMessage->destination = null;
  127. $acknowledgeMessage->timeToLive = 0;
  128. $acknowledgeMessage->timestamp = '122357272200';
  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 = 'F12B5C25-1302-8A8F-2A64-C14D2B3CB7D5';
  155. $acknowledgeMessage->clientId = '4C2A28C0-41BB-DA28-93C3-000018B12642';
  156. $acknowledgeMessage->messageId = '0B68113D-6210-20A9-D2AF-00002A9C1CCC';
  157. $acknowledgeMessage->destination = null;
  158. $acknowledgeMessage->timeToLive = 0;
  159. $acknowledgeMessage->timestamp = '122301345100';
  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 = 'F12B5C25-1302-8A8F-2A64-C14D2B3CB7D5';
  181. $acknowledgeMessage->clientId = '4C2A28C0-41BB-DA28-93C3-000018B12642';
  182. $acknowledgeMessage->messageId = '0B68113D-6210-20A9-D2AF-00002A9C1CCC';
  183. $acknowledgeMessage->destination = null;
  184. $acknowledgeMessage->timeToLive = 0;
  185. $acknowledgeMessage->timestamp = '122301345100';
  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 = 'A89F7C97-4D04-8778-18D0-C16C1F29F78E';
  209. $acknowledgeMessage->clientId = '336B0697-F30B-FD49-0B7E-00002E34A6BB';
  210. $acknowledgeMessage->messageId = '6D9DC7EC-A273-83A9-ABE3-00005FD752D6';
  211. $acknowledgeMessage->destination = null;
  212. $acknowledgeMessage->timeToLive = 0;
  213. $acknowledgeMessage->timestamp = '122301548000';
  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 = '12CE12FD-5D4B-AE60-853A-D36339532640';
  237. $acknowledgeMessage->clientId = '16927B78-1DBD-64E9-42BB-000019A34253';
  238. $acknowledgeMessage->messageId = '6D4F7964-6BF6-22C8-9A16-000046BD6319';
  239. $acknowledgeMessage->destination = null;
  240. $acknowledgeMessage->timeToLive = 0;
  241. $acknowledgeMessage->timestamp = '122331688500';
  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 = '5B65D04A-6703-3C98-D7F1-D36DE839E97E';
  265. $acknowledgeMessage->clientId = '32E9C012-3FC0-F0C9-4A0B-00005FE13CD9';
  266. $acknowledgeMessage->messageId = '44777AB6-A085-01A9-1241-000033DFAFEE';
  267. $acknowledgeMessage->destination = null;
  268. $acknowledgeMessage->timeToLive = 0;
  269. $acknowledgeMessage->timestamp = '122331758500';
  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 = 'C44AE645-4D12-028B-FF5F-D2E42BE5D86C';
  307. $acknowledgeMessage->clientId = '40EAAAD2-4A9B-C388-A2FD-00003A809B9E';
  308. $acknowledgeMessage->messageId = '275CD08C-6461-BBC8-B27B-000030083B2C';
  309. $acknowledgeMessage->destination = null;
  310. $acknowledgeMessage->timeToLive = 0;
  311. $acknowledgeMessage->timestamp = '122330856000';
  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 = 'C44AE645-4D12-028B-FF5F-D2E42BE5D86C';
  351. $acknowledgeMessage->clientId = '40EAAAD2-4A9B-C388-A2FD-00003A809B9E';
  352. $acknowledgeMessage->messageId = '275CD08C-6461-BBC8-B27B-000030083B2C';
  353. $acknowledgeMessage->destination = null;
  354. $acknowledgeMessage->timeToLive = 0;
  355. $acknowledgeMessage->timestamp = '122330856000';
  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 = 'C44AE645-4D12-028B-FF5F-D2E42BE5D86C';
  394. $acknowledgeMessage->clientId = '40EAAAD2-4A9B-C388-A2FD-00003A809B9E';
  395. $acknowledgeMessage->messageId = '275CD08C-6461-BBC8-B27B-000030083B2C';
  396. $acknowledgeMessage->destination = null;
  397. $acknowledgeMessage->timeToLive = 0;
  398. $acknowledgeMessage->timestamp = '122330856000';
  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. // Check that the response matches the expected serialized value
  440. $this->assertEquals($mockResponse, $testResponse);
  441. }
  442. /**
  443. * Returning a SimpleXML object to AMF is serialized into a XMString ready for E4X
  444. *
  445. * @group ZF-4999
  446. */
  447. public function testSimpleXmlSerializedToAmf3XmlString()
  448. {
  449. $sXML = '<root><element><key>a</key><value>b</value></element></root>';
  450. $data = new DOMDocument();
  451. $data->preserveWhiteSpace = false;
  452. $data->loadXML($sXML);
  453. $data = simplexml_import_dom($data);
  454. // Create an acknowlege message for a response to a RemotingMessage
  455. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  456. $acknowledgeMessage->correlationId = 'B0B0E583-5A80-826B-C2D1-D67A63D2F5E1';
  457. $acknowledgeMessage->clientId = '3D281DFB-FAC8-E368-3267-0000696DA53F';
  458. $acknowledgeMessage->messageId = '436381AA-C8C1-9749-2B05-000067CEA2CD';
  459. $acknowledgeMessage->destination = null;
  460. $acknowledgeMessage->timeToLive = 0;
  461. $acknowledgeMessage->timestamp = '122766401600';
  462. $acknowledgeMessage->body = $data;
  463. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  464. // serialize the data to an AMF output stream
  465. $this->_response->setObjectEncoding(0x03);
  466. $this->_response->addAmfBody($newBody);
  467. $this->_response->finalize();
  468. $testResponse = $this->_response->getResponse();
  469. // Load the expected response.
  470. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/domdocumentAmf3Response.bin');
  471. // Check that the response matches the expected serialized value
  472. $this->assertEquals($mockResponse, $testResponse);
  473. }
  474. /**
  475. * Check to make sure that cyclic references work inside of the AMF3 serializer
  476. * @group ZF-6205
  477. */
  478. public function testReferenceObjectsToAmf3()
  479. {
  480. $data = new ReferenceTest();
  481. $data = $data->getReference();
  482. // Create an acknowlege message for a response to a RemotingMessage
  483. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  484. $acknowledgeMessage->correlationId = '839B091C-8DDF-F6DD-2FF1-EAA82AE39608';
  485. $acknowledgeMessage->clientId = '21CC629C-58AF-2D68-A292-000006F8D883';
  486. $acknowledgeMessage->messageId = '05E70A68-FF7F-D289-1A94-00004CCECA98';
  487. $acknowledgeMessage->destination = null;
  488. $acknowledgeMessage->timeToLive = 0;
  489. $acknowledgeMessage->timestamp = '124518243200';
  490. $acknowledgeMessage->body = $data;
  491. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  492. // serialize the data to an AMF output stream
  493. $this->_response->setObjectEncoding(0x03);
  494. $this->_response->addAmfBody($newBody);
  495. $this->_response->finalize();
  496. $testResponse = $this->_response->getResponse();
  497. // Load the expected response.
  498. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/referenceObjectAmf3Response.bin');
  499. // Check that the response matches the expected serialized value
  500. $this->assertEquals($mockResponse, $testResponse);
  501. }
  502. /**
  503. * PHP string to Amf0 string
  504. *
  505. */
  506. public function testPhpStringSerializedToAmf0String()
  507. {
  508. $data = "zyxwvutsrqpmlkjihgfedcba";
  509. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  510. $this->_response->setObjectEncoding(0x00);
  511. $this->_response->addAmfBody($newBody);
  512. $this->_response->finalize();
  513. $testResponse = $this->_response->getResponse();
  514. // Load the expected response.
  515. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringAmf0Response.bin');
  516. // Check that the response matches the expected serialized value
  517. $this->assertEquals($mockResponse, $testResponse);
  518. }
  519. /**
  520. * PHP Array to Amf0 Array
  521. *
  522. */
  523. public function testPhpArraySerializedToAmf0Array()
  524. {
  525. $data = array("g", "f", "e","d","c","b","a");
  526. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  527. $this->_response->setObjectEncoding(0x00);
  528. $this->_response->addAmfBody($newBody);
  529. $this->_response->finalize();
  530. $testResponse = $this->_response->getResponse();
  531. // Load the expected response.
  532. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/arrayAmf0Response.bin');
  533. // Check that the response matches the expected serialized value
  534. $this->assertEquals($mockResponse, $testResponse);
  535. }
  536. /**
  537. * Check to make sure that we can place arrays in arrays.
  538. *
  539. * @group ZF-4712
  540. */
  541. public function testPhpNestedArraySerializedToAmf0Array()
  542. {
  543. $data = array("items"=>array("a","b"));
  544. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  545. $this->_response->setObjectEncoding(0x00);
  546. $this->_response->addAmfBody($newBody);
  547. $this->_response->finalize();
  548. $testResponse = $this->_response->getResponse();
  549. // Load the expected response.
  550. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nestedArrayAmf0Response.bin');
  551. // Check that the response matches the expected serialized value
  552. $this->assertEquals($mockResponse, $testResponse);
  553. }
  554. /**
  555. * Allow sparse arrays to be retruned to Actionscript without loosing the keys.
  556. *
  557. * @group ZF-5094
  558. */
  559. public function testPhpSparseArraySerializedToAmf0Array()
  560. {
  561. $data = array(1 => 'foo', 5 => 'bar');
  562. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  563. $this->_response->setObjectEncoding(0x00);
  564. $this->_response->addAmfBody($newBody);
  565. $this->_response->finalize();
  566. $testResponse = $this->_response->getResponse();
  567. // Load the expected response.
  568. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/sparseArrayAmf0Response.bin');
  569. // Check that the response matches the expected serialized value
  570. $this->assertEquals($mockResponse, $testResponse);
  571. }
  572. /**
  573. * Test to convert string keyed arrays are converted to objects so that we do not loose
  574. * the key refrence in the associative array.
  575. *
  576. * @group ZF-5094
  577. */
  578. public function testPhpStringKeyArrayToAmf0Object()
  579. {
  580. $data = array('foo' => 5, 'bar' => 23);
  581. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  582. $this->_response->setObjectEncoding(0x00);
  583. $this->_response->addAmfBody($newBody);
  584. $this->_response->finalize();
  585. $testResponse = $this->_response->getResponse();
  586. // Load the expected response.
  587. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringKeyArrayAmf0Response.bin');
  588. // Check that the response matches the expected serialized value
  589. $this->assertEquals($mockResponse, $testResponse);
  590. }
  591. /**
  592. * PHP Object to Amf0 Object
  593. *
  594. */
  595. public function testPhpObjectSerializedToAmf0Object()
  596. {
  597. $data = array('b'=>'bar',"a" =>'foo');
  598. $data = (object) $data;
  599. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  600. $this->_response->setObjectEncoding(0x00);
  601. $this->_response->addAmfBody($newBody);
  602. $this->_response->finalize();
  603. $testResponse = $this->_response->getResponse();
  604. // Load the expected response.
  605. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/objectAmf0Response.bin');
  606. // Check that the response matches the expected serialized value
  607. $this->assertEquals($mockResponse, $testResponse);
  608. }
  609. public function testPhpObjectSerializedToAmf0TypedObjectClassMap()
  610. {
  611. Zend_Amf_Parse_TypeLoader::setMapping("ContactVO","Contact");
  612. $data = array();
  613. $contact = new Contact();
  614. $contact->id = '15';
  615. $contact->firstname = 'Joe';
  616. $contact->lastname = 'Smith';
  617. $contact->email = 'jsmith@adobe.com';
  618. $contact->mobile = '123-456-7890';
  619. unset($contact->_explicitType);
  620. array_push( $data, $contact );
  621. $contact = new Contact();
  622. $contact->id = '23';
  623. $contact->firstname = 'Adobe';
  624. $contact->lastname = 'Flex';
  625. $contact->email = 'was@here.com';
  626. $contact->mobile = '123-456-7890';
  627. unset($contact->_explicitType);
  628. array_push( $data, $contact );
  629. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  630. $this->_response->setObjectEncoding(0x00);
  631. $this->_response->addAmfBody($newBody);
  632. $this->_response->finalize();
  633. $testResponse = $this->_response->getResponse();
  634. // Load the expected response.
  635. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  636. // Check that the response matches the expected serialized value
  637. $this->assertEquals($mockResponse, $testResponse);
  638. }
  639. public function testPhpObjectSerializedToAmf0TypedObjectExplicitType()
  640. {
  641. $data = array();
  642. $contact = new Contact();
  643. $contact->id = '15';
  644. $contact->firstname = 'Joe';
  645. $contact->lastname = 'Smith';
  646. $contact->email = 'jsmith@adobe.com';
  647. $contact->mobile = '123-456-7890';
  648. array_push( $data, $contact );
  649. $contact = new Contact();
  650. $contact->id = '23';
  651. $contact->firstname = 'Adobe';
  652. $contact->lastname = 'Flex';
  653. $contact->email = 'was@here.com';
  654. $contact->mobile = '123-456-7890';
  655. array_push( $data, $contact );
  656. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  657. $this->_response->setObjectEncoding(0x00);
  658. $this->_response->addAmfBody($newBody);
  659. $this->_response->finalize();
  660. $testResponse = $this->_response->getResponse();
  661. // Load the expected response.
  662. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  663. // Check that the response matches the expected serialized value
  664. $this->assertEquals($mockResponse, $testResponse);
  665. }
  666. public function testPhpObjectSerializedToAmf0TypedObjectGetAsClassName()
  667. {
  668. $data = array();
  669. $contact = new Contact();
  670. $contact->id = '15';
  671. $contact->firstname = 'Joe';
  672. $contact->lastname = 'Smith';
  673. $contact->email = 'jsmith@adobe.com';
  674. $contact->mobile = '123-456-7890';
  675. unset($contact->_explicitType);
  676. array_push( $data, $contact );
  677. $contact = new Contact();
  678. $contact->id = '23';
  679. $contact->firstname = 'Adobe';
  680. $contact->lastname = 'Flex';
  681. $contact->email = 'was@here.com';
  682. $contact->mobile = '123-456-7890';
  683. unset($contact->_explicitType);
  684. array_push( $data, $contact );
  685. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  686. $this->_response->setObjectEncoding(0x00);
  687. $this->_response->addAmfBody($newBody);
  688. $this->_response->finalize();
  689. $testResponse = $this->_response->getResponse();
  690. // Load the expected response.
  691. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  692. // Check that the response matches the expected serialized value
  693. $this->assertEquals($mockResponse, $testResponse);
  694. }
  695. /**
  696. * The feature test allows for php to just retun it's class name if nothing is specified. Using
  697. * _explicitType, setClassMap, getASClassName() should only be used now if you want to override the
  698. * PHP class name for specifying the return type.
  699. * @group ZF-6130
  700. */
  701. public function testPhpObjectNameSerializedToAmf0ClassName()
  702. {
  703. $data = array();
  704. $contact = new ContactVO();
  705. $contact->id = '15';
  706. $contact->firstname = 'Joe';
  707. $contact->lastname = 'Smith';
  708. $contact->email = 'jsmith@adobe.com';
  709. $contact->mobile = '123-456-7890';
  710. array_push( $data, $contact );
  711. $contact = new ContactVO();
  712. $contact->id = '23';
  713. $contact->firstname = 'Adobe';
  714. $contact->lastname = 'Flex';
  715. $contact->email = 'was@here.com';
  716. $contact->mobile = '123-456-7890';
  717. array_push( $data, $contact );
  718. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  719. $this->_response->setObjectEncoding(0x00);
  720. $this->_response->addAmfBody($newBody);
  721. $this->_response->finalize();
  722. $testResponse = $this->_response->getResponse();
  723. // Load the expected response.
  724. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  725. // Check that the response matches the expected serialized value
  726. $this->assertEquals($mockResponse, $testResponse);
  727. }
  728. /**
  729. * PHP float to Amf0 Number
  730. *
  731. */
  732. public function testPhpFloatSerializedToAmf0Number()
  733. {
  734. $data = 31.57;
  735. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  736. $this->_response->setObjectEncoding(0x00);
  737. $this->_response->addAmfBody($newBody);
  738. $this->_response->finalize();
  739. $testResponse = $this->_response->getResponse();
  740. // Load the expected response.
  741. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/numberAmf0Response.bin');
  742. // Check that the response matches the expected serialized value
  743. $this->assertEquals($mockResponse, $testResponse);
  744. }
  745. /**
  746. * PHP DateTime to Amf0 date
  747. *
  748. */
  749. public function testPhpDateTimeSerializedToAmf0Date()
  750. {
  751. date_default_timezone_set('America/Chicago');
  752. $dateSrc = '1978-10-23 4:20 America/Chicago';
  753. $date = new DateTime($dateSrc, new DateTimeZone('America/Chicago'));
  754. $data = $date;
  755. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  756. $this->_response->setObjectEncoding(0x00);
  757. $this->_response->addAmfBody($newBody);
  758. $this->_response->finalize();
  759. $testResponse = $this->_response->getResponse();
  760. // Load the expected response.
  761. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  762. // Check that the response matches the expected serialized value
  763. $this->assertEquals($mockResponse, $testResponse);
  764. }
  765. public function testZendDateSerializedToAmf0Date()
  766. {
  767. $date = new Zend_Date('October 23, 1978', null, 'en_US');
  768. $date->set('4:20:00',Zend_Date::TIMES);
  769. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$date);
  770. $this->_response->setObjectEncoding(0x00);
  771. $this->_response->addAmfBody($newBody);
  772. $this->_response->finalize();
  773. $testResponse = $this->_response->getResponse();
  774. // Load the expected response.
  775. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  776. // Check that the response matches the expected serialized value
  777. $this->assertEquals($mockResponse, $testResponse);
  778. }
  779. /**
  780. * PHP boolean true to Amf0 bool true.
  781. *
  782. */
  783. public function testPhpBoolTrueSerializedToAmf0Bool()
  784. {
  785. $data = true;
  786. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  787. $this->_response->setObjectEncoding(0x00);
  788. $this->_response->addAmfBody($newBody);
  789. $this->_response->finalize();
  790. $testResponse = $this->_response->getResponse();
  791. // Load the expected response.
  792. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolTrueAmf0Response.bin');
  793. // Check that the response matches the expected serialized value
  794. $this->assertEquals($mockResponse, $testResponse);
  795. }
  796. /**
  797. * PHP boolean true to Amf0 bool true.
  798. *
  799. */
  800. public function testPhpBoolFalseSerializedToAmf0Bool()
  801. {
  802. $data = false;
  803. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  804. $this->_response->setObjectEncoding(0x00);
  805. $this->_response->addAmfBody($newBody);
  806. $this->_response->finalize();
  807. $testResponse = $this->_response->getResponse();
  808. // Load the expected response.
  809. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolFalseAmf0Response.bin');
  810. // Check that the response matches the expected serialized value
  811. $this->assertEquals($mockResponse, $testResponse);
  812. }
  813. public function testPHPNullSerializedToAmf0Null()
  814. {
  815. $data = null;
  816. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  817. $this->_response->setObjectEncoding(0x00);
  818. $this->_response->addAmfBody($newBody);
  819. $this->_response->finalize();
  820. $testResponse = $this->_response->getResponse();
  821. // Load the expected response.
  822. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nullAmf0Response.bin');
  823. // Check that the response matches the expected serialized value
  824. $this->assertEquals($mockResponse, $testResponse);
  825. }
  826. public function testResponseShouldNotHaveMessageHeadersByDefault()
  827. {
  828. $headers = $this->_response->getAmfHeaders();
  829. $this->assertEquals(0, count($headers));
  830. }
  831. public function testResponseShouldAggregateMessageHeaders()
  832. {
  833. $this->header1 = new Zend_Amf_Value_MessageHeader('foo', false, 'bar');
  834. $this->header2 = new Zend_Amf_Value_MessageHeader('bar', true, 'baz');
  835. $this->_response->addAmfHeader($this->header1)
  836. ->addAmfHeader($this->header2);
  837. $headers = $this->_response->getAmfHeaders();
  838. $this->assertEquals(2, count($headers));
  839. $this->assertContains($this->header1, $headers);
  840. $this->assertContains($this->header2, $headers);
  841. }
  842. public function testResponseHeadersShouldBeSerializedWhenWritingMessage()
  843. {
  844. $this->testResponseShouldAggregateMessageHeaders();
  845. $this->_response->finalize();
  846. $response = $this->_response->getResponse();
  847. $request = new Zend_Amf_Request();
  848. $request->initialize($response);
  849. $headers = $request->getAmfHeaders();
  850. $this->assertEquals(2, count($headers));
  851. }
  852. public function testToStringShouldProxyToGetResponse()
  853. {
  854. $this->testResponseShouldAggregateMessageHeaders();
  855. $this->_response->finalize();
  856. $response = $this->_response->getResponse();
  857. $test = $this->_response->__toString();
  858. $this->assertSame($response, $test);
  859. }
  860. }
  861. /*
  862. * Used to test recursive cyclic references in the serializer.
  863. *@group ZF-6205
  864. */
  865. class ReferenceTest {
  866. public function getReference() {
  867. $o = new TestObject();
  868. $o->recursive = new TestObject();
  869. $o->recursive->recursive = $o;
  870. return $o;
  871. }
  872. }
  873. /**
  874. * @see ReferenceTest
  875. */
  876. class TestObject {
  877. public $recursive;
  878. }
  879. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_ResponseTest::main') {
  880. Zend_Amf_ResponseTest::main();
  881. }