RequestTest.php 31 KB

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