SmdTest.php 14 KB

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