ResponseTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 'Zend/Date.php';
  14. /**
  15. * Test case for Zend_Amf_Response
  16. *
  17. * @package Zend_Amf
  18. * @subpackage UnitTests
  19. * @version $Id$
  20. */
  21. class Zend_Amf_ResponseTest extends PHPUnit_Framework_TestCase
  22. {
  23. // The message response status code.
  24. public $responseURI = "/2/onResult";
  25. /**
  26. * Zend_Amf_Request object
  27. * @var Zend_Amf_Request
  28. */
  29. protected $_response;
  30. /**
  31. * Runs the test methods of this class.
  32. *
  33. * @return void
  34. */
  35. public static function main()
  36. {
  37. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_ResponseTest");
  38. $result = PHPUnit_TextUI_TestRunner::run($suite);
  39. }
  40. /**
  41. * Setup environment
  42. */
  43. public function setUp()
  44. {
  45. date_default_timezone_set('America/Chicago');
  46. Zend_Locale::setDefault('en_US');
  47. Zend_Amf_Parse_TypeLoader::resetMap();
  48. $this->_response = new Zend_Amf_Response();
  49. }
  50. /**
  51. * Teardown environment
  52. */
  53. public function tearDown()
  54. {
  55. unset($this->_response);
  56. }
  57. /**
  58. * PHP String to Amf String
  59. *
  60. */
  61. public function testPhpStringSerializedToAmf3String()
  62. {
  63. // Create php object to serialize
  64. $data = "zyxwvutsrqpmlkjihgfedcba";
  65. // Create an acknowlege message for a response to a RemotingMessage
  66. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  67. $acknowledgeMessage->correlationId = '1AE5794F-C53D-FB03-5D2A-BEE6ADCD953C';
  68. $acknowledgeMessage->clientId = '6FC3B309-11DF-CB49-9A4D-0000579EAF16';
  69. $acknowledgeMessage->messageId = '1CCDEA74-75CF-ACE8-0B46-00002C38B1A4';
  70. $acknowledgeMessage->destination = null;
  71. $acknowledgeMessage->timeToLive = 0;
  72. $acknowledgeMessage->timestamp = '122297350100';
  73. $acknowledgeMessage->body = $data;
  74. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI, null, $acknowledgeMessage);
  75. // serialize the data to an AMF output stream
  76. $this->_response->setObjectEncoding(0x03);
  77. $this->_response->addAmfBody($newBody);
  78. $this->_response->finalize();
  79. $testResponse = $this->_response->getResponse();
  80. // Load the expected response.
  81. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringAmf3Response.bin');
  82. // Check that the response matches the expected serialized value
  83. $this->assertEquals($mockResponse, $testResponse);
  84. }
  85. /**
  86. * PHP Arrat to Amf Array
  87. *
  88. */
  89. public function testPhpArraySerializedToAmf3Array()
  90. {
  91. // Create php object to serialize
  92. $data = array("g", "f", "e","d","c","b","a");
  93. // Create an acknowlege message for a response to a RemotingMessage
  94. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  95. $acknowledgeMessage->correlationId = '014167F1-FCEB-6346-DCEF-BF03441367F5';
  96. $acknowledgeMessage->clientId = '6DEB5BBA-AFEE-CCA9-FB3C-00005662BA16';
  97. $acknowledgeMessage->messageId = '1822F838-FE49-11E8-730F-00000705B926';
  98. $acknowledgeMessage->destination = null;
  99. $acknowledgeMessage->timeToLive = 0;
  100. $acknowledgeMessage->timestamp = '122297537400';
  101. $acknowledgeMessage->body = $data;
  102. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI, null, $acknowledgeMessage);
  103. // serialize the data to an AMF output stream
  104. $this->_response->setObjectEncoding(0x03);
  105. $this->_response->addAmfBody($newBody);
  106. $this->_response->finalize();
  107. $testResponse = $this->_response->getResponse();
  108. // Load the expected response.
  109. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/arrayAmf3Response.bin');
  110. // Check that the response matches the expected serialized value
  111. $this->assertEquals($mockResponse, $testResponse);
  112. }
  113. /**
  114. * PHP float to Amf3 Number
  115. *
  116. */
  117. public function testPhpFloatSerializedToAmf3Number()
  118. {
  119. $data = 31.57;
  120. // Create an acknowlege message for a response to a RemotingMessage
  121. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  122. $acknowledgeMessage->correlationId = '712ECAE3-2888-990E-D91C-E29E11AC7D0E';
  123. $acknowledgeMessage->clientId = '5E55BB37-59AA-A969-7373-0000158CEBB7';
  124. $acknowledgeMessage->messageId = '67B9E08C-0E35-9168-BA7B-000066ED5FF4';
  125. $acknowledgeMessage->destination = null;
  126. $acknowledgeMessage->timeToLive = 0;
  127. $acknowledgeMessage->timestamp = '122357272200';
  128. $acknowledgeMessage->body = $data;
  129. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI, null, $acknowledgeMessage);
  130. // serialize the data to an AMF output stream
  131. $this->_response->setObjectEncoding(0x03);
  132. $this->_response->addAmfBody($newBody);
  133. $this->_response->finalize();
  134. $testResponse = $this->_response->getResponse();
  135. // Load the expected response.
  136. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/numberAmf3Response.bin');
  137. // Check that the response matches the expected serialized value
  138. $this->assertEquals($mockResponse, $testResponse);
  139. }
  140. /**
  141. * PHP DateTime to Amf Date
  142. *
  143. */
  144. public function testPhpDateTimeSerializedToAmf3Date()
  145. {
  146. // Create php object to serialize
  147. date_default_timezone_set('America/Chicago');
  148. $dateSrc = '1978-10-23 4:20 America/Chicago';
  149. $date = new DateTime($dateSrc, new DateTimeZone('America/Chicago'));
  150. $data = $date;
  151. // Create an acknowlege message for a response to a RemotingMessage
  152. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  153. $acknowledgeMessage->correlationId = 'F12B5C25-1302-8A8F-2A64-C14D2B3CB7D5';
  154. $acknowledgeMessage->clientId = '4C2A28C0-41BB-DA28-93C3-000018B12642';
  155. $acknowledgeMessage->messageId = '0B68113D-6210-20A9-D2AF-00002A9C1CCC';
  156. $acknowledgeMessage->destination = null;
  157. $acknowledgeMessage->timeToLive = 0;
  158. $acknowledgeMessage->timestamp = '122301345100';
  159. $acknowledgeMessage->body = $data;
  160. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  161. // serialize the data to an AMF output stream
  162. $this->_response->setObjectEncoding(0x03);
  163. $this->_response->addAmfBody($newBody);
  164. $this->_response->finalize();
  165. $testResponse = $this->_response->getResponse();
  166. // Load the expected response.
  167. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf3Response.bin');
  168. // Check that the response matches the expected serialized value
  169. $this->assertEquals($mockResponse, $testResponse);
  170. }
  171. public function testZendDateTimeSerializedToAmf3Date()
  172. {
  173. // Create php object to serialize
  174. $date = new Zend_Date('October 23, 1978', null, 'en_US');
  175. $date->set('4:20:00',Zend_Date::TIMES);
  176. $data = $date;
  177. // Create an acknowlege message for a response to a RemotingMessage
  178. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  179. $acknowledgeMessage->correlationId = 'F12B5C25-1302-8A8F-2A64-C14D2B3CB7D5';
  180. $acknowledgeMessage->clientId = '4C2A28C0-41BB-DA28-93C3-000018B12642';
  181. $acknowledgeMessage->messageId = '0B68113D-6210-20A9-D2AF-00002A9C1CCC';
  182. $acknowledgeMessage->destination = null;
  183. $acknowledgeMessage->timeToLive = 0;
  184. $acknowledgeMessage->timestamp = '122301345100';
  185. $acknowledgeMessage->body = $data;
  186. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  187. // serialize the data to an AMF output stream
  188. $this->_response->setObjectEncoding(0x03);
  189. $this->_response->addAmfBody($newBody);
  190. $this->_response->finalize();
  191. $testResponse = $this->_response->getResponse();
  192. // Load the expected response.
  193. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf3Response.bin');
  194. // Check that the response matches the expected serialized value
  195. $this->assertEquals($mockResponse, $testResponse);
  196. }
  197. /**
  198. * Test the largest Integer that AS in can handle
  199. *
  200. */
  201. public function testPhpLargeIntSerializedToAmf3Int()
  202. {
  203. // Create php object to serialize
  204. $data = 268435455;
  205. // Create an acknowlege message for a response to a RemotingMessage
  206. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  207. $acknowledgeMessage->correlationId = 'A89F7C97-4D04-8778-18D0-C16C1F29F78E';
  208. $acknowledgeMessage->clientId = '336B0697-F30B-FD49-0B7E-00002E34A6BB';
  209. $acknowledgeMessage->messageId = '6D9DC7EC-A273-83A9-ABE3-00005FD752D6';
  210. $acknowledgeMessage->destination = null;
  211. $acknowledgeMessage->timeToLive = 0;
  212. $acknowledgeMessage->timestamp = '122301548000';
  213. $acknowledgeMessage->body = $data;
  214. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  215. // serialize the data to an AMF output stream
  216. $this->_response->setObjectEncoding(0x03);
  217. $this->_response->addAmfBody($newBody);
  218. $this->_response->finalize();
  219. $testResponse = $this->_response->getResponse();
  220. // Load the expected response.
  221. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/largeIntAmf3Response.bin');
  222. // Check that the response matches the expected serialized value
  223. $this->assertEquals($mockResponse, $testResponse);
  224. }
  225. /**
  226. * Convert boolean true to php boolean true
  227. *
  228. */
  229. public function testPhpBoolTrueSerializedToAmf3BoolTrue()
  230. {
  231. // Create php object to serialize
  232. $data = true;
  233. // Create an acknowlege message for a response to a RemotingMessage
  234. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  235. $acknowledgeMessage->correlationId = '12CE12FD-5D4B-AE60-853A-D36339532640';
  236. $acknowledgeMessage->clientId = '16927B78-1DBD-64E9-42BB-000019A34253';
  237. $acknowledgeMessage->messageId = '6D4F7964-6BF6-22C8-9A16-000046BD6319';
  238. $acknowledgeMessage->destination = null;
  239. $acknowledgeMessage->timeToLive = 0;
  240. $acknowledgeMessage->timestamp = '122331688500';
  241. $acknowledgeMessage->body = $data;
  242. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  243. // serialize the data to an AMF output stream
  244. $this->_response->setObjectEncoding(0x03);
  245. $this->_response->addAmfBody($newBody);
  246. $this->_response->finalize();
  247. $testResponse = $this->_response->getResponse();
  248. // Load the expected response.
  249. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolTrueAmf3Response.bin');
  250. // Check that the response matches the expected serialized value
  251. $this->assertEquals($mockResponse, $testResponse);
  252. }
  253. /**
  254. * Covert boolean false to PHP boolean false
  255. *
  256. */
  257. public function testPhpBoolFalseSerializedToAmf3BoolFalse()
  258. {
  259. // Create php object to serialize
  260. $data = false;
  261. // Create an acknowlege message for a response to a RemotingMessage
  262. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  263. $acknowledgeMessage->correlationId = '5B65D04A-6703-3C98-D7F1-D36DE839E97E';
  264. $acknowledgeMessage->clientId = '32E9C012-3FC0-F0C9-4A0B-00005FE13CD9';
  265. $acknowledgeMessage->messageId = '44777AB6-A085-01A9-1241-000033DFAFEE';
  266. $acknowledgeMessage->destination = null;
  267. $acknowledgeMessage->timeToLive = 0;
  268. $acknowledgeMessage->timestamp = '122331758500';
  269. $acknowledgeMessage->body = $data;
  270. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  271. // serialize the data to an AMF output stream
  272. $this->_response->setObjectEncoding(0x03);
  273. $this->_response->addAmfBody($newBody);
  274. $this->_response->finalize();
  275. $testResponse = $this->_response->getResponse();
  276. // Load the expected response.
  277. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolFalseAmf3Response.bin');
  278. // Check that the response matches the expected serialized value
  279. $this->assertEquals($mockResponse, $testResponse);
  280. }
  281. /**
  282. * test case for taking a PHP typed object and sending it back to flex as
  283. * a typed object. uses explicit type
  284. *
  285. */
  286. public function testPhpTypedObjectSerializedToAmf3TypedObjectExplicitType()
  287. {
  288. $data = array();
  289. $contact = new Contact();
  290. $contact->id = '15';
  291. $contact->firstname = 'Joe';
  292. $contact->lastname = 'Smith';
  293. $contact->email = 'jsmith@adobe.com';
  294. $contact->mobile = '123-456-7890';
  295. array_push( $data, $contact );
  296. $contact = new Contact();
  297. $contact->id = '23';
  298. $contact->firstname = 'Adobe';
  299. $contact->lastname = 'Flex';
  300. $contact->email = 'was@here.com';
  301. $contact->mobile = '123-456-7890';
  302. array_push( $data, $contact );
  303. // Create an acknowlege message for a response to a RemotingMessage
  304. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  305. $acknowledgeMessage->correlationId = 'C44AE645-4D12-028B-FF5F-D2E42BE5D86C';
  306. $acknowledgeMessage->clientId = '40EAAAD2-4A9B-C388-A2FD-00003A809B9E';
  307. $acknowledgeMessage->messageId = '275CD08C-6461-BBC8-B27B-000030083B2C';
  308. $acknowledgeMessage->destination = null;
  309. $acknowledgeMessage->timeToLive = 0;
  310. $acknowledgeMessage->timestamp = '122330856000';
  311. $acknowledgeMessage->body = $data;
  312. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  313. // serialize the data to an AMF output stream
  314. $this->_response->setObjectEncoding(0x03);
  315. $this->_response->addAmfBody($newBody);
  316. $this->_response->finalize();
  317. $testResponse = $this->_response->getResponse();
  318. // Load the expected response.
  319. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/classMapAmf3Response.bin');
  320. // Check that the response matches the expected serialized value
  321. $this->assertEquals($mockResponse, $testResponse);
  322. }
  323. /**
  324. * Test case for taking a PHP typed object and sending it back to flex as
  325. * a typed object. uses getAsClassName
  326. *
  327. */
  328. public function testPhpTypedObjectSerializedToAmf3TypedObjectGetAsClassName()
  329. {
  330. $data = array();
  331. $contact = new Contact();
  332. $contact->id = '15';
  333. $contact->firstname = 'Joe';
  334. $contact->lastname = 'Smith';
  335. $contact->email = 'jsmith@adobe.com';
  336. $contact->mobile = '123-456-7890';
  337. unset($contact->_explicitType);
  338. array_push( $data, $contact );
  339. $contact = new Contact();
  340. $contact->id = '23';
  341. $contact->firstname = 'Adobe';
  342. $contact->lastname = 'Flex';
  343. $contact->email = 'was@here.com';
  344. $contact->mobile = '123-456-7890';
  345. unset($contact->_explicitType);
  346. array_push( $data, $contact );
  347. // Create an acknowlege message for a response to a RemotingMessage
  348. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  349. $acknowledgeMessage->correlationId = 'C44AE645-4D12-028B-FF5F-D2E42BE5D86C';
  350. $acknowledgeMessage->clientId = '40EAAAD2-4A9B-C388-A2FD-00003A809B9E';
  351. $acknowledgeMessage->messageId = '275CD08C-6461-BBC8-B27B-000030083B2C';
  352. $acknowledgeMessage->destination = null;
  353. $acknowledgeMessage->timeToLive = 0;
  354. $acknowledgeMessage->timestamp = '122330856000';
  355. $acknowledgeMessage->body = $data;
  356. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  357. // serialize the data to an AMF output stream
  358. $this->_response->setObjectEncoding(0x03);
  359. $this->_response->addAmfBody($newBody);
  360. $this->_response->finalize();
  361. $testResponse = $this->_response->getResponse();
  362. // Load the expected response.
  363. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/classMapAmf3Response.bin');
  364. // Check that the response matches the expected serialized value
  365. $this->assertEquals($mockResponse, $testResponse);
  366. }
  367. /**
  368. * Returning a DOMDocument object to AMF is serialized into a XMString ready for E4X
  369. *
  370. * @group ZF-4999
  371. */
  372. public function testPhpDomDocumentSerializedToAmf3XmlString()
  373. {
  374. $sXML = '<root><element><key>a</key><value>b</value></element></root>';
  375. $data = new DOMDocument();
  376. $data->preserveWhiteSpace = false;
  377. $data->loadXML($sXML);
  378. // Create an acknowlege message for a response to a RemotingMessage
  379. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  380. $acknowledgeMessage->correlationId = 'B0B0E583-5A80-826B-C2D1-D67A63D2F5E1';
  381. $acknowledgeMessage->clientId = '3D281DFB-FAC8-E368-3267-0000696DA53F';
  382. $acknowledgeMessage->messageId = '436381AA-C8C1-9749-2B05-000067CEA2CD';
  383. $acknowledgeMessage->destination = null;
  384. $acknowledgeMessage->timeToLive = 0;
  385. $acknowledgeMessage->timestamp = '122766401600';
  386. $acknowledgeMessage->body = $data;
  387. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  388. // serialize the data to an AMF output stream
  389. $this->_response->setObjectEncoding(0x03);
  390. $this->_response->addAmfBody($newBody);
  391. $this->_response->finalize();
  392. $testResponse = $this->_response->getResponse();
  393. // Load the expected response.
  394. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/domdocumentAmf3Response.bin');
  395. // Check that the response matches the expected serialized value
  396. $this->assertEquals($mockResponse, $testResponse);
  397. }
  398. /**
  399. * Returning a SimpleXML object to AMF is serialized into a XMString ready for E4X
  400. *
  401. * @group ZF-4999
  402. */
  403. public function testSimpleXmlSerializedToAmf3XmlString()
  404. {
  405. $sXML = '<root><element><key>a</key><value>b</value></element></root>';
  406. $data = new DOMDocument();
  407. $data->preserveWhiteSpace = false;
  408. $data->loadXML($sXML);
  409. $data = simplexml_import_dom($data);
  410. // Create an acknowlege message for a response to a RemotingMessage
  411. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  412. $acknowledgeMessage->correlationId = 'B0B0E583-5A80-826B-C2D1-D67A63D2F5E1';
  413. $acknowledgeMessage->clientId = '3D281DFB-FAC8-E368-3267-0000696DA53F';
  414. $acknowledgeMessage->messageId = '436381AA-C8C1-9749-2B05-000067CEA2CD';
  415. $acknowledgeMessage->destination = null;
  416. $acknowledgeMessage->timeToLive = 0;
  417. $acknowledgeMessage->timestamp = '122766401600';
  418. $acknowledgeMessage->body = $data;
  419. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  420. // serialize the data to an AMF output stream
  421. $this->_response->setObjectEncoding(0x03);
  422. $this->_response->addAmfBody($newBody);
  423. $this->_response->finalize();
  424. $testResponse = $this->_response->getResponse();
  425. // Load the expected response.
  426. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/domdocumentAmf3Response.bin');
  427. // Check that the response matches the expected serialized value
  428. $this->assertEquals($mockResponse, $testResponse);
  429. }
  430. /**
  431. * PHP string to Amf0 string
  432. *
  433. */
  434. public function testPhpStringSerializedToAmf0String()
  435. {
  436. $data = "zyxwvutsrqpmlkjihgfedcba";
  437. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  438. $this->_response->setObjectEncoding(0x00);
  439. $this->_response->addAmfBody($newBody);
  440. $this->_response->finalize();
  441. $testResponse = $this->_response->getResponse();
  442. // Load the expected response.
  443. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringAmf0Response.bin');
  444. // Check that the response matches the expected serialized value
  445. $this->assertEquals($mockResponse, $testResponse);
  446. }
  447. /**
  448. * PHP Array to Amf0 Array
  449. *
  450. */
  451. public function testPhpArraySerializedToAmf0Array()
  452. {
  453. $data = array("g", "f", "e","d","c","b","a");
  454. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  455. $this->_response->setObjectEncoding(0x00);
  456. $this->_response->addAmfBody($newBody);
  457. $this->_response->finalize();
  458. $testResponse = $this->_response->getResponse();
  459. // Load the expected response.
  460. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/arrayAmf0Response.bin');
  461. // Check that the response matches the expected serialized value
  462. $this->assertEquals($mockResponse, $testResponse);
  463. }
  464. /**
  465. * Check to make sure that we can place arrays in arrays.
  466. *
  467. * @group ZF-4712
  468. */
  469. public function testPhpNestedArraySerializedToAmf0Array()
  470. {
  471. $data = array("items"=>array("a","b"));
  472. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  473. $this->_response->setObjectEncoding(0x00);
  474. $this->_response->addAmfBody($newBody);
  475. $this->_response->finalize();
  476. $testResponse = $this->_response->getResponse();
  477. // Load the expected response.
  478. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nestedArrayAmf0Response.bin');
  479. // Check that the response matches the expected serialized value
  480. $this->assertEquals($mockResponse, $testResponse);
  481. }
  482. /**
  483. * Allow sparse arrays to be retruned to Actionscript without loosing the keys.
  484. *
  485. * @group ZF-5094
  486. */
  487. public function testPhpSparseArraySerializedToAmf0Array()
  488. {
  489. $data = array(1 => 'foo', 5 => 'bar');
  490. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  491. $this->_response->setObjectEncoding(0x00);
  492. $this->_response->addAmfBody($newBody);
  493. $this->_response->finalize();
  494. $testResponse = $this->_response->getResponse();
  495. // Load the expected response.
  496. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/sparseArrayAmf0Response.bin');
  497. // Check that the response matches the expected serialized value
  498. $this->assertEquals($mockResponse, $testResponse);
  499. }
  500. /**
  501. * Test to convert string keyed arrays are converted to objects so that we do not loose
  502. * the key refrence in the associative array.
  503. *
  504. * @group ZF-5094
  505. */
  506. public function testPhpStringKeyArrayToAmf0Object()
  507. {
  508. $data = array('foo' => 5, 'bar' => 23);
  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/stringKeyArrayAmf0Response.bin');
  516. // Check that the response matches the expected serialized value
  517. $this->assertEquals($mockResponse, $testResponse);
  518. }
  519. /**
  520. * PHP Object to Amf0 Object
  521. *
  522. */
  523. public function testPhpObjectSerializedToAmf0Object()
  524. {
  525. $data = array('b'=>'bar',"a" =>'foo');
  526. $data = (object) $data;
  527. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  528. $this->_response->setObjectEncoding(0x00);
  529. $this->_response->addAmfBody($newBody);
  530. $this->_response->finalize();
  531. $testResponse = $this->_response->getResponse();
  532. // Load the expected response.
  533. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/objectAmf0Response.bin');
  534. // Check that the response matches the expected serialized value
  535. $this->assertEquals($mockResponse, $testResponse);
  536. }
  537. public function testPhpObjectSerializedToAmf0TypedObjectClassMap()
  538. {
  539. Zend_Amf_Parse_TypeLoader::setMapping("ContactVO","Contact");
  540. $data = array();
  541. $contact = new Contact();
  542. $contact->id = '15';
  543. $contact->firstname = 'Joe';
  544. $contact->lastname = 'Smith';
  545. $contact->email = 'jsmith@adobe.com';
  546. $contact->mobile = '123-456-7890';
  547. unset($contact->_explicitType);
  548. array_push( $data, $contact );
  549. $contact = new Contact();
  550. $contact->id = '23';
  551. $contact->firstname = 'Adobe';
  552. $contact->lastname = 'Flex';
  553. $contact->email = 'was@here.com';
  554. $contact->mobile = '123-456-7890';
  555. unset($contact->_explicitType);
  556. array_push( $data, $contact );
  557. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  558. $this->_response->setObjectEncoding(0x00);
  559. $this->_response->addAmfBody($newBody);
  560. $this->_response->finalize();
  561. $testResponse = $this->_response->getResponse();
  562. // Load the expected response.
  563. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  564. // Check that the response matches the expected serialized value
  565. $this->assertEquals($mockResponse, $testResponse);
  566. }
  567. public function testPhpObjectSerializedToAmf0TypedObjectExplicitType()
  568. {
  569. $data = array();
  570. $contact = new Contact();
  571. $contact->id = '15';
  572. $contact->firstname = 'Joe';
  573. $contact->lastname = 'Smith';
  574. $contact->email = 'jsmith@adobe.com';
  575. $contact->mobile = '123-456-7890';
  576. array_push( $data, $contact );
  577. $contact = new Contact();
  578. $contact->id = '23';
  579. $contact->firstname = 'Adobe';
  580. $contact->lastname = 'Flex';
  581. $contact->email = 'was@here.com';
  582. $contact->mobile = '123-456-7890';
  583. array_push( $data, $contact );
  584. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  585. $this->_response->setObjectEncoding(0x00);
  586. $this->_response->addAmfBody($newBody);
  587. $this->_response->finalize();
  588. $testResponse = $this->_response->getResponse();
  589. // Load the expected response.
  590. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  591. // Check that the response matches the expected serialized value
  592. $this->assertEquals($mockResponse, $testResponse);
  593. }
  594. public function testPhpObjectSerializedToAmf0TypedObjectGetAsClassName()
  595. {
  596. $data = array();
  597. $contact = new Contact();
  598. $contact->id = '15';
  599. $contact->firstname = 'Joe';
  600. $contact->lastname = 'Smith';
  601. $contact->email = 'jsmith@adobe.com';
  602. $contact->mobile = '123-456-7890';
  603. unset($contact->_explicitType);
  604. array_push( $data, $contact );
  605. $contact = new Contact();
  606. $contact->id = '23';
  607. $contact->firstname = 'Adobe';
  608. $contact->lastname = 'Flex';
  609. $contact->email = 'was@here.com';
  610. $contact->mobile = '123-456-7890';
  611. unset($contact->_explicitType);
  612. array_push( $data, $contact );
  613. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  614. $this->_response->setObjectEncoding(0x00);
  615. $this->_response->addAmfBody($newBody);
  616. $this->_response->finalize();
  617. $testResponse = $this->_response->getResponse();
  618. // Load the expected response.
  619. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  620. // Check that the response matches the expected serialized value
  621. $this->assertEquals($mockResponse, $testResponse);
  622. }
  623. /**
  624. * PHP float to Amf0 Number
  625. *
  626. */
  627. public function testPhpFloatSerializedToAmf0Number()
  628. {
  629. $data = 31.57;
  630. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  631. $this->_response->setObjectEncoding(0x00);
  632. $this->_response->addAmfBody($newBody);
  633. $this->_response->finalize();
  634. $testResponse = $this->_response->getResponse();
  635. // Load the expected response.
  636. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/numberAmf0Response.bin');
  637. // Check that the response matches the expected serialized value
  638. $this->assertEquals($mockResponse, $testResponse);
  639. }
  640. /**
  641. * PHP DateTime to Amf0 date
  642. *
  643. */
  644. public function testPhpDateTimeSerializedToAmf0Date()
  645. {
  646. date_default_timezone_set('America/Chicago');
  647. $dateSrc = '1978-10-23 4:20 America/Chicago';
  648. $date = new DateTime($dateSrc, new DateTimeZone('America/Chicago'));
  649. $data = $date;
  650. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  651. $this->_response->setObjectEncoding(0x00);
  652. $this->_response->addAmfBody($newBody);
  653. $this->_response->finalize();
  654. $testResponse = $this->_response->getResponse();
  655. // Load the expected response.
  656. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  657. // Check that the response matches the expected serialized value
  658. $this->assertEquals($mockResponse, $testResponse);
  659. }
  660. public function testZendDateSerializedToAmf0Date()
  661. {
  662. $date = new Zend_Date('October 23, 1978', null, 'en_US');
  663. $date->set('4:20:00',Zend_Date::TIMES);
  664. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$date);
  665. $this->_response->setObjectEncoding(0x00);
  666. $this->_response->addAmfBody($newBody);
  667. $this->_response->finalize();
  668. $testResponse = $this->_response->getResponse();
  669. // Load the expected response.
  670. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  671. // Check that the response matches the expected serialized value
  672. $this->assertEquals($mockResponse, $testResponse);
  673. }
  674. /**
  675. * PHP boolean true to Amf0 bool true.
  676. *
  677. */
  678. public function testPhpBoolTrueSerializedToAmf0Bool()
  679. {
  680. $data = true;
  681. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  682. $this->_response->setObjectEncoding(0x00);
  683. $this->_response->addAmfBody($newBody);
  684. $this->_response->finalize();
  685. $testResponse = $this->_response->getResponse();
  686. // Load the expected response.
  687. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolTrueAmf0Response.bin');
  688. // Check that the response matches the expected serialized value
  689. $this->assertEquals($mockResponse, $testResponse);
  690. }
  691. /**
  692. * PHP boolean true to Amf0 bool true.
  693. *
  694. */
  695. public function testPhpBoolFalseSerializedToAmf0Bool()
  696. {
  697. $data = false;
  698. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  699. $this->_response->setObjectEncoding(0x00);
  700. $this->_response->addAmfBody($newBody);
  701. $this->_response->finalize();
  702. $testResponse = $this->_response->getResponse();
  703. // Load the expected response.
  704. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolFalseAmf0Response.bin');
  705. // Check that the response matches the expected serialized value
  706. $this->assertEquals($mockResponse, $testResponse);
  707. }
  708. public function testPHPNullSerializedToAmf0Null()
  709. {
  710. $data = null;
  711. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  712. $this->_response->setObjectEncoding(0x00);
  713. $this->_response->addAmfBody($newBody);
  714. $this->_response->finalize();
  715. $testResponse = $this->_response->getResponse();
  716. // Load the expected response.
  717. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nullAmf0Response.bin');
  718. // Check that the response matches the expected serialized value
  719. $this->assertEquals($mockResponse, $testResponse);
  720. }
  721. public function testResponseShouldNotHaveMessageHeadersByDefault()
  722. {
  723. $headers = $this->_response->getAmfHeaders();
  724. $this->assertEquals(0, count($headers));
  725. }
  726. public function testResponseShouldAggregateMessageHeaders()
  727. {
  728. $this->header1 = new Zend_Amf_Value_MessageHeader('foo', false, 'bar');
  729. $this->header2 = new Zend_Amf_Value_MessageHeader('bar', true, 'baz');
  730. $this->_response->addAmfHeader($this->header1)
  731. ->addAmfHeader($this->header2);
  732. $headers = $this->_response->getAmfHeaders();
  733. $this->assertEquals(2, count($headers));
  734. $this->assertContains($this->header1, $headers);
  735. $this->assertContains($this->header2, $headers);
  736. }
  737. public function testResponseHeadersShouldBeSerializedWhenWritingMessage()
  738. {
  739. $this->testResponseShouldAggregateMessageHeaders();
  740. $this->_response->finalize();
  741. $response = $this->_response->getResponse();
  742. $request = new Zend_Amf_Request();
  743. $request->initialize($response);
  744. $headers = $request->getAmfHeaders();
  745. $this->assertEquals(2, count($headers));
  746. }
  747. public function testToStringShouldProxyToGetResponse()
  748. {
  749. $this->testResponseShouldAggregateMessageHeaders();
  750. $this->_response->finalize();
  751. $response = $this->_response->getResponse();
  752. $test = $this->_response->__toString();
  753. $this->assertSame($response, $test);
  754. }
  755. }
  756. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_ResponseTest::main') {
  757. Zend_Amf_ResponseTest::main();
  758. }