SmdTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_Json_Server
  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_Json_Server_SmdTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_SmdTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/Json/Server/Smd.php';
  28. require_once 'Zend/Json/Server/Smd/Service.php';
  29. require_once 'Zend/Json.php';
  30. /**
  31. * Test class for Zend_Json_Server_Smd
  32. *
  33. * @category Zend
  34. * @package Zend_Json_Server
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Json
  39. * @group Zend_Json_Server
  40. */
  41. class Zend_Json_Server_SmdTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @return void
  47. */
  48. public static function main()
  49. {
  50. require_once "PHPUnit/TextUI/TestRunner.php";
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_SmdTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. /**
  55. * Sets up the fixture, for example, open a network connection.
  56. * This method is called before a test is executed.
  57. *
  58. * @return void
  59. */
  60. public function setUp()
  61. {
  62. $this->smd = new Zend_Json_Server_Smd();
  63. }
  64. /**
  65. * Tears down the fixture, for example, close a network connection.
  66. * This method is called after a test is executed.
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. }
  73. public function testTransportShouldDefaultToPost()
  74. {
  75. $this->assertEquals('POST', $this->smd->getTransport());
  76. }
  77. public function testTransportAccessorsShouldWorkUnderNormalInput()
  78. {
  79. $this->smd->setTransport('POST');
  80. $this->assertEquals('POST', $this->smd->getTransport());
  81. }
  82. public function testTransportShouldBeLimitedToPost()
  83. {
  84. foreach (array('GET', 'REST') as $transport) {
  85. try {
  86. $this->smd->setTransport($transport);
  87. $this->fail('Invalid transport should throw exception');
  88. } catch (Zend_Json_Server_Exception $e) {
  89. $this->assertContains('Invalid transport', $e->getMessage());
  90. }
  91. }
  92. }
  93. public function testEnvelopeShouldDefaultToJsonRpcVersion1()
  94. {
  95. $this->assertEquals(Zend_Json_Server_Smd::ENV_JSONRPC_1, $this->smd->getEnvelope());
  96. }
  97. public function testEnvelopeAccessorsShouldWorkUnderNormalInput()
  98. {
  99. $this->testEnvelopeShouldDefaultToJsonRpcVersion1();
  100. $this->smd->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
  101. $this->assertEquals(Zend_Json_Server_Smd::ENV_JSONRPC_2, $this->smd->getEnvelope());
  102. $this->smd->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_1);
  103. $this->assertEquals(Zend_Json_Server_Smd::ENV_JSONRPC_1, $this->smd->getEnvelope());
  104. }
  105. public function testEnvelopeShouldBeLimitedToJsonRpcVersions()
  106. {
  107. foreach (array('URL', 'PATH', 'JSON') as $env) {
  108. try {
  109. $this->smd->setEnvelope($env);
  110. $this->fail('Invalid envelope type should throw exception');
  111. } catch (Zend_Json_Server_Exception $e) {
  112. $this->assertContains('Invalid envelope', $e->getMessage());
  113. }
  114. }
  115. }
  116. public function testContentTypeShouldDefaultToApplicationJson()
  117. {
  118. $this->assertEquals('application/json', $this->smd->getContentType());
  119. }
  120. public function testContentTypeAccessorsShouldWorkUnderNormalInput()
  121. {
  122. foreach (array('text/json', 'text/plain', 'application/x-json') as $type) {
  123. $this->smd->setContentType($type);
  124. $this->assertEquals($type, $this->smd->getContentType());
  125. }
  126. }
  127. public function testContentTypeShouldBeLimitedToMimeFormatStrings()
  128. {
  129. foreach (array('plain', 'json', 'foobar') as $type) {
  130. try {
  131. $this->smd->setContentType($type);
  132. $this->fail('Invalid content type should raise exception');
  133. } catch (Zend_Json_Server_Exception $e) {
  134. $this->assertContains('Invalid content type', $e->getMessage());
  135. }
  136. }
  137. }
  138. public function testTargetShouldDefaultToNull()
  139. {
  140. $this->assertNull($this->smd->getTarget());
  141. }
  142. public function testTargetAccessorsShouldWorkUnderNormalInput()
  143. {
  144. $this->testTargetShouldDefaultToNull();
  145. $this->smd->setTarget('foo');
  146. $this->assertEquals('foo', $this->smd->getTarget());
  147. }
  148. public function testIdShouldDefaultToNull()
  149. {
  150. $this->assertNull($this->smd->getId());
  151. }
  152. public function testIdAccessorsShouldWorkUnderNormalInput()
  153. {
  154. $this->testIdShouldDefaultToNull();
  155. $this->smd->setId('foo');
  156. $this->assertEquals('foo', $this->smd->getId());
  157. }
  158. public function testDescriptionShouldDefaultToNull()
  159. {
  160. $this->assertNull($this->smd->getDescription());
  161. }
  162. public function testDescriptionAccessorsShouldWorkUnderNormalInput()
  163. {
  164. $this->testDescriptionShouldDefaultToNull();
  165. $this->smd->setDescription('foo');
  166. $this->assertEquals('foo', $this->smd->getDescription());
  167. }
  168. public function testDojoCompatibilityShouldBeDisabledByDefault()
  169. {
  170. $this->assertFalse($this->smd->isDojoCompatible());
  171. }
  172. public function testDojoCompatibilityFlagShouldBeMutable()
  173. {
  174. $this->testDojoCompatibilityShouldBeDisabledByDefault();
  175. $this->smd->setDojoCompatible(true);
  176. $this->assertTrue($this->smd->isDojoCompatible());
  177. $this->smd->setDojoCompatible(false);
  178. $this->assertFalse($this->smd->isDojoCompatible());
  179. }
  180. public function testServicesShouldBeEmptyByDefault()
  181. {
  182. $services = $this->smd->getServices();
  183. $this->assertTrue(is_array($services));
  184. $this->assertTrue(empty($services));
  185. }
  186. public function testShouldBeAbleToUseServiceObjectToAddService()
  187. {
  188. $service = new Zend_Json_Server_Smd_Service('foo');
  189. $this->smd->addService($service);
  190. $this->assertSame($service, $this->smd->getService('foo'));
  191. }
  192. public function testShouldBeAbleToUseArrayToAddService()
  193. {
  194. $service = array(
  195. 'name' => 'foo',
  196. );
  197. $this->smd->addService($service);
  198. $foo = $this->smd->getService('foo');
  199. $this->assertTrue($foo instanceof Zend_Json_Server_Smd_Service);
  200. $this->assertEquals('foo', $foo->getName());
  201. }
  202. public function testAddingServiceWithExistingServiceNameShouldThrowException()
  203. {
  204. $service = new Zend_Json_Server_Smd_Service('foo');
  205. $this->smd->addService($service);
  206. $test = new Zend_Json_Server_Smd_Service('foo');
  207. try {
  208. $this->smd->addService($test);
  209. $this->fail('Adding service with existing service name should throw exception');
  210. } catch (Zend_Json_Server_Exception $e) {
  211. $this->assertContains('already register', $e->getMessage());
  212. }
  213. }
  214. public function testAttemptingToRegisterInvalidServiceShouldThrowException()
  215. {
  216. foreach (array('foo', false, 1, 1.0) as $service) {
  217. try {
  218. $this->smd->addService($service);
  219. $this->fail('Attempt to register invalid service should throw exception');
  220. } catch (Zend_Json_Server_Exception $e) {
  221. $this->assertContains('Invalid service', $e->getMessage());
  222. }
  223. }
  224. }
  225. public function testShouldBeAbleToAddManyServicesAtOnceWithArrayOfServiceObjects()
  226. {
  227. $one = new Zend_Json_Server_Smd_Service('one');
  228. $two = new Zend_Json_Server_Smd_Service('two');
  229. $three = new Zend_Json_Server_Smd_Service('three');
  230. $services = array($one, $two, $three);
  231. $this->smd->addServices($services);
  232. $test = $this->smd->getServices();
  233. $this->assertSame($services, array_values($test));
  234. }
  235. public function testShouldBeAbleToAddManyServicesAtOnceWithArrayOfArrays()
  236. {
  237. $services = array(
  238. array('name' => 'one'),
  239. array('name' => 'two'),
  240. array('name' => 'three'),
  241. );
  242. $this->smd->addServices($services);
  243. $test = $this->smd->getServices();
  244. $this->assertSame(array('one', 'two', 'three'), array_keys($test));
  245. }
  246. public function testShouldBeAbleToAddManyServicesAtOnceWithMixedArrayOfObjectsAndArrays()
  247. {
  248. $two = new Zend_Json_Server_Smd_Service('two');
  249. $services = array(
  250. array('name' => 'one'),
  251. $two,
  252. array('name' => 'three'),
  253. );
  254. $this->smd->addServices($services);
  255. $test = $this->smd->getServices();
  256. $this->assertSame(array('one', 'two', 'three'), array_keys($test));
  257. $this->assertEquals($two, $test['two']);
  258. }
  259. public function testSetServicesShouldOverwriteExistingServices()
  260. {
  261. $this->testShouldBeAbleToAddManyServicesAtOnceWithMixedArrayOfObjectsAndArrays();
  262. $five = new Zend_Json_Server_Smd_Service('five');
  263. $services = array(
  264. array('name' => 'four'),
  265. $five,
  266. array('name' => 'six'),
  267. );
  268. $this->smd->setServices($services);
  269. $test = $this->smd->getServices();
  270. $this->assertSame(array('four', 'five', 'six'), array_keys($test));
  271. $this->assertEquals($five, $test['five']);
  272. }
  273. public function testShouldBeAbleToRetrieveServiceByName()
  274. {
  275. $this->testShouldBeAbleToUseServiceObjectToAddService();
  276. }
  277. public function testShouldBeAbleToRemoveServiceByName()
  278. {
  279. $this->testShouldBeAbleToUseServiceObjectToAddService();
  280. $this->assertTrue($this->smd->removeService('foo'));
  281. $this->assertFalse($this->smd->getService('foo'));
  282. }
  283. public function testShouldBeAbleToCastToArray()
  284. {
  285. $options = $this->getOptions();
  286. $this->smd->setOptions($options);
  287. $service = $this->smd->toArray();
  288. $this->validateServiceArray($service, $options);
  289. }
  290. public function testShouldBeAbleToCastToDojoArray()
  291. {
  292. $options = $this->getOptions();
  293. $this->smd->setOptions($options);
  294. $smd = $this->smd->toDojoArray();
  295. $this->assertTrue(is_array($smd));
  296. $this->assertTrue(array_key_exists('SMDVersion', $smd));
  297. $this->assertTrue(array_key_exists('serviceType', $smd));
  298. $this->assertTrue(array_key_exists('methods', $smd));
  299. $this->assertEquals('.1', $smd['SMDVersion']);
  300. $this->assertEquals('JSON-RPC', $smd['serviceType']);
  301. $methods = $smd['methods'];
  302. $this->assertEquals(2, count($methods));
  303. $foo = array_shift($methods);
  304. $this->assertTrue(array_key_exists('name', $foo));
  305. $this->assertTrue(array_key_exists('serviceURL', $foo));
  306. $this->assertTrue(array_key_exists('parameters', $foo));
  307. $this->assertEquals('foo', $foo['name']);
  308. $this->assertEquals($this->smd->getTarget(), $foo['serviceURL']);
  309. $this->assertTrue(is_array($foo['parameters']));
  310. $this->assertEquals(1, count($foo['parameters']));
  311. $bar = array_shift($methods);
  312. $this->assertTrue(array_key_exists('name', $bar));
  313. $this->assertTrue(array_key_exists('serviceURL', $bar));
  314. $this->assertTrue(array_key_exists('parameters', $bar));
  315. $this->assertEquals('bar', $bar['name']);
  316. $this->assertEquals($this->smd->getTarget(), $bar['serviceURL']);
  317. $this->assertTrue(is_array($bar['parameters']));
  318. $this->assertEquals(1, count($bar['parameters']));
  319. }
  320. public function testShouldBeAbleToRenderAsJson()
  321. {
  322. $options = $this->getOptions();
  323. $this->smd->setOptions($options);
  324. $json = $this->smd->toJson();
  325. $smd = Zend_Json::decode($json);
  326. $this->validateServiceArray($smd, $options);
  327. }
  328. public function testToStringImplementationShouldProxyToJson()
  329. {
  330. $options = $this->getOptions();
  331. $this->smd->setOptions($options);
  332. $json = $this->smd->__toString();
  333. $smd = Zend_Json::decode($json);
  334. $this->validateServiceArray($smd, $options);
  335. }
  336. public function getOptions()
  337. {
  338. return array(
  339. 'target' => '/test/me',
  340. 'id' => '/test/me',
  341. 'services' => array(
  342. array(
  343. 'name' => 'foo',
  344. 'params' => array(
  345. array('type' => 'boolean'),
  346. ),
  347. 'return' => 'boolean',
  348. ),
  349. array(
  350. 'name' => 'bar',
  351. 'params' => array(
  352. array('type' => 'integer'),
  353. ),
  354. 'return' => 'string',
  355. ),
  356. )
  357. );
  358. }
  359. public function validateServiceArray(array $smd, array $options)
  360. {
  361. $this->assertTrue(is_array($smd));
  362. $this->assertTrue(array_key_exists('SMDVersion', $smd));
  363. $this->assertTrue(array_key_exists('target', $smd));
  364. $this->assertTrue(array_key_exists('id', $smd));
  365. $this->assertTrue(array_key_exists('transport', $smd));
  366. $this->assertTrue(array_key_exists('envelope', $smd));
  367. $this->assertTrue(array_key_exists('contentType', $smd));
  368. $this->assertTrue(array_key_exists('services', $smd));
  369. $this->assertEquals(Zend_Json_Server_Smd::SMD_VERSION, $smd['SMDVersion']);
  370. $this->assertEquals($options['target'], $smd['target']);
  371. $this->assertEquals($options['id'], $smd['id']);
  372. $this->assertEquals($this->smd->getTransport(), $smd['transport']);
  373. $this->assertEquals($this->smd->getEnvelope(), $smd['envelope']);
  374. $this->assertEquals($this->smd->getContentType(), $smd['contentType']);
  375. $services = $smd['services'];
  376. $this->assertEquals(2, count($services));
  377. $this->assertTrue(array_key_exists('foo', $services));
  378. $this->assertTrue(array_key_exists('bar', $services));
  379. }
  380. }
  381. // Call Zend_Json_Server_SmdTest::main() if this source file is executed directly.
  382. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_SmdTest::main") {
  383. Zend_Json_Server_SmdTest::main();
  384. }