ServerTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. <?php
  2. // Call Zend_Amf_ServerTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Amf_ServerTest::main");
  5. }
  6. /**
  7. * Test helper
  8. */
  9. require_once dirname(__FILE__) . '/../../TestHelper.php';
  10. require_once 'Zend/Amf/Server.php';
  11. require_once 'Zend/Amf/Request.php';
  12. require_once 'Zend/Amf/Parse/TypeLoader.php';
  13. require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
  14. require_once 'ServiceA.php';
  15. require_once 'ServiceB.php';
  16. require_once 'Zend/Session.php';
  17. class Zend_Amf_ServerTest extends PHPUnit_Framework_TestCase
  18. {
  19. protected $_server;
  20. public static function main()
  21. {
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_ServerTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. public function setUp()
  26. {
  27. $this->_server = new Zend_Amf_Server();
  28. $this->_server->setProduction(false);
  29. }
  30. public function testDown()
  31. {
  32. unset($this->_server);
  33. }
  34. /**
  35. * Call as method call
  36. *
  37. * Returns: void
  38. */
  39. public function test__construct()
  40. {
  41. $this->assertTrue($this->_server instanceof Zend_Amf_Server);
  42. }
  43. public function testIsProductionByDefault()
  44. {
  45. $this->_server = new Zend_Amf_Server;
  46. $this->assertTrue($this->_server->isProduction());
  47. }
  48. public function testProductionFlagShouldBeMutable()
  49. {
  50. $this->testIsProductionByDefault();
  51. $this->_server->setProduction(false);
  52. $this->assertFalse($this->_server->isProduction());
  53. $this->_server->setProduction(true);
  54. $this->assertTrue($this->_server->isProduction());
  55. }
  56. public function testSetClass()
  57. {
  58. $this->_server->setClass('Zend_Amf_testclass', 'test');
  59. $methods = $this->_server->listMethods();
  60. $this->assertTrue(in_array('test.test1', $methods));
  61. $this->assertTrue(in_array('test.test2', $methods));
  62. $this->assertFalse(in_array('test._test3', $methods));
  63. $this->assertFalse(in_array('test.__construct', $methods));
  64. }
  65. /**
  66. * @expectedException Zend_Amf_Server_Exception
  67. */
  68. public function testSetClassShouldRaiseExceptionOnInvalidClassname()
  69. {
  70. $this->_server->setClass('foobar');
  71. }
  72. /**
  73. * @expectedException Zend_Amf_Server_Exception
  74. */
  75. public function testSetClassShouldRaiseExceptionOnInvalidClasstype()
  76. {
  77. $this->_server->setClass(array('foobar'));
  78. }
  79. /**
  80. * @expectedException Zend_Amf_Server_Exception
  81. */
  82. public function testSetClassShouldRaiseExceptionOnDuplicateMethodName()
  83. {
  84. $this->_server->setClass('Zend_Amf_testclass', 'tc');
  85. $this->_server->setClass('Zend_Amf_testclassPrivate', 'tc');
  86. }
  87. /**
  88. * ZF-5393
  89. */
  90. public function testSetClassUsingObject()
  91. {
  92. $testClass = new Zend_Amf_testclass();
  93. $this->_server->setClass($testClass);
  94. $this->assertEquals(8, count($this->_server->getFunctions()));
  95. }
  96. /**
  97. * addFunction() test
  98. *
  99. * Call as method call
  100. *
  101. * Expects:
  102. * - function:
  103. * - namespace: Optional; has default;
  104. *
  105. * Returns: void
  106. */
  107. public function testAddFunction()
  108. {
  109. try {
  110. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'test');
  111. } catch (Exception $e) {
  112. $this->fail('Attachment should have worked');
  113. }
  114. $methods = $this->_server->listMethods();
  115. $this->assertTrue(in_array('test.Zend_Amf_Server_testFunction', $methods), var_export($methods, 1));
  116. try {
  117. $this->_server->addFunction('nosuchfunction');
  118. $this->fail('nosuchfunction() should not exist and should throw an exception');
  119. } catch (Exception $e) {
  120. // do nothing
  121. }
  122. $server = new Zend_Amf_Server();
  123. try {
  124. $server->addFunction(
  125. array(
  126. 'Zend_Amf_Server_testFunction',
  127. 'Zend_Amf_Server_testFunction2',
  128. ),
  129. 'zsr'
  130. );
  131. } catch (Exception $e) {
  132. $this->fail('Error attaching array of functions: ' . $e->getMessage());
  133. }
  134. $methods = $server->listMethods();
  135. $this->assertTrue(in_array('zsr.Zend_Amf_Server_testFunction', $methods));
  136. $this->assertTrue(in_array('zsr.Zend_Amf_Server_testFunction2', $methods));
  137. }
  138. /**
  139. * @expectedException Zend_Amf_Server_Exception
  140. */
  141. public function testAddFunctionShouldRaiseExceptionForInvalidFunctionName()
  142. {
  143. $this->_server->addFunction(true);
  144. }
  145. /**
  146. * @expectedException Zend_Amf_Server_Exception
  147. */
  148. public function testAddFunctionShouldRaiseExceptionOnDuplicateMethodName()
  149. {
  150. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'tc');
  151. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'tc');
  152. }
  153. /**
  154. * Test sending data to the remote class and make sure we
  155. * recieve the proper response.
  156. *
  157. */
  158. public function testHandleLoadedClassAmf0()
  159. {
  160. // serialize the data to an AMF output stream
  161. $data[] = "12345";
  162. $this->_server->setClass('Zend_Amf_testclass');
  163. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.test1","/1",$data);
  164. $request = new Zend_Amf_Request();
  165. $request->addAmfBody($newBody);
  166. $request->setObjectEncoding(0x00);
  167. $result = $this->_server->handle($request);
  168. $response = $this->_server->getResponse();
  169. $responseBody = $response->getAmfBodies();
  170. // Now check if the return data was properly set.
  171. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  172. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  173. $this->assertEquals("String: 12345", $responseBody[0]->getData(), var_export($responseBody, 1));
  174. }
  175. public function testShouldAllowHandlingFunctionCallsViaAmf0()
  176. {
  177. // serialize the data to an AMF output stream
  178. $data = array('foo', 'bar');
  179. $this->_server->addFunction('Zend_Amf_Server_testFunction');
  180. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_Server_testFunction","/1",$data);
  181. $request = new Zend_Amf_Request();
  182. $request->addAmfBody($newBody);
  183. $request->setObjectEncoding(0x00);
  184. $result = $this->_server->handle($request);
  185. $response = $this->_server->getResponse();
  186. $responseBody = $response->getAmfBodies();
  187. // Now check if the return data was properly set.
  188. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  189. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  190. $this->assertEquals("bar: foo", $responseBody[0]->getData(), var_export($responseBody, 1));
  191. }
  192. /**
  193. * Test to make sure that AMF3 basic requests are handled for loading
  194. * a class.
  195. * This type of call is sent from NetConnection rather than RemoteObject
  196. *
  197. * @group ZF-4680
  198. */
  199. public function testHandleLoadedClassAmf3NetConnection()
  200. {
  201. // serialize the data to an AMF output stream
  202. $data[] = "12345";
  203. $this->_server->setClass('Zend_Amf_testclass');
  204. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.test1","/1",$data);
  205. $request = new Zend_Amf_Request();
  206. $request->addAmfBody($newBody);
  207. $request->setObjectEncoding(0x03);
  208. $result = $this->_server->handle($request);
  209. $response = $this->_server->getResponse();
  210. $responseBody = $response->getAmfBodies();
  211. // Now check if the return data was properly set.
  212. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  213. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  214. $this->assertEquals("String: 12345", $responseBody[0]->getData(), var_export($responseBody, 1));
  215. }
  216. /**
  217. * Test to make sure that AMF3 basic requests are handled for function calls.
  218. * This type of call is sent from net connection rather than RemoteObject
  219. *
  220. * @group ZF-4680
  221. */
  222. public function testShouldAllowHandlingFunctionCallsViaAmf3NetConnection()
  223. {
  224. // serialize the data to an AMF output stream
  225. $data = array('foo', 'bar');
  226. $this->_server->addFunction('Zend_Amf_Server_testFunction');
  227. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_Server_testFunction","/1",$data);
  228. $request = new Zend_Amf_Request();
  229. $request->addAmfBody($newBody);
  230. $request->setObjectEncoding(0x03);
  231. $result = $this->_server->handle($request);
  232. $response = $this->_server->getResponse();
  233. $responseBody = $response->getAmfBodies();
  234. // Now check if the return data was properly set.
  235. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  236. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  237. $this->assertEquals("bar: foo", $responseBody[0]->getData(), var_export($responseBody, 1));
  238. }
  239. /**
  240. * Test sending data to the remote class and make sure we
  241. * recieve the proper response.
  242. *
  243. */
  244. public function testHandleLoadedClassAmf3()
  245. {
  246. // serialize the data to an AMF output stream
  247. $data[] = "12345";
  248. $this->_server->setClass('Zend_Amf_testclass');
  249. // create a mock remoting message
  250. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  251. $message->operation = 'test1';
  252. $message->source = 'Zend_Amf_testclass';
  253. $message->body = $data;
  254. // create a mock message body to place th remoting message inside
  255. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  256. $request = new Zend_Amf_Request();
  257. // at the requested service to a request
  258. $request->addAmfBody($newBody);
  259. $request->setObjectEncoding(0x03);
  260. // let the server handle mock request
  261. $result = $this->_server->handle($request);
  262. $response = $this->_server->getResponse();
  263. $responseBody = $response->getAmfBodies();
  264. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  265. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  266. // Now check if the return data was properly set.
  267. $acknowledgeMessage = $responseBody[0]->getData();
  268. // check that we have a message beening returned
  269. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  270. // Check the message body is the expected data to be returned
  271. $this->assertEquals("String: 12345", $acknowledgeMessage->body);
  272. }
  273. /**
  274. * Test to make sure that you can have the same method name in two different classes.
  275. *
  276. * @group ZF-5040
  277. */
  278. public function testSameMethodNameInTwoServices()
  279. {
  280. $this->_server->setClass('ServiceA');
  281. $this->_server->setClass('ServiceB');
  282. // create a mock remoting message
  283. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  284. $message->operation = 'getMenu';
  285. $message->source = 'ServiceB';
  286. $message->body = array();
  287. // create a mock message body to place th remoting message inside
  288. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  289. $request = new Zend_Amf_Request();
  290. // at the requested service to a request
  291. $request->addAmfBody($newBody);
  292. $request->setObjectEncoding(0x03);
  293. // let the server handle mock request
  294. $result = $this->_server->handle($request);
  295. $response = $this->_server->getResponse();
  296. $responseBody = $response->getAmfBodies();
  297. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  298. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  299. // Now check if the return data was properly set.
  300. $acknowledgeMessage = $responseBody[0]->getData();
  301. // check that we have a message beening returned
  302. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  303. // Check the message body is the expected data to be returned
  304. $this->assertEquals("myMenuB", $acknowledgeMessage->body);
  305. }
  306. /**
  307. * test command message. THis is the first call the Flex
  308. * makes before any subsequent service calls.
  309. */
  310. public function testCommandMessagePingOperation()
  311. {
  312. $message = new Zend_Amf_Value_Messaging_CommandMessage();
  313. $message->operation = 5;
  314. $message->messageId = $message->generateId();
  315. // create a mock message body to place th remoting message inside
  316. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  317. $request = new Zend_Amf_Request();
  318. // at the requested service to a request
  319. $request->addAmfBody($newBody);
  320. $request->setObjectEncoding(0x03);
  321. // let the server handle mock request
  322. $result = $this->_server->handle($request);
  323. $response = $this->_server->getResponse();
  324. $responseBody = $response->getAmfBodies();
  325. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  326. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  327. // Now check if the return data was properly set.
  328. $acknowledgeMessage = $responseBody[0]->getData();
  329. // check that we have a message beening returned
  330. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  331. // Check that the MessageID was not corrupeted when set to the correlationId
  332. $this->assertEquals($acknowledgeMessage->correlationId, $message->messageId);
  333. }
  334. public function testInvalidAmf0MessageShouldResultInErrorMessage()
  335. {
  336. // serialize the data to an AMF output stream
  337. $data[] = "12345";
  338. $this->_server->setClass('Zend_Amf_testclass');
  339. $newBody = new Zend_Amf_Value_MessageBody("bogus","/1",$data);
  340. $request = new Zend_Amf_Request();
  341. $request->addAmfBody($newBody);
  342. $request->setObjectEncoding(0x00);
  343. $result = $this->_server->handle($request);
  344. $bodies = $result->getAmfBodies();
  345. $found = false;
  346. foreach ($bodies as $body) {
  347. $data = $body->getData();
  348. if (!is_array($data)) {
  349. continue;
  350. }
  351. if (!array_key_exists('description', $data)) {
  352. continue;
  353. }
  354. if (strstr($data['description'], 'does not exist')) {
  355. $found = true;
  356. break;
  357. }
  358. }
  359. $this->assertTrue($found, 'Invalid method did not raise error condition' . var_export($bodies, 1));
  360. }
  361. public function testInvalidCommandMessageShouldResultInErrorMessage()
  362. {
  363. $message = new Zend_Amf_Value_Messaging_CommandMessage();
  364. $message->operation = 'pong';
  365. $message->messageId = $message->generateId();
  366. // create a mock message body to place th remoting message inside
  367. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  368. $request = new Zend_Amf_Request();
  369. // at the requested service to a request
  370. $request->addAmfBody($newBody);
  371. $request->setObjectEncoding(0x03);
  372. // let the server handle mock request
  373. $result = $this->_server->handle($request);
  374. $response = $this->_server->getResponse();
  375. $responseBody = $response->getAmfBodies();
  376. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  377. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  378. // Now check if the return data was properly set.
  379. $message = $responseBody[0]->getData();
  380. // check that we have a message beening returned
  381. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  382. }
  383. /**
  384. * Add a class mapping and lookup the mapping to make sure
  385. * the mapping succeeds
  386. */
  387. public function testClassMap()
  388. {
  389. $this->_server->setClassMap('controller.test', 'Zend_Amf_testclass');
  390. $className = Zend_Amf_Parse_TypeLoader::getMappedClassName('Zend_Amf_testclass');
  391. $this->assertEquals('controller.test', $className);
  392. }
  393. public function testDispatchingMethodShouldReturnErrorMessageForInvalidMethod()
  394. {
  395. // serialize the data to an AMF output stream
  396. $data[] = "12345";
  397. $this->_server->setClass('Zend_Amf_testclass');
  398. // create a mock remoting message
  399. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  400. $message->operation = 'bogus'; // INVALID method!
  401. $message->body = $data;
  402. // create a mock message body to place th remoting message inside
  403. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  404. $request = new Zend_Amf_Request();
  405. // at the requested service to a request
  406. $request->addAmfBody($newBody);
  407. $request->setObjectEncoding(0x03);
  408. // let the server handle mock request
  409. $result = $this->_server->handle($request);
  410. $bodies = $result->getAmfBodies();
  411. $found = false;
  412. foreach ($bodies as $body) {
  413. $data = $body->getData();
  414. if ($data instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
  415. if (strstr($data->faultString, 'does not exist')) {
  416. $found = true;
  417. break;
  418. }
  419. }
  420. }
  421. $this->assertTrue($found, 'Invalid method did not raise error condition: ' . var_export($bodies, 1));
  422. }
  423. public function testDispatchingMethodThatThrowsExceptionShouldReturnErrorMessageWhenProductionFlagOff()
  424. {
  425. // serialize the data to an AMF output stream
  426. $data = array();
  427. $this->_server->setClass('Zend_Amf_testclass');
  428. // create a mock remoting message
  429. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  430. $message->operation = 'throwException';
  431. $message->source = 'Zend_Amf_testclass';
  432. $message->body = $data;
  433. // create a mock message body to place th remoting message inside
  434. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  435. $request = new Zend_Amf_Request();
  436. // at the requested service to a request
  437. $request->addAmfBody($newBody);
  438. $request->setObjectEncoding(0x03);
  439. // let the server handle mock request
  440. $result = $this->_server->handle($request);
  441. $bodies = $result->getAmfBodies();
  442. $found = false;
  443. foreach ($bodies as $body) {
  444. $data = $body->getData();
  445. if ($data instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
  446. if (strstr($data->faultString, 'should not be displayed')) {
  447. $found = true;
  448. break;
  449. }
  450. }
  451. }
  452. $this->assertTrue($found, 'Method raising exception should display error message when not in production');
  453. }
  454. public function testDispatchingMethodThatThrowsExceptionShouldNotReturnErrorMessageWhenProductionFlagOn()
  455. {
  456. // serialize the data to an AMF output stream
  457. $data = array();
  458. $this->_server->setClass('Zend_Amf_testclass')
  459. ->setProduction(true);
  460. // create a mock remoting message
  461. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  462. $message->operation = 'throwException';
  463. $message->source = 'Zend_Amf_testclass';
  464. $message->body = $data;
  465. // create a mock message body to place th remoting message inside
  466. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  467. $request = new Zend_Amf_Request();
  468. // at the requested service to a request
  469. $request->addAmfBody($newBody);
  470. $request->setObjectEncoding(0x03);
  471. // let the server handle mock request
  472. $result = $this->_server->handle($request);
  473. $bodies = $result->getAmfBodies();
  474. $found = false;
  475. foreach ($bodies as $body) {
  476. $data = $body->getData();
  477. if ($data instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
  478. if (strstr($data->faultString, 'should not be displayed')) {
  479. $found = true;
  480. break;
  481. }
  482. }
  483. }
  484. $this->assertFalse($found, 'Method raising exception should not display error message when in production');
  485. }
  486. public function testDispatchingMethodShouldPassInvocationArgumentsToMethod()
  487. {
  488. // serialize the data to an AMF output stream
  489. $data[] = "baz";
  490. $this->_server->setClass('Zend_Amf_testclass', '', 'foo', 'bar');
  491. // create a mock remoting message
  492. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  493. $message->operation = 'checkArgv';
  494. $message->source = 'Zend_Amf_testclass';
  495. $message->body = $data;
  496. // create a mock message body to place th remoting message inside
  497. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  498. $request = new Zend_Amf_Request();
  499. // at the requested service to a request
  500. $request->addAmfBody($newBody);
  501. $request->setObjectEncoding(0x03);
  502. // let the server handle mock request
  503. $result = $this->_server->handle($request);
  504. $bodies = $result->getAmfBodies();
  505. $found = false;
  506. foreach ($bodies as $body) {
  507. $data = $body->getData();
  508. if ('Zend_Amf_Value_Messaging_AcknowledgeMessage' == get_class($data)) {
  509. if ('baz:foo:bar' == $data->body) {
  510. $found = true;
  511. break;
  512. }
  513. }
  514. }
  515. $this->assertTrue($found, 'Valid response not found');
  516. }
  517. public function testServerShouldSeamlesslyInvokeStaticMethods()
  518. {
  519. // serialize the data to an AMF output stream
  520. $data[] = "testing";
  521. $this->_server->setClass('Zend_Amf_testclass');
  522. // create a mock remoting message
  523. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  524. $message->operation = 'checkStaticUsage';
  525. $message->source = 'Zend_Amf_testclass';
  526. $message->body = $data;
  527. // create a mock message body to place th remoting message inside
  528. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  529. $request = new Zend_Amf_Request();
  530. // at the requested service to a request
  531. $request->addAmfBody($newBody);
  532. $request->setObjectEncoding(0x03);
  533. // let the server handle mock request
  534. $result = $this->_server->handle($request);
  535. $bodies = $result->getAmfBodies();
  536. $found = false;
  537. foreach ($bodies as $body) {
  538. $data = $body->getData();
  539. if ('Zend_Amf_Value_Messaging_AcknowledgeMessage' == get_class($data)) {
  540. if ('testing' == $data->body) {
  541. $found = true;
  542. break;
  543. }
  544. }
  545. }
  546. $this->assertTrue($found, 'Valid response not found');
  547. }
  548. public function testServerShouldSeamlesslyInvokeFunctions()
  549. {
  550. // serialize the data to an AMF output stream
  551. $data[] = 'foo';
  552. $data[] = 'bar';
  553. $this->_server->addFunction('Zend_Amf_Server_testFunction');
  554. // create a mock remoting message
  555. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  556. $message->operation = 'Zend_Amf_Server_testFunction';
  557. $message->source = null;
  558. $message->body = $data;
  559. // create a mock message body to place th remoting message inside
  560. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  561. $request = new Zend_Amf_Request();
  562. // at the requested service to a request
  563. $request->addAmfBody($newBody);
  564. $request->setObjectEncoding(0x03);
  565. // let the server handle mock request
  566. $result = $this->_server->handle($request);
  567. $bodies = $result->getAmfBodies();
  568. $found = false;
  569. foreach ($bodies as $body) {
  570. $data = $body->getData();
  571. if ('Zend_Amf_Value_Messaging_AcknowledgeMessage' == get_class($data)) {
  572. if ('bar: foo' == $data->body) {
  573. $found = true;
  574. break;
  575. }
  576. }
  577. }
  578. $this->assertTrue($found, 'Valid response not found');
  579. }
  580. public function testDispatchingMethodCorrespondingToClassWithPrivateConstructorShouldReturnErrorMessage()
  581. {
  582. // serialize the data to an AMF output stream
  583. $data[] = "baz";
  584. $this->_server->setClass('Zend_Amf_testclassPrivate');
  585. // create a mock remoting message
  586. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  587. $message->operation = 'test1';
  588. $message->source = 'Zend_Amf_testclassPrivate';
  589. $message->body = $data;
  590. // create a mock message body to place th remoting message inside
  591. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  592. $request = new Zend_Amf_Request();
  593. // at the requested service to a request
  594. $request->addAmfBody($newBody);
  595. $request->setObjectEncoding(0x03);
  596. // let the server handle mock request
  597. $result = $this->_server->handle($request);
  598. $bodies = $result->getAmfBodies();
  599. $found = false;
  600. foreach ($bodies as $body) {
  601. $data = $body->getData();
  602. if ('Zend_Amf_Value_Messaging_ErrorMessage' == get_class($data)) {
  603. if (strstr($data->faultString, 'Error instantiating class')) {
  604. $found = true;
  605. break;
  606. }
  607. }
  608. }
  609. $this->assertTrue($found, 'Method succeeded?');
  610. }
  611. public function testNotPassingRequestToHandleShouldResultInServerCreatingRequest()
  612. {
  613. $this->_server->setClass('Zend_Amf_testclass');
  614. ob_start();
  615. $result = $this->_server->handle();
  616. $content = ob_get_clean();
  617. $request = $this->_server->getRequest();
  618. $this->assertTrue($request instanceof Zend_Amf_Request_Http);
  619. $bodies = $request->getAmfBodies();
  620. $this->assertEquals(0, count($bodies));
  621. $this->assertContains('Endpoint', $content);
  622. }
  623. public function testSetRequestShouldAllowValidStringClassNames()
  624. {
  625. $this->_server->setRequest('Zend_Amf_Request');
  626. $request = $this->_server->getRequest();
  627. $this->assertTrue($request instanceof Zend_Amf_Request);
  628. $this->assertFalse($request instanceof Zend_Amf_Request_Http);
  629. }
  630. /**
  631. * @expectedException Zend_Amf_Server_Exception
  632. */
  633. public function testSetRequestShouldRaiseExceptionOnInvalidStringClassName()
  634. {
  635. $this->_server->setRequest('Zend_Amf_ServerTest_BogusRequest');
  636. }
  637. public function testSetRequestShouldAllowValidRequestObjects()
  638. {
  639. $request = new Zend_Amf_Request;
  640. $this->_server->setRequest($request);
  641. $this->assertSame($request, $this->_server->getRequest());
  642. }
  643. /**
  644. * @expectedException Zend_Amf_Server_Exception
  645. */
  646. public function testSetRequestShouldRaiseExceptionOnInvalidRequestObjects()
  647. {
  648. require_once 'Zend/XmlRpc/Request.php';
  649. $request = new Zend_XmlRpc_Request;
  650. $this->_server->setRequest($request);
  651. }
  652. public function testSetResponseShouldAllowValidStringClassNames()
  653. {
  654. $this->_server->setResponse('Zend_Amf_Response');
  655. $response = $this->_server->getResponse();
  656. $this->assertTrue($response instanceof Zend_Amf_Response);
  657. $this->assertFalse($response instanceof Zend_Amf_Response_Http);
  658. }
  659. /**
  660. * @expectedException Zend_Amf_Server_Exception
  661. */
  662. public function testSetResponseShouldRaiseExceptionOnInvalidStringClassName()
  663. {
  664. $this->_server->setResponse('Zend_Amf_ServerTest_BogusResponse');
  665. }
  666. public function testSetResponseShouldAllowValidResponseObjects()
  667. {
  668. $response = new Zend_Amf_Response;
  669. $this->_server->setResponse($response);
  670. $this->assertSame($response, $this->_server->getResponse());
  671. }
  672. /**
  673. * @expectedException Zend_Amf_Server_Exception
  674. */
  675. public function testSetResponseShouldRaiseExceptionOnInvalidResponseObjects()
  676. {
  677. require_once 'Zend/XmlRpc/Response.php';
  678. $response = new Zend_XmlRpc_Response;
  679. $this->_server->setResponse($response);
  680. }
  681. public function testGetFunctionsShouldReturnArrayOfDispatchables()
  682. {
  683. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'tf')
  684. ->setClass('Zend_Amf_testclass', 'tc')
  685. ->setClass('Zend_Amf_testclassPrivate', 'tcp');
  686. $functions = $this->_server->getFunctions();
  687. $this->assertTrue(is_array($functions));
  688. $this->assertTrue(0 < count($functions));
  689. $namespaces = array('tf', 'tc', 'tcp');
  690. foreach ($functions as $key => $value) {
  691. $this->assertTrue(strstr($key, '.') ? true : false, $key);
  692. $ns = substr($key, 0, strpos($key, '.'));
  693. $this->assertContains($ns, $namespaces, $key);
  694. $this->assertTrue($value instanceof Zend_Server_Reflection_Function_Abstract);
  695. }
  696. }
  697. public function testFaultShouldBeUnimplemented()
  698. {
  699. $this->assertNull($this->_server->fault());
  700. }
  701. public function testPersistenceShouldBeUnimplemented()
  702. {
  703. $this->assertNull($this->_server->setPersistence(true));
  704. }
  705. public function testLoadFunctionsShouldBeUnimplemented()
  706. {
  707. $this->assertNull($this->_server->loadFunctions(true));
  708. }
  709. /**
  710. * @group ZF-5388
  711. * Issue if only one parameter of type array is passed it is nested into another array.
  712. */
  713. public function testSingleArrayParamaterAMF3()
  714. {
  715. // serialize the data to an AMF output stream
  716. $data[] = array('item1', 'item2');
  717. $this->_server->setClass('Zend_Amf_testclass');
  718. // create a mock remoting message
  719. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  720. $message->operation = 'testSingleArrayParamater';
  721. $message->source = 'Zend_Amf_testclass';
  722. $message->body = $data;
  723. // create a mock message body to place th remoting message inside
  724. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  725. $request = new Zend_Amf_Request();
  726. // at the requested service to a request
  727. $request->addAmfBody($newBody);
  728. $request->setObjectEncoding(0x03);
  729. // let the server handle mock request
  730. $result = $this->_server->handle($request);
  731. $response = $this->_server->getResponse();
  732. $responseBody = $response->getAmfBodies();
  733. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  734. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  735. // Now check if the return data was properly set.
  736. $acknowledgeMessage = $responseBody[0]->getData();
  737. // check that we have a message beening returned
  738. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  739. // Check the message body is the expected data to be returned
  740. $this->assertTrue($acknowledgeMessage->body);
  741. }
  742. /**
  743. * @group ZF-5388
  744. * Issue if only one parameter of type array is passed it is nested into another array.
  745. */
  746. public function testSingleArrayParamaterAMF0()
  747. {
  748. $data[] = array('item1', 'item2');
  749. $this->_server->setClass('Zend_Amf_testclass');
  750. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.testSingleArrayParamater","/1",$data);
  751. $request = new Zend_Amf_Request();
  752. $request->addAmfBody($newBody);
  753. $request->setObjectEncoding(0x00);
  754. $result = $this->_server->handle($request);
  755. $response = $this->_server->getResponse();
  756. $responseBody = $response->getAmfBodies();
  757. // Now check if the return data was properly set.
  758. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  759. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  760. $this->assertTrue($responseBody[0]->getData(), var_export($responseBody, 1));
  761. }
  762. /**
  763. * @group ZF-5388
  764. * Issue if only one parameter of type array is passed it is nested into another array.
  765. */
  766. public function testMutiArrayParamaterAMF3()
  767. {
  768. // serialize the data to an AMF output stream
  769. $data[] = array('item1', 'item2');
  770. $data[] = array('item3', 'item4');
  771. $this->_server->setClass('Zend_Amf_testclass');
  772. // create a mock remoting message
  773. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  774. $message->operation = 'testMultiArrayParamater';
  775. $message->source = 'Zend_Amf_testclass';
  776. $message->body = $data;
  777. // create a mock message body to place th remoting message inside
  778. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  779. $request = new Zend_Amf_Request();
  780. // at the requested service to a request
  781. $request->addAmfBody($newBody);
  782. $request->setObjectEncoding(0x03);
  783. // let the server handle mock request
  784. $result = $this->_server->handle($request);
  785. $response = $this->_server->getResponse();
  786. $responseBody = $response->getAmfBodies();
  787. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  788. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  789. // Now check if the return data was properly set.
  790. $acknowledgeMessage = $responseBody[0]->getData();
  791. // check that we have a message beening returned
  792. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  793. // Check the message body is the expected data to be returned
  794. $this->assertEquals(4, count($acknowledgeMessage->body));
  795. }
  796. /**
  797. * @group ZF-5388
  798. * Issue if multipol parameters are sent and one is of type array is passed.
  799. */
  800. public function testMutiArrayParamaterAMF0()
  801. {
  802. $data[] = array('item1', 'item2');
  803. $data[] = array('item3', 'item4');
  804. $this->_server->setClass('Zend_Amf_testclass');
  805. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.testMultiArrayParamater","/1",$data);
  806. $request = new Zend_Amf_Request();
  807. $request->addAmfBody($newBody);
  808. $request->setObjectEncoding(0x00);
  809. $result = $this->_server->handle($request);
  810. $response = $this->_server->getResponse();
  811. $responseBody = $response->getAmfBodies();
  812. // Now check if the return data was properly set.
  813. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  814. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  815. $this->assertEquals(4, count($responseBody[0]->getData()), var_export($responseBody, 1));
  816. }
  817. /**
  818. * @group ZF-5346
  819. */
  820. public function testSingleObjectParamaterAMF3()
  821. {
  822. // serialize the data to an AMF output stream
  823. $data[] = array('item1', 'item2');
  824. $data[] = array('item3', 'item4');
  825. $this->_server->setClass('Zend_Amf_testclass');
  826. // create a mock remoting message
  827. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  828. $message->operation = 'testMultiArrayParamater';
  829. $message->source = 'Zend_Amf_testclass';
  830. $message->body = $data;
  831. // create a mock message body to place th remoting message inside
  832. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  833. $request = new Zend_Amf_Request();
  834. // at the requested service to a request
  835. $request->addAmfBody($newBody);
  836. $request->setObjectEncoding(0x03);
  837. // let the server handle mock request
  838. $result = $this->_server->handle($request);
  839. $response = $this->_server->getResponse();
  840. $responseBody = $response->getAmfBodies();
  841. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  842. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  843. // Now check if the return data was properly set.
  844. $acknowledgeMessage = $responseBody[0]->getData();
  845. // check that we have a message beening returned
  846. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  847. // Check the message body is the expected data to be returned
  848. $this->assertEquals(4, count($acknowledgeMessage->body));
  849. }
  850. /**
  851. * Check that when using server->setSession you get an amf header that has an append to gateway sessionID
  852. * @group ZF-5381
  853. */
  854. public function testSessionAmf3()
  855. {
  856. Zend_Session::start();
  857. $this->_server->setClass('Zend_Amf_testSession');
  858. $this->_server->setSession();
  859. // create a mock remoting message
  860. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  861. $message->operation = 'getCount';
  862. $message->source = 'Zend_Amf_testSession';
  863. $message->body = array();
  864. // create a mock message body to place th remoting message inside
  865. $newBody = new Zend_Amf_Value_MessageBody(null,"/1", $message);
  866. $request = new Zend_Amf_Request();
  867. // at the requested service to a request
  868. $request->addAmfBody($newBody);
  869. $request->setObjectEncoding(0x03);
  870. // let the server handle mock request
  871. $result = $this->_server->handle($request);
  872. $response = $this->_server->getResponse();
  873. $responseBody = $response->getAmfBodies();
  874. // Now check if the return data was properly set.
  875. $acknowledgeMessage = $responseBody[0]->getData();
  876. // check that we have a message beening returned
  877. $this->assertEquals(1, $acknowledgeMessage->body);
  878. // check that a header is being returned for the session id
  879. $headerBody = $response->getAmfHeaders();
  880. $this->assertEquals('AppendToGatewayUrl',$headerBody[0]->name);
  881. // Do not stop session since it still can be used by other tests
  882. // Zend_Session::stop();
  883. }
  884. }
  885. if (PHPUnit_MAIN_METHOD == "Zend_Amf_ServerTest::main") {
  886. Zend_Amf_ServerTest::main();
  887. }
  888. /**
  889. * Zend_Amf_Server_testFunction
  890. *
  891. * Function for use with Amf server unit tests
  892. *
  893. * @param array $var1
  894. * @param string $var2
  895. * @return string
  896. */
  897. function Zend_Amf_Server_testFunction($var1, $var2 = 'optional')
  898. {
  899. return $var2 . ': ' . implode(',', (array) $var1);
  900. }
  901. /**
  902. * Zend_Amf_Server_testFunction2
  903. *
  904. * Function for use with Amf server unit tests
  905. *
  906. * @return string
  907. */
  908. function Zend_Amf_Server_testFunction2()
  909. {
  910. return 'function2';
  911. }
  912. /**
  913. * Class to used with Zend_Amf_Server unit tests.
  914. *
  915. */
  916. class Zend_Amf_testclass
  917. {
  918. public function __construct()
  919. {
  920. }
  921. /**
  922. * Concatinate a string
  923. *
  924. * @param string
  925. * @return string
  926. */
  927. public function test1($string = '')
  928. {
  929. return 'String: '. (string) $string;
  930. }
  931. /**
  932. * Test2
  933. *
  934. * Returns imploded array
  935. *
  936. * @param array $array
  937. * @return string
  938. */
  939. public static function test2($array)
  940. {
  941. return implode('; ', (array) $array);
  942. }
  943. /**
  944. * Test3
  945. *
  946. * Should not be available...
  947. *
  948. * @return void
  949. */
  950. protected function _test3()
  951. {
  952. }
  953. /**
  954. * Test base64 encoding in request and response
  955. *
  956. * @param base64 $data
  957. * @return base64
  958. */
  959. public function base64($data)
  960. {
  961. return $data;
  962. }
  963. /**
  964. * Test that invoke arguments are passed
  965. *
  966. * @param string $message message argument for comparisons
  967. * @return string
  968. */
  969. public function checkArgv($message)
  970. {
  971. $argv = func_get_args();
  972. return implode(':', $argv);
  973. }
  974. /**
  975. * Test static usage
  976. *
  977. * @param string $message
  978. * @return string
  979. */
  980. public static function checkStaticUsage($message)
  981. {
  982. return $message;
  983. }
  984. /**
  985. * Test throwing exceptions
  986. *
  987. * @return void
  988. */
  989. public function throwException()
  990. {
  991. throw new Exception('This exception should not be displayed');
  992. }
  993. /**
  994. * test if we can send an array as a paramater without it getting nested two
  995. * Used to test ZF-5388
  996. */
  997. public function testSingleArrayParamater($inputArray){
  998. if( $inputArray[0] == 'item1' ){
  999. return true;
  1000. }
  1001. return false;
  1002. }
  1003. /**
  1004. * This will crash if two arrays are not passed into the function.
  1005. * Used to test ZF-5388
  1006. */
  1007. public function testMultiArrayParamater($arrayOne, $arrayTwo)
  1008. {
  1009. return array_merge($arrayOne, $arrayTwo);
  1010. }
  1011. }
  1012. /**
  1013. * Class with private constructor
  1014. */
  1015. class Zend_Amf_testclassPrivate
  1016. {
  1017. private function __construct()
  1018. {
  1019. }
  1020. /**
  1021. * Test1
  1022. *
  1023. * Returns 'String: ' . $string
  1024. *
  1025. * @param string $string
  1026. * @return string
  1027. */
  1028. public function test1($string = '')
  1029. {
  1030. return 'String: '. (string) $string;
  1031. }
  1032. }
  1033. /**
  1034. * Example class for sending a session back to ActionScript.
  1035. */
  1036. class Zend_Amf_testSession
  1037. {
  1038. /** Check if the session is available or create it. */
  1039. public function __construct() {
  1040. if (!isset($_SESSION['count'])) {
  1041. $_SESSION['count'] = 0;
  1042. }
  1043. }
  1044. /** increment the current count session variable and return it's value */
  1045. public function getCount()
  1046. {
  1047. $_SESSION['count']++;
  1048. return $_SESSION['count'];
  1049. }
  1050. }