ResponseTest.php 40 KB

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