ServerTest.php 48 KB

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