RequestTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. // Call Zend_Json_Server_RequestTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_RequestTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Json/Server/Request.php';
  8. require_once 'Zend/Json.php';
  9. /**
  10. * Test class for Zend_Json_Server_Request
  11. */
  12. class Zend_Json_Server_RequestTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. require_once "PHPUnit/TextUI/TestRunner.php";
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_RequestTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. /**
  26. * Sets up the fixture, for example, open a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. $this->request = new Zend_Json_Server_Request();
  34. }
  35. /**
  36. * Tears down the fixture, for example, close a network connection.
  37. * This method is called after a test is executed.
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. }
  44. public function testShouldHaveNoParamsByDefault()
  45. {
  46. $params = $this->request->getParams();
  47. $this->assertTrue(empty($params));
  48. }
  49. public function testShouldBeAbleToAddAParamAsValueOnly()
  50. {
  51. $this->request->addParam('foo');
  52. $params = $this->request->getParams();
  53. $this->assertEquals(1, count($params));
  54. $test = array_shift($params);
  55. $this->assertEquals('foo', $test);
  56. }
  57. public function testShouldBeAbleToAddAParamAsKeyValuePair()
  58. {
  59. $this->request->addParam('bar', 'foo');
  60. $params = $this->request->getParams();
  61. $this->assertEquals(1, count($params));
  62. $this->assertTrue(array_key_exists('foo', $params));
  63. $this->assertEquals('bar', $params['foo']);
  64. }
  65. public function testInvalidKeysShouldBeIgnored()
  66. {
  67. $count = 0;
  68. foreach (array(array('foo', true), array('foo', new stdClass), array('foo', array())) as $spec) {
  69. $this->request->addParam($spec[0], $spec[1]);
  70. $this->assertNull($this->request->getParam('foo'));
  71. $params = $this->request->getParams();
  72. ++$count;
  73. $this->assertEquals($count, count($params));
  74. }
  75. }
  76. public function testShouldBeAbleToAddMultipleIndexedParamsAtOnce()
  77. {
  78. $params = array(
  79. 'foo',
  80. 'bar',
  81. 'baz',
  82. );
  83. $this->request->addParams($params);
  84. $test = $this->request->getParams();
  85. $this->assertSame($params, $test);
  86. }
  87. public function testShouldBeAbleToAddMultipleNamedParamsAtOnce()
  88. {
  89. $params = array(
  90. 'foo' => 'bar',
  91. 'bar' => 'baz',
  92. 'baz' => 'bat',
  93. );
  94. $this->request->addParams($params);
  95. $test = $this->request->getParams();
  96. $this->assertSame($params, $test);
  97. }
  98. public function testShouldBeAbleToAddMixedIndexedAndNamedParamsAtOnce()
  99. {
  100. $params = array(
  101. 'foo' => 'bar',
  102. 'baz',
  103. 'baz' => 'bat',
  104. );
  105. $this->request->addParams($params);
  106. $test = $this->request->getParams();
  107. $this->assertEquals(array_values($params), array_values($test));
  108. $this->assertTrue(array_key_exists('foo', $test));
  109. $this->assertTrue(array_key_exists('baz', $test));
  110. $this->assertTrue(in_array('baz', $test));
  111. }
  112. public function testSetParamsShouldOverwriteParams()
  113. {
  114. $this->testShouldBeAbleToAddMixedIndexedAndNamedParamsAtOnce();
  115. $params = array(
  116. 'one',
  117. 'two',
  118. 'three',
  119. );
  120. $this->request->setParams($params);
  121. $this->assertSame($params, $this->request->getParams());
  122. }
  123. public function testShouldBeAbleToRetrieveParamByKeyOrIndex()
  124. {
  125. $this->testShouldBeAbleToAddMixedIndexedAndNamedParamsAtOnce();
  126. $params = $this->request->getParams();
  127. $this->assertEquals('bar', $this->request->getParam('foo'), var_export($params, 1));
  128. $this->assertEquals('baz', $this->request->getParam(1), var_export($params, 1));
  129. $this->assertEquals('bat', $this->request->getParam('baz'), var_export($params, 1));
  130. }
  131. public function testMethodShouldBeNullByDefault()
  132. {
  133. $this->assertNull($this->request->getMethod());
  134. }
  135. public function testMethodErrorShouldBeFalseByDefault()
  136. {
  137. $this->assertFalse($this->request->isMethodError());
  138. }
  139. public function testMethodAccessorsShouldWorkUnderNormalInput()
  140. {
  141. $this->request->setMethod('foo');
  142. $this->assertEquals('foo', $this->request->getMethod());
  143. }
  144. public function testSettingMethodWithInvalidNameShouldSetError()
  145. {
  146. foreach (array('1ad', 'abc-123', 'ad$$832r#@') as $method) {
  147. $this->request->setMethod($method);
  148. $this->assertNull($this->request->getMethod());
  149. $this->assertTrue($this->request->isMethodError());
  150. }
  151. }
  152. public function testIdShouldBeNullByDefault()
  153. {
  154. $this->assertNull($this->request->getId());
  155. }
  156. public function testIdAccessorsShouldWorkUnderNormalInput()
  157. {
  158. $this->request->setId('foo');
  159. $this->assertEquals('foo', $this->request->getId());
  160. }
  161. public function testVersionShouldBeJsonRpcV1ByDefault()
  162. {
  163. $this->assertEquals('1.0', $this->request->getVersion());
  164. }
  165. public function testVersionShouldBeLimitedToV1AndV2()
  166. {
  167. $this->testVersionShouldBeJsonRpcV1ByDefault();
  168. $this->request->setVersion('2.0');
  169. $this->assertEquals('2.0', $this->request->getVersion());
  170. $this->request->setVersion('foo');
  171. $this->assertEquals('1.0', $this->request->getVersion());
  172. }
  173. public function testShouldBeAbleToLoadRequestFromJsonString()
  174. {
  175. $options = $this->getOptions();
  176. $json = Zend_Json::encode($options);
  177. $this->request->loadJson($json);
  178. $this->assertEquals('foo', $this->request->getMethod());
  179. $this->assertEquals('foobar', $this->request->getId());
  180. $this->assertEquals($options['params'], $this->request->getParams());
  181. }
  182. public function testLoadingFromJsonShouldSetJsonRpcVersionWhenPresent()
  183. {
  184. $options = $this->getOptions();
  185. $options['jsonrpc'] = '2.0';
  186. $json = Zend_Json::encode($options);
  187. $this->request->loadJson($json);
  188. $this->assertEquals('2.0', $this->request->getVersion());
  189. }
  190. public function testShouldBeAbleToCastToJson()
  191. {
  192. $options = $this->getOptions();
  193. $this->request->setOptions($options);
  194. $json = $this->request->toJson();
  195. $this->validateJson($json, $options);
  196. }
  197. public function testCastingToStringShouldCastToJson()
  198. {
  199. $options = $this->getOptions();
  200. $this->request->setOptions($options);
  201. $json = $this->request->__toString();
  202. $this->validateJson($json, $options);
  203. }
  204. /**
  205. * @group ZF-6187
  206. */
  207. public function testMethodNamesShouldAllowDotNamespacing()
  208. {
  209. $this->request->setMethod('foo.bar');
  210. $this->assertEquals('foo.bar', $this->request->getMethod());
  211. }
  212. public function getOptions()
  213. {
  214. return array(
  215. 'method' => 'foo',
  216. 'params' => array(
  217. 5,
  218. 'four',
  219. true,
  220. ),
  221. 'id' => 'foobar'
  222. );
  223. }
  224. public function validateJson($json, array $options)
  225. {
  226. $test = Zend_Json::decode($json);
  227. $this->assertTrue(is_array($test), var_export($json, 1));
  228. $this->assertTrue(array_key_exists('id', $test));
  229. $this->assertTrue(array_key_exists('method', $test));
  230. $this->assertTrue(array_key_exists('params', $test));
  231. $this->assertTrue(is_string($test['id']));
  232. $this->assertTrue(is_string($test['method']));
  233. $this->assertTrue(is_array($test['params']));
  234. $this->assertEquals($options['id'], $test['id']);
  235. $this->assertEquals($options['method'], $test['method']);
  236. $this->assertSame($options['params'], $test['params']);
  237. }
  238. }
  239. // Call Zend_Json_Server_RequestTest::main() if this source file is executed directly.
  240. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_RequestTest::main") {
  241. Zend_Json_Server_RequestTest::main();
  242. }