ResponseTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. * Returning a DOMDocument object to AMF is serialized into a XMString ready for E4X
  370. *
  371. * @group ZF-4999
  372. */
  373. public function testPhpDomDocumentSerializedToAmf3XmlString()
  374. {
  375. $sXML = '<root><element><key>a</key><value>b</value></element></root>';
  376. $data = new DOMDocument();
  377. $data->preserveWhiteSpace = false;
  378. $data->loadXML($sXML);
  379. // Create an acknowlege message for a response to a RemotingMessage
  380. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  381. $acknowledgeMessage->correlationId = 'B0B0E583-5A80-826B-C2D1-D67A63D2F5E1';
  382. $acknowledgeMessage->clientId = '3D281DFB-FAC8-E368-3267-0000696DA53F';
  383. $acknowledgeMessage->messageId = '436381AA-C8C1-9749-2B05-000067CEA2CD';
  384. $acknowledgeMessage->destination = null;
  385. $acknowledgeMessage->timeToLive = 0;
  386. $acknowledgeMessage->timestamp = '122766401600';
  387. $acknowledgeMessage->body = $data;
  388. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  389. // serialize the data to an AMF output stream
  390. $this->_response->setObjectEncoding(0x03);
  391. $this->_response->addAmfBody($newBody);
  392. $this->_response->finalize();
  393. $testResponse = $this->_response->getResponse();
  394. // Load the expected response.
  395. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/domdocumentAmf3Response.bin');
  396. // Check that the response matches the expected serialized value
  397. $this->assertEquals($mockResponse, $testResponse);
  398. }
  399. /**
  400. * Returning a SimpleXML object to AMF is serialized into a XMString ready for E4X
  401. *
  402. * @group ZF-4999
  403. */
  404. public function testSimpleXmlSerializedToAmf3XmlString()
  405. {
  406. $sXML = '<root><element><key>a</key><value>b</value></element></root>';
  407. $data = new DOMDocument();
  408. $data->preserveWhiteSpace = false;
  409. $data->loadXML($sXML);
  410. $data = simplexml_import_dom($data);
  411. // Create an acknowlege message for a response to a RemotingMessage
  412. $acknowledgeMessage = new Zend_Amf_Value_Messaging_AcknowledgeMessage(null);
  413. $acknowledgeMessage->correlationId = 'B0B0E583-5A80-826B-C2D1-D67A63D2F5E1';
  414. $acknowledgeMessage->clientId = '3D281DFB-FAC8-E368-3267-0000696DA53F';
  415. $acknowledgeMessage->messageId = '436381AA-C8C1-9749-2B05-000067CEA2CD';
  416. $acknowledgeMessage->destination = null;
  417. $acknowledgeMessage->timeToLive = 0;
  418. $acknowledgeMessage->timestamp = '122766401600';
  419. $acknowledgeMessage->body = $data;
  420. $newBody = new Zend_Amf_Value_MessageBody($this->responseURI,null,$acknowledgeMessage);
  421. // serialize the data to an AMF output stream
  422. $this->_response->setObjectEncoding(0x03);
  423. $this->_response->addAmfBody($newBody);
  424. $this->_response->finalize();
  425. $testResponse = $this->_response->getResponse();
  426. // Load the expected response.
  427. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/domdocumentAmf3Response.bin');
  428. // Check that the response matches the expected serialized value
  429. $this->assertEquals($mockResponse, $testResponse);
  430. }
  431. /**
  432. * PHP string to Amf0 string
  433. *
  434. */
  435. public function testPhpStringSerializedToAmf0String()
  436. {
  437. $data = "zyxwvutsrqpmlkjihgfedcba";
  438. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  439. $this->_response->setObjectEncoding(0x00);
  440. $this->_response->addAmfBody($newBody);
  441. $this->_response->finalize();
  442. $testResponse = $this->_response->getResponse();
  443. // Load the expected response.
  444. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringAmf0Response.bin');
  445. // Check that the response matches the expected serialized value
  446. $this->assertEquals($mockResponse, $testResponse);
  447. }
  448. /**
  449. * PHP Array to Amf0 Array
  450. *
  451. */
  452. public function testPhpArraySerializedToAmf0Array()
  453. {
  454. $data = array("g", "f", "e","d","c","b","a");
  455. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  456. $this->_response->setObjectEncoding(0x00);
  457. $this->_response->addAmfBody($newBody);
  458. $this->_response->finalize();
  459. $testResponse = $this->_response->getResponse();
  460. // Load the expected response.
  461. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/arrayAmf0Response.bin');
  462. // Check that the response matches the expected serialized value
  463. $this->assertEquals($mockResponse, $testResponse);
  464. }
  465. /**
  466. * Check to make sure that we can place arrays in arrays.
  467. *
  468. * @group ZF-4712
  469. */
  470. public function testPhpNestedArraySerializedToAmf0Array()
  471. {
  472. $data = array("items"=>array("a","b"));
  473. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  474. $this->_response->setObjectEncoding(0x00);
  475. $this->_response->addAmfBody($newBody);
  476. $this->_response->finalize();
  477. $testResponse = $this->_response->getResponse();
  478. // Load the expected response.
  479. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nestedArrayAmf0Response.bin');
  480. // Check that the response matches the expected serialized value
  481. $this->assertEquals($mockResponse, $testResponse);
  482. }
  483. /**
  484. * Allow sparse arrays to be retruned to Actionscript without loosing the keys.
  485. *
  486. * @group ZF-5094
  487. */
  488. public function testPhpSparseArraySerializedToAmf0Array()
  489. {
  490. $data = array(1 => 'foo', 5 => 'bar');
  491. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  492. $this->_response->setObjectEncoding(0x00);
  493. $this->_response->addAmfBody($newBody);
  494. $this->_response->finalize();
  495. $testResponse = $this->_response->getResponse();
  496. // Load the expected response.
  497. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/sparseArrayAmf0Response.bin');
  498. // Check that the response matches the expected serialized value
  499. $this->assertEquals($mockResponse, $testResponse);
  500. }
  501. /**
  502. * Test to convert string keyed arrays are converted to objects so that we do not loose
  503. * the key refrence in the associative array.
  504. *
  505. * @group ZF-5094
  506. */
  507. public function testPhpStringKeyArrayToAmf0Object()
  508. {
  509. $data = array('foo' => 5, 'bar' => 23);
  510. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  511. $this->_response->setObjectEncoding(0x00);
  512. $this->_response->addAmfBody($newBody);
  513. $this->_response->finalize();
  514. $testResponse = $this->_response->getResponse();
  515. // Load the expected response.
  516. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/stringKeyArrayAmf0Response.bin');
  517. // Check that the response matches the expected serialized value
  518. $this->assertEquals($mockResponse, $testResponse);
  519. }
  520. /**
  521. * PHP Object to Amf0 Object
  522. *
  523. */
  524. public function testPhpObjectSerializedToAmf0Object()
  525. {
  526. $data = array('b'=>'bar',"a" =>'foo');
  527. $data = (object) $data;
  528. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  529. $this->_response->setObjectEncoding(0x00);
  530. $this->_response->addAmfBody($newBody);
  531. $this->_response->finalize();
  532. $testResponse = $this->_response->getResponse();
  533. // Load the expected response.
  534. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/objectAmf0Response.bin');
  535. // Check that the response matches the expected serialized value
  536. $this->assertEquals($mockResponse, $testResponse);
  537. }
  538. public function testPhpObjectSerializedToAmf0TypedObjectClassMap()
  539. {
  540. Zend_Amf_Parse_TypeLoader::setMapping("ContactVO","Contact");
  541. $data = array();
  542. $contact = new Contact();
  543. $contact->id = '15';
  544. $contact->firstname = 'Joe';
  545. $contact->lastname = 'Smith';
  546. $contact->email = 'jsmith@adobe.com';
  547. $contact->mobile = '123-456-7890';
  548. unset($contact->_explicitType);
  549. array_push( $data, $contact );
  550. $contact = new Contact();
  551. $contact->id = '23';
  552. $contact->firstname = 'Adobe';
  553. $contact->lastname = 'Flex';
  554. $contact->email = 'was@here.com';
  555. $contact->mobile = '123-456-7890';
  556. unset($contact->_explicitType);
  557. array_push( $data, $contact );
  558. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  559. $this->_response->setObjectEncoding(0x00);
  560. $this->_response->addAmfBody($newBody);
  561. $this->_response->finalize();
  562. $testResponse = $this->_response->getResponse();
  563. // Load the expected response.
  564. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  565. // Check that the response matches the expected serialized value
  566. $this->assertEquals($mockResponse, $testResponse);
  567. }
  568. public function testPhpObjectSerializedToAmf0TypedObjectExplicitType()
  569. {
  570. $data = array();
  571. $contact = new Contact();
  572. $contact->id = '15';
  573. $contact->firstname = 'Joe';
  574. $contact->lastname = 'Smith';
  575. $contact->email = 'jsmith@adobe.com';
  576. $contact->mobile = '123-456-7890';
  577. array_push( $data, $contact );
  578. $contact = new Contact();
  579. $contact->id = '23';
  580. $contact->firstname = 'Adobe';
  581. $contact->lastname = 'Flex';
  582. $contact->email = 'was@here.com';
  583. $contact->mobile = '123-456-7890';
  584. array_push( $data, $contact );
  585. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  586. $this->_response->setObjectEncoding(0x00);
  587. $this->_response->addAmfBody($newBody);
  588. $this->_response->finalize();
  589. $testResponse = $this->_response->getResponse();
  590. // Load the expected response.
  591. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  592. // Check that the response matches the expected serialized value
  593. $this->assertEquals($mockResponse, $testResponse);
  594. }
  595. public function testPhpObjectSerializedToAmf0TypedObjectGetAsClassName()
  596. {
  597. $data = array();
  598. $contact = new Contact();
  599. $contact->id = '15';
  600. $contact->firstname = 'Joe';
  601. $contact->lastname = 'Smith';
  602. $contact->email = 'jsmith@adobe.com';
  603. $contact->mobile = '123-456-7890';
  604. unset($contact->_explicitType);
  605. array_push( $data, $contact );
  606. $contact = new Contact();
  607. $contact->id = '23';
  608. $contact->firstname = 'Adobe';
  609. $contact->lastname = 'Flex';
  610. $contact->email = 'was@here.com';
  611. $contact->mobile = '123-456-7890';
  612. unset($contact->_explicitType);
  613. array_push( $data, $contact );
  614. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  615. $this->_response->setObjectEncoding(0x00);
  616. $this->_response->addAmfBody($newBody);
  617. $this->_response->finalize();
  618. $testResponse = $this->_response->getResponse();
  619. // Load the expected response.
  620. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  621. // Check that the response matches the expected serialized value
  622. $this->assertEquals($mockResponse, $testResponse);
  623. }
  624. /**
  625. * The feature test allows for php to just retun it's class name if nothing is specified. Using
  626. * _explicitType, setClassMap, getASClassName() should only be used now if you want to override the
  627. * PHP class name for specifying the return type.
  628. * @group ZF-6130
  629. */
  630. public function testPhpObjectNameSerializedToAmf0ClassName()
  631. {
  632. $data = array();
  633. $contact = new ContactVO();
  634. $contact->id = '15';
  635. $contact->firstname = 'Joe';
  636. $contact->lastname = 'Smith';
  637. $contact->email = 'jsmith@adobe.com';
  638. $contact->mobile = '123-456-7890';
  639. array_push( $data, $contact );
  640. $contact = new ContactVO();
  641. $contact->id = '23';
  642. $contact->firstname = 'Adobe';
  643. $contact->lastname = 'Flex';
  644. $contact->email = 'was@here.com';
  645. $contact->mobile = '123-456-7890';
  646. array_push( $data, $contact );
  647. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  648. $this->_response->setObjectEncoding(0x00);
  649. $this->_response->addAmfBody($newBody);
  650. $this->_response->finalize();
  651. $testResponse = $this->_response->getResponse();
  652. // Load the expected response.
  653. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/typedObjectAmf0Response.bin');
  654. // Check that the response matches the expected serialized value
  655. $this->assertEquals($mockResponse, $testResponse);
  656. }
  657. /**
  658. * PHP float to Amf0 Number
  659. *
  660. */
  661. public function testPhpFloatSerializedToAmf0Number()
  662. {
  663. $data = 31.57;
  664. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  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/numberAmf0Response.bin');
  671. // Check that the response matches the expected serialized value
  672. $this->assertEquals($mockResponse, $testResponse);
  673. }
  674. /**
  675. * PHP DateTime to Amf0 date
  676. *
  677. */
  678. public function testPhpDateTimeSerializedToAmf0Date()
  679. {
  680. date_default_timezone_set('America/Chicago');
  681. $dateSrc = '1978-10-23 4:20 America/Chicago';
  682. $date = new DateTime($dateSrc, new DateTimeZone('America/Chicago'));
  683. $data = $date;
  684. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  685. $this->_response->setObjectEncoding(0x00);
  686. $this->_response->addAmfBody($newBody);
  687. $this->_response->finalize();
  688. $testResponse = $this->_response->getResponse();
  689. // Load the expected response.
  690. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/dateAmf0Response.bin');
  691. // Check that the response matches the expected serialized value
  692. $this->assertEquals($mockResponse, $testResponse);
  693. }
  694. public function testZendDateSerializedToAmf0Date()
  695. {
  696. $date = new Zend_Date('October 23, 1978', null, 'en_US');
  697. $date->set('4:20:00',Zend_Date::TIMES);
  698. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$date);
  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/dateAmf0Response.bin');
  705. // Check that the response matches the expected serialized value
  706. $this->assertEquals($mockResponse, $testResponse);
  707. }
  708. /**
  709. * PHP boolean true to Amf0 bool true.
  710. *
  711. */
  712. public function testPhpBoolTrueSerializedToAmf0Bool()
  713. {
  714. $data = true;
  715. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  716. $this->_response->setObjectEncoding(0x00);
  717. $this->_response->addAmfBody($newBody);
  718. $this->_response->finalize();
  719. $testResponse = $this->_response->getResponse();
  720. // Load the expected response.
  721. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolTrueAmf0Response.bin');
  722. // Check that the response matches the expected serialized value
  723. $this->assertEquals($mockResponse, $testResponse);
  724. }
  725. /**
  726. * PHP boolean true to Amf0 bool true.
  727. *
  728. */
  729. public function testPhpBoolFalseSerializedToAmf0Bool()
  730. {
  731. $data = false;
  732. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  733. $this->_response->setObjectEncoding(0x00);
  734. $this->_response->addAmfBody($newBody);
  735. $this->_response->finalize();
  736. $testResponse = $this->_response->getResponse();
  737. // Load the expected response.
  738. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/boolFalseAmf0Response.bin');
  739. // Check that the response matches the expected serialized value
  740. $this->assertEquals($mockResponse, $testResponse);
  741. }
  742. public function testPHPNullSerializedToAmf0Null()
  743. {
  744. $data = null;
  745. $newBody = new Zend_Amf_Value_MessageBody('/1/onResult',null,$data);
  746. $this->_response->setObjectEncoding(0x00);
  747. $this->_response->addAmfBody($newBody);
  748. $this->_response->finalize();
  749. $testResponse = $this->_response->getResponse();
  750. // Load the expected response.
  751. $mockResponse = file_get_contents(dirname(__FILE__) .'/Response/mock/nullAmf0Response.bin');
  752. // Check that the response matches the expected serialized value
  753. $this->assertEquals($mockResponse, $testResponse);
  754. }
  755. public function testResponseShouldNotHaveMessageHeadersByDefault()
  756. {
  757. $headers = $this->_response->getAmfHeaders();
  758. $this->assertEquals(0, count($headers));
  759. }
  760. public function testResponseShouldAggregateMessageHeaders()
  761. {
  762. $this->header1 = new Zend_Amf_Value_MessageHeader('foo', false, 'bar');
  763. $this->header2 = new Zend_Amf_Value_MessageHeader('bar', true, 'baz');
  764. $this->_response->addAmfHeader($this->header1)
  765. ->addAmfHeader($this->header2);
  766. $headers = $this->_response->getAmfHeaders();
  767. $this->assertEquals(2, count($headers));
  768. $this->assertContains($this->header1, $headers);
  769. $this->assertContains($this->header2, $headers);
  770. }
  771. public function testResponseHeadersShouldBeSerializedWhenWritingMessage()
  772. {
  773. $this->testResponseShouldAggregateMessageHeaders();
  774. $this->_response->finalize();
  775. $response = $this->_response->getResponse();
  776. $request = new Zend_Amf_Request();
  777. $request->initialize($response);
  778. $headers = $request->getAmfHeaders();
  779. $this->assertEquals(2, count($headers));
  780. }
  781. public function testToStringShouldProxyToGetResponse()
  782. {
  783. $this->testResponseShouldAggregateMessageHeaders();
  784. $this->_response->finalize();
  785. $response = $this->_response->getResponse();
  786. $test = $this->_response->__toString();
  787. $this->assertSame($response, $test);
  788. }
  789. }
  790. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_ResponseTest::main') {
  791. Zend_Amf_ResponseTest::main();
  792. }