ServiceTest.php 13 KB

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