ServiceTest.php 13 KB

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