ServerTest.php 50 KB

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