PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Json/Server/SmdTest.php

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