ResponseTest.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. * PHP string to Amf0 string
  476. *
  477. */
  478. public function testPhpStringSerializedToAmf0String()
  479. {
  480. $data = "zyxwvutsrqpmlkjihgfedcba";
  481. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  482. $this->_response->setObjectEncoding(0x00);
  483. $this->_response->addAmfBody($newBody);
  484. $this->_response->finalize();
  485. $testResponse = $this->_response->getResponse();
  486. // Load the expected response.
  487. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringAmf0Response.bin');
  488. // Check that the response matches the expected serialized value
  489. $this->assertEquals($mockResponse, $testResponse);
  490. }
  491. /**
  492. * PHP Array to Amf0 Array
  493. *
  494. */
  495. public function testPhpArraySerializedToAmf0Array()
  496. {
  497. $data = array("g", "f", "e","d","c","b","a");
  498. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  499. $this->_response->setObjectEncoding(0x00);
  500. $this->_response->addAmfBody($newBody);
  501. $this->_response->finalize();
  502. $testResponse = $this->_response->getResponse();
  503. // Load the expected response.
  504. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/arrayAmf0Response.bin');
  505. // Check that the response matches the expected serialized value
  506. $this->assertEquals($mockResponse, $testResponse);
  507. }
  508. /**
  509. * Check to make sure that we can place arrays in arrays.
  510. *
  511. * @group ZF-4712
  512. */
  513. public function testPhpNestedArraySerializedToAmf0Array()
  514. {
  515. $data = array("items"=>array("a","b"));
  516. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  517. $this->_response->setObjectEncoding(0x00);
  518. $this->_response->addAmfBody($newBody);
  519. $this->_response->finalize();
  520. $testResponse = $this->_response->getResponse();
  521. // Load the expected response.
  522. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nestedArrayAmf0Response.bin');
  523. // Check that the response matches the expected serialized value
  524. $this->assertEquals($mockResponse, $testResponse);
  525. }
  526. /**
  527. * Allow sparse arrays to be retruned to Actionscript without loosing the keys.
  528. *
  529. * @group ZF-5094
  530. */
  531. public function testPhpSparseArraySerializedToAmf0Array()
  532. {
  533. $data = array(1 => 'foo', 5 => 'bar');
  534. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  535. $this->_response->setObjectEncoding(0x00);
  536. $this->_response->addAmfBody($newBody);
  537. $this->_response->finalize();
  538. $testResponse = $this->_response->getResponse();
  539. // Load the expected response.
  540. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/sparseArrayAmf0Response.bin');
  541. // Check that the response matches the expected serialized value
  542. $this->assertEquals($mockResponse, $testResponse);
  543. }
  544. /**
  545. * Test to convert string keyed arrays are converted to objects so that we do not loose
  546. * the key refrence in the associative array.
  547. *
  548. * @group ZF-5094
  549. */
  550. public function testPhpStringKeyArrayToAmf0Object()
  551. {
  552. $data = array('foo' => 5, 'bar' => 23);
  553. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  554. $this->_response->setObjectEncoding(0x00);
  555. $this->_response->addAmfBody($newBody);
  556. $this->_response->finalize();
  557. $testResponse = $this->_response->getResponse();
  558. // Load the expected response.
  559. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringKeyArrayAmf0Response.bin');
  560. // Check that the response matches the expected serialized value
  561. $this->assertEquals($mockResponse, $testResponse);
  562. }
  563. /**
  564. * PHP Object to Amf0 Object
  565. *
  566. */
  567. public function testPhpObjectSerializedToAmf0Object()
  568. {
  569. $data = array('b'=>'bar',"a" =>'foo');
  570. $data = (object) $data;
  571. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  572. $this->_response->setObjectEncoding(0x00);
  573. $this->_response->addAmfBody($newBody);
  574. $this->_response->finalize();
  575. $testResponse = $this->_response->getResponse();
  576. // Load the expected response.
  577. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/objectAmf0Response.bin');
  578. // Check that the response matches the expected serialized value
  579. $this->assertEquals($mockResponse, $testResponse);
  580. }
  581. public function testPhpObjectSerializedToAmf0TypedObjectClassMap()
  582. {
  583. Zend_Amf_Parse_TypeLoader::setMapping("ContactVO","Contact");
  584. $data = array();
  585. $contact = new Contact();
  586. $contact->id = '15';
  587. $contact->firstname = 'Joe';
  588. $contact->lastname = 'Smith';
  589. $contact->email = 'jsmith@adobe.com';
  590. $contact->mobile = '123-456-7890';
  591. unset($contact->_explicitType);
  592. array_push( $data, $contact );
  593. $contact = new Contact();
  594. $contact->id = '23';
  595. $contact->firstname = 'Adobe';
  596. $contact->lastname = 'Flex';
  597. $contact->email = 'was@here.com';
  598. $contact->mobile = '123-456-7890';
  599. unset($contact->_explicitType);
  600. array_push( $data, $contact );
  601. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  602. $this->_response->setObjectEncoding(0x00);
  603. $this->_response->addAmfBody($newBody);
  604. $this->_response->finalize();
  605. $testResponse = $this->_response->getResponse();
  606. // Load the expected response.
  607. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  608. // Check that the response matches the expected serialized value
  609. $this->assertEquals($mockResponse, $testResponse);
  610. }
  611. public function testPhpObjectSerializedToAmf0TypedObjectExplicitType()
  612. {
  613. $data = array();
  614. $contact = new Contact();
  615. $contact->id = '15';
  616. $contact->firstname = 'Joe';
  617. $contact->lastname = 'Smith';
  618. $contact->email = 'jsmith@adobe.com';
  619. $contact->mobile = '123-456-7890';
  620. 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. array_push( $data, $contact );
  628. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  629. $this->_response->setObjectEncoding(0x00);
  630. $this->_response->addAmfBody($newBody);
  631. $this->_response->finalize();
  632. $testResponse = $this->_response->getResponse();
  633. // Load the expected response.
  634. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  635. // Check that the response matches the expected serialized value
  636. $this->assertEquals($mockResponse, $testResponse);
  637. }
  638. public function testPhpObjectSerializedToAmf0TypedObjectGetAsClassName()
  639. {
  640. $data = array();
  641. $contact = new Contact();
  642. $contact->id = '15';
  643. $contact->firstname = 'Joe';
  644. $contact->lastname = 'Smith';
  645. $contact->email = 'jsmith@adobe.com';
  646. $contact->mobile = '123-456-7890';
  647. unset($contact->_explicitType);
  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. unset($contact->_explicitType);
  656. array_push( $data, $contact );
  657. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  658. $this->_response->setObjectEncoding(0x00);
  659. $this->_response->addAmfBody($newBody);
  660. $this->_response->finalize();
  661. $testResponse = $this->_response->getResponse();
  662. // Load the expected response.
  663. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  664. // Check that the response matches the expected serialized value
  665. $this->assertEquals($mockResponse, $testResponse);
  666. }
  667. /**
  668. * The feature test allows for php to just retun it's class name if nothing is specified. Using
  669. * _explicitType, setClassMap, getASClassName() should only be used now if you want to override the
  670. * PHP class name for specifying the return type.
  671. * @group ZF-6130
  672. */
  673. public function testPhpObjectNameSerializedToAmf0ClassName()
  674. {
  675. $data = array();
  676. $contact = new ContactVO();
  677. $contact->id = '15';
  678. $contact->firstname = 'Joe';
  679. $contact->lastname = 'Smith';
  680. $contact->email = 'jsmith@adobe.com';
  681. $contact->mobile = '123-456-7890';
  682. array_push( $data, $contact );
  683. $contact = new ContactVO();
  684. $contact->id = '23';
  685. $contact->firstname = 'Adobe';
  686. $contact->lastname = 'Flex';
  687. $contact->email = 'was@here.com';
  688. $contact->mobile = '123-456-7890';
  689. array_push( $data, $contact );
  690. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  691. $this->_response->setObjectEncoding(0x00);
  692. $this->_response->addAmfBody($newBody);
  693. $this->_response->finalize();
  694. $testResponse = $this->_response->getResponse();
  695. // Load the expected response.
  696. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  697. // Check that the response matches the expected serialized value
  698. $this->assertEquals($mockResponse, $testResponse);
  699. }
  700. /**
  701. * PHP float to Amf0 Number
  702. *
  703. */
  704. public function testPhpFloatSerializedToAmf0Number()
  705. {
  706. $data = 31.57;
  707. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  708. $this->_response->setObjectEncoding(0x00);
  709. $this->_response->addAmfBody($newBody);
  710. $this->_response->finalize();
  711. $testResponse = $this->_response->getResponse();
  712. // Load the expected response.
  713. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/numberAmf0Response.bin');
  714. // Check that the response matches the expected serialized value
  715. $this->assertEquals($mockResponse, $testResponse);
  716. }
  717. /**
  718. * PHP DateTime to Amf0 date
  719. *
  720. */
  721. public function testPhpDateTimeSerializedToAmf0Date()
  722. {
  723. date_default_timezone_set('America/Chicago');
  724. $dateSrc = '1978-10-23 4:20 America/Chicago';
  725. $date = new DateTime($dateSrc, new DateTimeZone('America/Chicago'));
  726. $data = $date;
  727. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  728. $this->_response->setObjectEncoding(0x00);
  729. $this->_response->addAmfBody($newBody);
  730. $this->_response->finalize();
  731. $testResponse = $this->_response->getResponse();
  732. // Load the expected response.
  733. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  734. // Check that the response matches the expected serialized value
  735. $this->assertEquals($mockResponse, $testResponse);
  736. }
  737. public function testZendDateSerializedToAmf0Date()
  738. {
  739. $date = new Zend_Date('October 23, 1978', null, 'en_US');
  740. $date->set('4:20:00',Zend_Date::TIMES);
  741. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$date);
  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/dateAmf0Response.bin');
  748. // Check that the response matches the expected serialized value
  749. $this->assertEquals($mockResponse, $testResponse);
  750. }
  751. /**
  752. * PHP boolean true to Amf0 bool true.
  753. *
  754. */
  755. public function testPhpBoolTrueSerializedToAmf0Bool()
  756. {
  757. $data = true;
  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/boolTrueAmf0Response.bin');
  765. // Check that the response matches the expected serialized value
  766. $this->assertEquals($mockResponse, $testResponse);
  767. }
  768. /**
  769. * PHP boolean true to Amf0 bool true.
  770. *
  771. */
  772. public function testPhpBoolFalseSerializedToAmf0Bool()
  773. {
  774. $data = false;
  775. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  776. $this->_response->setObjectEncoding(0x00);
  777. $this->_response->addAmfBody($newBody);
  778. $this->_response->finalize();
  779. $testResponse = $this->_response->getResponse();
  780. // Load the expected response.
  781. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolFalseAmf0Response.bin');
  782. // Check that the response matches the expected serialized value
  783. $this->assertEquals($mockResponse, $testResponse);
  784. }
  785. public function testPHPNullSerializedToAmf0Null()
  786. {
  787. $data = null;
  788. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  789. $this->_response->setObjectEncoding(0x00);
  790. $this->_response->addAmfBody($newBody);
  791. $this->_response->finalize();
  792. $testResponse = $this->_response->getResponse();
  793. // Load the expected response.
  794. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nullAmf0Response.bin');
  795. // Check that the response matches the expected serialized value
  796. $this->assertEquals($mockResponse, $testResponse);
  797. }
  798. public function testResponseShouldNotHaveMessageHeadersByDefault()
  799. {
  800. $headers = $this->_response->getAmfHeaders();
  801. $this->assertEquals(0, count($headers));
  802. }
  803. public function testResponseShouldAggregateMessageHeaders()
  804. {
  805. $this->header1 = new Zend_Amf_Value_MessageHeader('foo', false, 'bar');
  806. $this->header2 = new Zend_Amf_Value_MessageHeader('bar', true, 'baz');
  807. $this->_response->addAmfHeader($this->header1)
  808. ->addAmfHeader($this->header2);
  809. $headers = $this->_response->getAmfHeaders();
  810. $this->assertEquals(2, count($headers));
  811. $this->assertContains($this->header1, $headers);
  812. $this->assertContains($this->header2, $headers);
  813. }
  814. public function testResponseHeadersShouldBeSerializedWhenWritingMessage()
  815. {
  816. $this->testResponseShouldAggregateMessageHeaders();
  817. $this->_response->finalize();
  818. $response = $this->_response->getResponse();
  819. $request = new Zend_Amf_Request();
  820. $request->initialize($response);
  821. $headers = $request->getAmfHeaders();
  822. $this->assertEquals(2, count($headers));
  823. }
  824. public function testToStringShouldProxyToGetResponse()
  825. {
  826. $this->testResponseShouldAggregateMessageHeaders();
  827. $this->_response->finalize();
  828. $response = $this->_response->getResponse();
  829. $test = $this->_response->__toString();
  830. $this->assertSame($response, $test);
  831. }
  832. }
  833. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_ResponseTest::main') {
  834. Zend_Amf_ResponseTest::main();
  835. }