ResponseTest.php 41 KB

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