RequestTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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_RequestTest::main');
  24. }
  25. require_once 'Zend/Amf/Request.php';
  26. require_once 'Zend/Amf/Parse/TypeLoader.php';
  27. require_once 'Zend/Locale.php';
  28. require_once 'Contact.php';
  29. /**
  30. * Test case for Zend_Amf_Request
  31. *
  32. * @category Zend
  33. * @package Zend_Amf
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Amf
  38. */
  39. class Zend_Amf_RequestTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Zend_Amf_Request object
  43. * @var Zend_Amf_Request
  44. */
  45. protected $_request;
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @return void
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_RequestTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Setup environment
  58. */
  59. public function setUp()
  60. {
  61. date_default_timezone_set("America/Chicago");
  62. Zend_Locale::setDefault('en');
  63. Zend_Amf_Parse_TypeLoader::resetMap();
  64. $this->_request = new Zend_Amf_Request();
  65. }
  66. /**
  67. * Teardown environment
  68. */
  69. public function tearDown()
  70. {
  71. unset($this->_request);
  72. }
  73. /**
  74. * ActionScript undef to PHP null
  75. *
  76. */
  77. public function testAmf3RemoteObjectUndefParameterDeserializedToNativePhpNull()
  78. {
  79. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/undefAmf3Request.bin');
  80. // send the mock object request to be deserialized
  81. $this->_request->initialize($myRequest);
  82. // Make sure the encoding type is properly set.
  83. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  84. // Make sure that no headers where recievedpbs
  85. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  86. // Make sure that the message body was set after deserialization
  87. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  88. $bodies = $this->_request->getAmfBodies();
  89. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  90. $message = $bodies[0]->getData();
  91. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  92. // Make sure that our endpoint is properly set.
  93. $this->assertEquals('returnUndefined', $message->operation);
  94. $this->assertEquals('RoundTrip', $message->source);
  95. $data = $message->body;
  96. // Make sure that we are dealing with a PHP null
  97. $this->assertTrue(is_null($data[0]));
  98. }
  99. /**
  100. * ActionScript String to PHP String
  101. *
  102. */
  103. public function testAmf3RemoteObjectStringParameterDeserializedToNativePhpString()
  104. {
  105. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/stringAmf3Request.bin');
  106. // send the mock object request to be deserialized
  107. $this->_request->initialize($myRequest);
  108. // Make sure the encoding type is properly set.
  109. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  110. // Make sure that no headers where recieved
  111. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  112. // Make sure that the message body was set after deserialization
  113. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  114. $bodies = $this->_request->getAmfBodies();
  115. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  116. $message = $bodies[0]->getData();
  117. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  118. // Make sure that our endpoint is properly set.
  119. $this->assertEquals('returnString', $message->operation);
  120. $this->assertEquals('RoundTrip', $message->source);
  121. $data = $message->body;
  122. // Make sure that we are dealing with a PHP string
  123. $this->assertTrue(is_string($data[0]));
  124. // Make sure that the string was deserialized properly and check its value
  125. $this->assertEquals('abcdefghijklmpqrstuvwxyz', $data[0]);
  126. }
  127. /**
  128. * ActionScript Array to Php Array
  129. *
  130. */
  131. public function testAmf3RemoteObjectArrayParameterDeserializedToNativePhpArray()
  132. {
  133. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/arrayAmf3Request.bin');
  134. // send the mock object request to be deserialized
  135. $this->_request->initialize($myRequest);
  136. // Make sure the encoding type is properly set.
  137. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  138. // Make sure that no headers where recieved
  139. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  140. // Make sure that the message body was set after deserialization
  141. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  142. $bodies = $this->_request->getAmfBodies();
  143. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  144. $message = $bodies[0]->getData();
  145. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  146. // Make sure that our endpoint is properly set.
  147. $this->assertEquals('returnArray', $message->operation);
  148. $this->assertEquals('RoundTrip', $message->source);
  149. $data = $message->body;
  150. // Make sure that we are dealing with a PHP array
  151. $this->assertTrue(is_array($data[0]));
  152. // Make sure that the array was deserialized properly and check its value
  153. $this->assertEquals('a', $data[0][0]);
  154. $this->assertEquals('g', $data[0][6]);
  155. }
  156. /**
  157. * ActionScript Numnber to PHP float
  158. *
  159. */
  160. public function testAmf3NumberParameterDeserializedToNativePhpFloat()
  161. {
  162. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/numberAmf3Request.bin');
  163. // send the mock object request to be deserialized
  164. $this->_request->initialize($myRequest);
  165. // Make sure the encoding type is properly set.
  166. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  167. // Make sure that no headers where recieved
  168. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  169. // Make sure that the message body was set after deserialization
  170. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  171. $bodies = $this->_request->getAmfBodies();
  172. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  173. $message = $bodies[0]->getData();
  174. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  175. // Make sure that our endpoint is properly set.
  176. $this->assertEquals('returnNumber', $message->operation);
  177. $this->assertEquals('RoundTrip', $message->source);
  178. $data = $message->body;
  179. // Make sure that we are dealing with a PHP float
  180. $this->assertTrue(is_float($data[0]));
  181. // Make sure that the float was deserialized properly and check its value
  182. $this->assertEquals(31.57, $data[0]);
  183. }
  184. /**
  185. * ActionScript Date to Php DateTime
  186. *
  187. */
  188. public function testAmf3DateParameterDeserializedToNativeDateTime()
  189. {
  190. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/dateAmf3Request.bin');
  191. // send the mock object request to be deserialized
  192. $this->_request->initialize($myRequest);
  193. // Make sure the encoding type is properly set.
  194. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  195. // Make sure that no headers where recieved
  196. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  197. // Make sure that the message body was set after deserialization
  198. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  199. $bodies = $this->_request->getAmfBodies();
  200. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  201. $message = $bodies[0]->getData();
  202. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  203. // Make sure that our endpoint is properly set.
  204. $this->assertEquals('returnDate', $message->operation);
  205. $this->assertEquals('RoundTrip', $message->source);
  206. $data = $message->body;
  207. // Make sure that the array was deserialized properly and check its value
  208. $this->assertEquals(1978, $data[0]->toString('Y'));
  209. }
  210. /**
  211. * Try and read in the largest Amf Integer to PHP int
  212. *
  213. */
  214. public function testAmf3LargeIntParameterDeserializedToNativePhpInt()
  215. {
  216. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/largeIntAmf3Request.bin');
  217. // send the mock object request to be deserialized
  218. $this->_request->initialize($myRequest);
  219. // Make sure the encoding type is properly set.
  220. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  221. // Make sure that no headers where recieved
  222. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  223. // Make sure that the message body was set after deserialization
  224. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  225. $bodies = $this->_request->getAmfBodies();
  226. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  227. $message = $bodies[0]->getData();
  228. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  229. // Make sure that our endpoint is properly set.
  230. $this->assertEquals('returnInt', $message->operation);
  231. $this->assertEquals('RoundTrip', $message->source);
  232. $data = $message->body;
  233. // Make sure that we are dealing with a PHP array
  234. $this->assertTrue(is_int($data[0]));
  235. // Make sure that the array was deserialized properly and check its value
  236. $this->assertEquals(268435455, $data[0]);
  237. }
  238. /**
  239. * Read boolean true and convert it to php boolean true
  240. *
  241. */
  242. public function testAmf3BoolTrueParameterDeserializedToNativePhpBool()
  243. {
  244. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/boolTrueAmf3Request.bin');
  245. // send the mock object request to be deserialized
  246. $this->_request->initialize($myRequest);
  247. // Make sure the encoding type is properly set.
  248. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  249. // Make sure that the message body was set after deserialization
  250. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  251. $bodies = $this->_request->getAmfBodies();
  252. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  253. $message = $bodies[0]->getData();
  254. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  255. // Make sure that our endpoint is properly set.
  256. $this->assertEquals('returnBool', $message->operation);
  257. $this->assertEquals('RoundTrip', $message->source);
  258. $data = $message->body;
  259. // Make sure that we are dealing with a PHP array
  260. $this->assertTrue(is_bool($data[0]));
  261. // Make sure that the Bool was deserialized properly and check its value
  262. $this->assertEquals(true, $data[0]);
  263. }
  264. /**
  265. * Convert boolean false to php boolean false.
  266. *
  267. */
  268. public function testAmf3BoolFalseParameterDeserializedToNativePhpBool()
  269. {
  270. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/boolFalseAmf3Request.bin');
  271. // send the mock object request to be deserialized
  272. $this->_request->initialize($myRequest);
  273. // Make sure the encoding type is properly set.
  274. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  275. // Make sure that the message body was set after deserialization
  276. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  277. $bodies = $this->_request->getAmfBodies();
  278. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  279. $message = $bodies[0]->getData();
  280. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  281. // Make sure that our endpoint is properly set.
  282. $this->assertEquals('returnBool', $message->operation);
  283. $this->assertEquals('RoundTrip', $message->source);
  284. $data = $message->body;
  285. // Make sure that we are dealing with a PHP array
  286. $this->assertTrue(is_bool($data[0]));
  287. // Make sure that the Bool was deserialized properly and check its value
  288. $this->assertEquals(false, $data[0]);
  289. }
  290. public function testAmf3XmlParameterDeserializedToNativePhpSimpleXml()
  291. {
  292. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/xmlAmf3Request.bin');
  293. // send the mock object request to be deserialized
  294. $this->_request->initialize($myRequest);
  295. // Make sure the encoding type is properly set.
  296. $this->assertEquals(0x03, $this->_request->getObjectEncoding());
  297. // Make sure that no headers where recieved
  298. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  299. // Make sure that the message body was set after deserialization
  300. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  301. $bodies = $this->_request->getAmfBodies();
  302. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  303. $message = $bodies[0]->getData();
  304. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_RemotingMessage);
  305. // Make sure that our endpoint is properly set.
  306. $this->assertEquals('returnXml', $message->operation);
  307. $this->assertEquals('RoundTrip', $message->source);
  308. $data = $message->body;
  309. // Make sure that we are dealing with a PHP simpleXml element
  310. $this->assertTrue($data[0] instanceof SimpleXMLElement);
  311. // Make sure that the xml was deserialized properly and check its value
  312. $this->assertEquals('hello', (string) $data[0]->p);
  313. }
  314. public function testAmf3ByteArrayDeserializedToNativePhpString()
  315. {
  316. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/byteArrayAmf3Request.bin');
  317. // send the mock object request to be deserialized
  318. $this->_request->initialize($myRequest);
  319. // Make sure that no headers where recieved
  320. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  321. // Make sure that the message body was set after deserialization
  322. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  323. $requestBody = $this->_request->getAmfBodies();
  324. $this->assertTrue($requestBody[0] instanceof Zend_Amf_Value_MessageBody);
  325. $data = $requestBody[0]->getData();
  326. // Make sure that we are dealing with a PHP string
  327. $this->assertTrue(is_string($data[0]));
  328. // Make sure that the string was deserialized properly and check its value
  329. $byteArray = file_get_contents(dirname(__FILE__) .'/Request/bytearray.bin');
  330. $this->assertEquals($byteArray, $data[0]);
  331. }
  332. /**
  333. * Actionscript String to PHP String
  334. *
  335. */
  336. public function testAmf0StringParameterDeserializedToNativePhpString()
  337. {
  338. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/stringAmf0Request.bin');
  339. // send the mock object request to be deserialized
  340. $this->_request->initialize($myRequest);
  341. // Make sure that no headers where recieved
  342. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  343. // Make sure that the message body was set after deserialization
  344. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  345. $requestBody = $this->_request->getAmfBodies();
  346. $this->assertTrue($requestBody[0] instanceof Zend_Amf_Value_MessageBody);
  347. $this->assertEquals('RoundTrip.returnString', $requestBody[0]->getTargetURI());
  348. $data = $requestBody[0]->getData();
  349. // Make sure that we are dealing with a PHP string
  350. $this->assertTrue(is_string($data[0]));
  351. // Make sure that the string was deserialized properly and check its value
  352. $this->assertEquals('abcdefghijklmpqrstuvwxyz', $data[0]);
  353. }
  354. /**
  355. * ActionScript Object to PHP Object for Amf0
  356. *
  357. */
  358. public function testAmf0ObjectParameterDeserializedToNativePhpObject()
  359. {
  360. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/objectAmf0Request.bin');
  361. // send the mock object request to be deserialized
  362. $this->_request->initialize($myRequest);
  363. // Make sure that no headers where recieved
  364. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  365. // Make sure that the message body was set after deserialization
  366. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  367. $bodies = $this->_request->getAmfBodies();
  368. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  369. $data = $bodies[0]->getData();
  370. // Make sure that we are dealing with a PHP string
  371. // Make sure that the string was deserialized properly and check its value
  372. $this->assertEquals('foo', $data[0]->a);
  373. $this->assertEquals('bar', $data[0]->b);
  374. }
  375. /**
  376. * Test to make sure that a generic object as the first paramater does not crash
  377. * @group ZF-5346
  378. */
  379. public function testAmf0ObjectFirstParameter()
  380. {
  381. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/objectFirstParamRequest.bin');
  382. // send the mock object request to be deserialized
  383. $this->_request->initialize($myRequest);
  384. // Make sure that no headers where recieved
  385. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  386. // Make sure that the message body was set after deserialization
  387. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  388. $bodies = $this->_request->getAmfBodies();
  389. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  390. $data = $bodies[0]->getData();
  391. // Make sure that we are dealing with a PHP string
  392. // Make sure that the string was deserialized properly and check its value
  393. $this->assertEquals('foo', $data[0]->a);
  394. $this->assertEquals('bar', $data[0]->b);
  395. $this->assertEquals(1234, $data[1]);
  396. }
  397. /**
  398. * ActionScript Mixed Array to PHP Object for Amf0
  399. *
  400. */
  401. public function testAmf0MixedArrayParameterDeserializedToNativePhpObject()
  402. {
  403. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/mixedArrayAmf0Request.bin');
  404. // send the mock object request to be deserialized
  405. $this->_request->initialize($myRequest);
  406. // Make sure that no headers where recieved
  407. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  408. // Make sure that the message body was set after deserialization
  409. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  410. $bodies = $this->_request->getAmfBodies();
  411. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  412. $data = $bodies[0]->getData();
  413. // Make sure that the string was deserialized properly and check its value
  414. $this->assertTrue(array_key_exists(1, $data[0]));
  415. $this->assertEquals('two', $data[0]->two);
  416. }
  417. /**
  418. * ActionScript Numnber to PHP float
  419. *
  420. */
  421. public function testAmf0NumberParameterDeserializedToNativePhpFloat()
  422. {
  423. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/numberAmf0Request.bin');
  424. // send the mock object request to be deserialized
  425. $this->_request->initialize($myRequest);
  426. // Make sure that no headers where recieved
  427. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  428. // Make sure that the message body was set after deserialization
  429. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  430. $bodies = $this->_request->getAmfBodies();
  431. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  432. $data = $bodies[0]->getData();
  433. // Make sure that the string was deserialized properly and check its value
  434. $this->assertTrue(is_float($data[0]));
  435. $this->assertEquals(31.57, $data[0]);
  436. }
  437. /**
  438. * ActionScript Date to PHP DateTime
  439. *
  440. */
  441. public function testAmf0DateParameterDeserializedToNativePhpDateTime()
  442. {
  443. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/dateAmf0Request.bin');
  444. // send the mock object request to be deserialized
  445. $this->_request->initialize($myRequest);
  446. // Make sure that no headers where recieved
  447. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  448. // Make sure that the message body was set after deserialization
  449. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  450. $bodies = $this->_request->getAmfBodies();
  451. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  452. $data = $bodies[0]->getData();
  453. // Make sure that the string was deserialized properly and check its value
  454. $this->assertEquals(10, $data[0]->toString('M'));
  455. $this->assertEquals(1978, $data[0]->toString('Y'));
  456. }
  457. /**
  458. * ActionScript Integer to PHP int
  459. *
  460. */
  461. public function testAmf0IntParameterDeserializedToNativePhpint()
  462. {
  463. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/intAmf0Request.bin');
  464. // send the mock object request to be deserialized
  465. $this->_request->initialize($myRequest);
  466. // Make sure that no headers where recieved
  467. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  468. // Make sure that the message body was set after deserialization
  469. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  470. $bodies = $this->_request->getAmfBodies();
  471. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  472. $data = $bodies[0]->getData();
  473. // Make sure that the string was deserialized properly and check its value
  474. $this->assertEquals(268435456, $data[0]);
  475. }
  476. /**
  477. * Convert an Amf0 boolean true to php boolean
  478. *
  479. */
  480. public function testAmf0BoolTrueParameterDeserializedToNativePhpBool()
  481. {
  482. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/boolTrueAmf0Request.bin');
  483. // send the mock object request to be deserialized
  484. $this->_request->initialize($myRequest);
  485. // Make sure that no headers where recieved
  486. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  487. // Make sure that the message body was set after deserialization
  488. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  489. $bodies = $this->_request->getAmfBodies();
  490. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  491. $data = $bodies[0]->getData();
  492. // Make sure that the string was deserialized properly and check its value
  493. $this->assertTrue(is_bool($data[0]));
  494. $this->assertEquals(true, $data[0]);
  495. }
  496. /**
  497. * Convert an Amf0 boolean false to php boolean
  498. *
  499. */
  500. public function testAmf0BoolFalseParameterDeserializedToNativePhpBool()
  501. {
  502. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/boolFalseAmf0Request.bin');
  503. // send the mock object request to be deserialized
  504. $this->_request->initialize($myRequest);
  505. // Make sure that no headers where recieved
  506. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  507. // Make sure that the message body was set after deserialization
  508. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  509. $bodies = $this->_request->getAmfBodies();
  510. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  511. $data = $bodies[0]->getData();
  512. // Make sure that the string was deserialized properly and check its value
  513. $this->assertTrue(is_bool($data[0]));
  514. $this->assertEquals(false, $data[0]);
  515. }
  516. public function testAmf0NullDeserializedToNativePhpNull()
  517. {
  518. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/nullAmf0Request.bin');
  519. // send the mock object request to be deserialized
  520. $this->_request->initialize($myRequest);
  521. // Make sure that no headers where recieved
  522. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  523. // Make sure that the message body was set after deserialization
  524. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  525. $bodies = $this->_request->getAmfBodies();
  526. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  527. $data = $bodies[0]->getData();
  528. // Make sure that the string was deserialized properly and check its value
  529. $this->assertTrue(is_null($data[0]));
  530. }
  531. public function testAmf0UndefinedDeserializedToNativePhpNull()
  532. {
  533. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/undefinedAmf0Request.bin');
  534. // send the mock object request to be deserialized
  535. $this->_request->initialize($myRequest);
  536. // Make sure that no headers where recieved
  537. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  538. // Make sure that the message body was set after deserialization
  539. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  540. $bodies = $this->_request->getAmfBodies();
  541. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  542. $data = $bodies[0]->getData();
  543. // Make sure that the string was deserialized properly and check its value
  544. $this->assertTrue(is_null($data[0]));
  545. }
  546. public function testAmf0XmlParameterDeserializedToNativePhpSimpleXml()
  547. {
  548. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/xmlAmf0Request.bin');
  549. // send the mock object request to be deserialized
  550. $this->_request->initialize($myRequest);
  551. // Make sure that no headers where recieved
  552. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  553. // Make sure that the message body was set after deserialization
  554. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  555. $bodies = $this->_request->getAmfBodies();
  556. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  557. $data = $bodies[0]->getData();
  558. // Make sure that we are dealing with a PHP simpleXml element
  559. $this->assertTrue($data[0] instanceof SimpleXMLElement);
  560. // Make sure that the xml was deserialized properly and check its value
  561. $this->assertEquals('hello', (string) $data[0]->p);
  562. }
  563. public function testAmf0ReferenceDeserialized()
  564. {
  565. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/referenceAmf0Request.bin');
  566. // send the mock object request to be deserialized
  567. $this->_request->initialize($myRequest);
  568. // Make sure that no headers where recieved
  569. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  570. // Make sure that the message body was set after deserialization
  571. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  572. $bodies = $this->_request->getAmfBodies();
  573. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  574. $data = $bodies[0]->getData();
  575. // Make sure that we are dealing with a PHP a number
  576. // Make sure that the xml was deserialized properly and check its value
  577. $this->assertEquals('foo', (string) $data[0]->a);
  578. }
  579. public function testAmf0TypedObjecDeserializedToNativePHPObject()
  580. {
  581. Zend_Amf_Parse_TypeLoader::setMapping("ContactVO","Contact");
  582. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/typedObjectAmf0Request.bin');
  583. // send the mock object request to be deserialized
  584. $this->_request->initialize($myRequest);
  585. // Make sure that no headers where recieved
  586. $this->assertEquals(0 , sizeof($this->_request->getAmfHeaders()));
  587. // Make sure that the message body was set after deserialization
  588. $this->assertEquals(1, sizeof($this->_request->getAmfBodies()));
  589. $bodies = $this->_request->getAmfBodies();
  590. $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody);
  591. $data = $bodies[0]->getData();
  592. // Make sure that we are dealing with a PHP simpleXml element
  593. $this->assertTrue($data[0] instanceof Contact);
  594. // Make sure that the xml was deserialized properly and check its value
  595. $this->assertEquals('arnold', (string) $data[0]->lastname);
  596. }
  597. public function testAmf0TypedObjecDeserializedToNativePHPObjectUnknownType()
  598. {
  599. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/bogusTypedObjectAmf0Request.bin');
  600. // send the mock object request to be deserialized
  601. $this->_request->initialize($myRequest);
  602. $requestBodies = $this->_request->getAmfBodies();
  603. $messageBody = reset($requestBodies);
  604. $data = $messageBody->getData();
  605. $dataObject = reset($data);
  606. $this->assertEquals('stdClass', get_class($dataObject));
  607. }
  608. /**
  609. * Test Amf0 credentials sent to the server
  610. *
  611. */
  612. public function testAmf0CredentialsInHeader()
  613. {
  614. $myRequest = file_get_contents(dirname(__FILE__) .'/Request/mock/credentialsheaderAmf0.bin');
  615. // send the mock object request to be deserialized
  616. $this->_request->initialize($myRequest);
  617. // Make sure that no headers where recieved
  618. $this->assertEquals(1 , sizeof($this->_request->getAmfHeaders()));
  619. $requestHeaders = $this->_request->getAmfHeaders();
  620. $this->assertTrue($requestHeaders[0] instanceof Zend_Amf_Value_MessageHeader);
  621. $this->assertEquals('Credentials', $requestHeaders[0]->name);
  622. $this->assertFalse($requestHeaders[0]->mustRead);
  623. $data = $requestHeaders[0]->data;
  624. // Check the resulting header
  625. $this->assertEquals('admin', $data->userid);
  626. $this->assertEquals('pw123', $data->password);
  627. }
  628. }
  629. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_RequestTest::main') {
  630. Zend_Amf_RequestTest::main();
  631. }