SmdTest.php 15 KB

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