ResponseTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. // 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. }