RequestTest.php 30 KB

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