ResponseTest.php 41 KB

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