ServiceTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. // Call Zend_Json_Server_Smd_ServiceTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_Smd_ServiceTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  7. require_once 'Zend/Json/Server/Smd/Service.php';
  8. require_once 'Zend/Json/Server/Smd.php';
  9. require_once 'Zend/Json.php';
  10. /**
  11. * Test class for Zend_Json_Server_Smd_Service
  12. */
  13. class Zend_Json_Server_Smd_ServiceTest 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_Smd_ServiceTest");
  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->service = new Zend_Json_Server_Smd_Service('foo');
  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 testConstructorShouldThrowExceptionWhenNoNameSet()
  46. {
  47. try {
  48. $service = new Zend_Json_Server_Smd_Service(null);
  49. $this->fail('Should throw exception when no name set');
  50. } catch (Zend_Json_Server_Exception $e) {
  51. $this->assertContains('requires a name', $e->getMessage());
  52. }
  53. try {
  54. $service = new Zend_Json_Server_Smd_Service(array());
  55. $this->fail('Should throw exception when no name set');
  56. } catch (Zend_Json_Server_Exception $e) {
  57. $this->assertContains('requires a name', $e->getMessage());
  58. }
  59. }
  60. public function testSettingNameShouldThrowExceptionWhenContainingInvalidFormat()
  61. {
  62. try {
  63. $this->service->setName('0ab-?');
  64. $this->fail('Invalid name should throw exception');
  65. } catch (Zend_Json_Server_Exception $e) {
  66. $this->assertContains('Invalid name', $e->getMessage());
  67. }
  68. try {
  69. $this->service->setName('ab-?');
  70. $this->fail('Invalid name should throw exception');
  71. } catch (Zend_Json_Server_Exception $e) {
  72. $this->assertContains('Invalid name', $e->getMessage());
  73. }
  74. }
  75. public function testNameAccessorsShouldWorkWithNormalInput()
  76. {
  77. $this->assertEquals('foo', $this->service->getName());
  78. $this->service->setName('bar');
  79. $this->assertEquals('bar', $this->service->getName());
  80. }
  81. public function testTransportShouldDefaultToPost()
  82. {
  83. $this->assertEquals('POST', $this->service->getTransport());
  84. }
  85. public function testTransportShouldBeLimitedToPost()
  86. {
  87. try {
  88. $this->service->setTransport('GET');
  89. $this->fail('Invalid transport should throw exception');
  90. } catch (Zend_Json_Server_Exception $e) {
  91. $this->assertContains('Invalid transport', $e->getMessage());
  92. }
  93. try {
  94. $this->service->setTransport('REST');
  95. $this->fail('Invalid transport should throw exception');
  96. } catch (Zend_Json_Server_Exception $e) {
  97. $this->assertContains('Invalid transport', $e->getMessage());
  98. }
  99. }
  100. public function testTransportAccessorsShouldWorkUnderNormalInput()
  101. {
  102. $this->service->setTransport('POST');
  103. $this->assertEquals('POST', $this->service->getTransport());
  104. }
  105. public function testTargetShouldBeNullInitially()
  106. {
  107. $this->assertNull($this->service->getTarget());
  108. }
  109. public function testTargetAccessorsShouldWorkUnderNormalInput()
  110. {
  111. $this->testTargetShouldBeNullInitially();
  112. $this->service->setTarget('foo');
  113. $this->assertEquals('foo', $this->service->getTarget());
  114. }
  115. public function testTargetAccessorsShouldNormalizeToString()
  116. {
  117. $this->testTargetShouldBeNullInitially();
  118. $this->service->setTarget(123);
  119. $value = $this->service->getTarget();
  120. $this->assertTrue(is_string($value));
  121. $this->assertEquals((string) 123, $value);
  122. }
  123. public function testEnvelopeShouldBeJsonRpc1CompliantByDefault()
  124. {
  125. $this->assertEquals(Zend_Json_Server_Smd::ENV_JSONRPC_1, $this->service->getEnvelope());
  126. }
  127. public function testEnvelopeShouldOnlyComplyWithJsonRpc1And2()
  128. {
  129. $this->testEnvelopeShouldBeJsonRpc1CompliantByDefault();
  130. $this->service->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
  131. $this->assertEquals(Zend_Json_Server_Smd::ENV_JSONRPC_2, $this->service->getEnvelope());
  132. $this->service->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_1);
  133. $this->assertEquals(Zend_Json_Server_Smd::ENV_JSONRPC_1, $this->service->getEnvelope());
  134. try {
  135. $this->service->setEnvelope('JSON-P');
  136. $this->fail('Should not be able to set non-JSON-RPC spec envelopes');
  137. } catch (Zend_Json_Server_Exception $e) {
  138. $this->assertContains('Invalid envelope', $e->getMessage());
  139. }
  140. }
  141. public function testShouldHaveNoParamsByDefault()
  142. {
  143. $params = $this->service->getParams();
  144. $this->assertTrue(empty($params));
  145. }
  146. public function testShouldBeAbleToAddParamsByTypeOnly()
  147. {
  148. $this->service->addParam('integer');
  149. $params = $this->service->getParams();
  150. $this->assertEquals(1, count($params));
  151. $param = array_shift($params);
  152. $this->assertEquals('integer', $param['type']);
  153. }
  154. public function testParamsShouldAcceptArrayOfTypes()
  155. {
  156. $type = array('integer', 'string');
  157. $this->service->addParam($type);
  158. $params = $this->service->getParams();
  159. $param = array_shift($params);
  160. $test = $param['type'];
  161. $this->assertTrue(is_array($test));
  162. $this->assertEquals($type, $test);
  163. }
  164. public function testInvalidParamTypeShouldThrowException()
  165. {
  166. try {
  167. $this->service->addParam(new stdClass);
  168. $this->fail('Invalid param type should throw exception');
  169. } catch (Zend_Json_Server_Exception $e) {
  170. $this->assertContains('Invalid param type', $e->getMessage());
  171. }
  172. }
  173. public function testShouldBeAbleToOrderParams()
  174. {
  175. $this->service->addParam('integer', array(), 4)
  176. ->addParam('string')
  177. ->addParam('boolean', array(), 3);
  178. $params = $this->service->getParams();
  179. $this->assertEquals(3, count($params));
  180. $param = array_shift($params);
  181. $this->assertEquals('string', $param['type'], var_export($params, 1));
  182. $param = array_shift($params);
  183. $this->assertEquals('boolean', $param['type'], var_export($params, 1));
  184. $param = array_shift($params);
  185. $this->assertEquals('integer', $param['type'], var_export($params, 1));
  186. }
  187. public function testShouldBeAbleToAddArbitraryParamOptions()
  188. {
  189. $this->service->addParam(
  190. 'integer',
  191. array(
  192. 'name' => 'foo',
  193. 'optional' => false,
  194. 'default' => 1,
  195. 'description' => 'Foo parameter',
  196. )
  197. );
  198. $params = $this->service->getParams();
  199. $param = array_shift($params);
  200. $this->assertEquals('foo', $param['name']);
  201. $this->assertFalse($param['optional']);
  202. $this->assertEquals(1, $param['default']);
  203. $this->assertEquals('Foo parameter', $param['description']);
  204. }
  205. public function testShouldBeAbleToAddMultipleParamsAtOnce()
  206. {
  207. $this->service->addParams(array(
  208. array('type' => 'integer', 'order' => 4),
  209. array('type' => 'string', 'name' => 'foo'),
  210. array('type' => 'boolean', 'order' => 3),
  211. ));
  212. $params = $this->service->getParams();
  213. $this->assertEquals(3, count($params));
  214. $param = array_shift($params);
  215. $this->assertEquals('string', $param['type']);
  216. $this->assertEquals('foo', $param['name']);
  217. $param = array_shift($params);
  218. $this->assertEquals('boolean', $param['type']);
  219. $param = array_shift($params);
  220. $this->assertEquals('integer', $param['type']);
  221. }
  222. public function testSetparamsShouldOverwriteExistingParams()
  223. {
  224. $this->testShouldBeAbleToAddMultipleParamsAtOnce();
  225. $params = $this->service->getParams();
  226. $this->assertEquals(3, count($params));
  227. $this->service->setParams(array(
  228. array('type' => 'string'),
  229. array('type' => 'integer'),
  230. ));
  231. $test = $this->service->getParams();
  232. $this->assertNotEquals($params, $test);
  233. $this->assertEquals(2, count($test));
  234. }
  235. public function testReturnShouldBeNullByDefault()
  236. {
  237. $this->assertNull($this->service->getReturn());
  238. }
  239. public function testReturnAccessorsShouldWorkWithNormalInput()
  240. {
  241. $this->testReturnShouldBeNullByDefault();
  242. $this->service->setReturn('integer');
  243. $this->assertEquals('integer', $this->service->getReturn());
  244. }
  245. public function testReturnAccessorsShouldAllowArrayOfTypes()
  246. {
  247. $this->testReturnShouldBeNullByDefault();
  248. $type = array('integer', 'string');
  249. $this->service->setReturn($type);
  250. $this->assertEquals($type, $this->service->getReturn());
  251. }
  252. public function testInvalidReturnTypeShouldThrowException()
  253. {
  254. try {
  255. $this->service->setReturn(new stdClass);
  256. $this->fail('Invalid return type should throw exception');
  257. } catch (Zend_Json_Server_Exception $e) {
  258. $this->assertContains('Invalid param type', $e->getMessage());
  259. }
  260. }
  261. public function testToArrayShouldCreateSmdCompatibleHash()
  262. {
  263. $this->setupSmdValidationObject();
  264. $smd = $this->service->toArray();
  265. $this->validateSmdArray($smd);
  266. }
  267. public function testTojsonShouldEmitJson()
  268. {
  269. $this->setupSmdValidationObject();
  270. $json = $this->service->toJson();
  271. $smd = Zend_Json::decode($json);
  272. $this->assertTrue(array_key_exists('foo', $smd));
  273. $this->assertTrue(is_array($smd['foo']));
  274. $this->validateSmdArray($smd['foo']);
  275. }
  276. public function setupSmdValidationObject()
  277. {
  278. $this->service->setName('foo')
  279. ->setTransport('POST')
  280. ->setTarget('/foo')
  281. ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2)
  282. ->addParam('boolean')
  283. ->addParam('array')
  284. ->addParam('object')
  285. ->setReturn('boolean');
  286. }
  287. public function validateSmdArray(array $smd)
  288. {
  289. $this->assertTrue(array_key_exists('transport', $smd));
  290. $this->assertEquals('POST', $smd['transport']);
  291. $this->assertTrue(array_key_exists('envelope', $smd));
  292. $this->assertEquals(Zend_Json_Server_Smd::ENV_JSONRPC_2, $smd['envelope']);
  293. $this->assertTrue(array_key_exists('parameters', $smd));
  294. $params = $smd['parameters'];
  295. $this->assertEquals(3, count($params));
  296. $param = array_shift($params);
  297. $this->assertEquals('boolean', $param['type']);
  298. $param = array_shift($params);
  299. $this->assertEquals('array', $param['type']);
  300. $param = array_shift($params);
  301. $this->assertEquals('object', $param['type']);
  302. $this->assertTrue(array_key_exists('returns', $smd));
  303. $this->assertEquals('boolean', $smd['returns']);
  304. }
  305. }
  306. // Call Zend_Json_Server_Smd_ServiceTest::main() if this source file is executed directly.
  307. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_Smd_ServiceTest::main") {
  308. Zend_Json_Server_Smd_ServiceTest::main();
  309. }