PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/trunk/tests/Zend/Json/Server/RequestTest.php

http://firephp.googlecode.com/
PHP | 301 lines | 215 code | 35 blank | 51 comment | 3 complexity | 5e0984b4313a1c696eb5370c4a624621 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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. * @version $Id: RequestTest.php 23772 2011-02-28 21:35:29Z ralph $
  21. */
  22. // Call Zend_Json_Server_RequestTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_RequestTest::main");
  25. }
  26. require_once 'Zend/Json/Server/Request.php';
  27. require_once 'Zend/Json.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Json_Server
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Json
  35. * @group Zend_Json_Server
  36. */
  37. class Zend_Json_Server_RequestTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_RequestTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. $this->request = new Zend_Json_Server_Request();
  58. }
  59. /**
  60. * Tears down the fixture, for example, close a network connection.
  61. * This method is called after a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function tearDown()
  66. {
  67. }
  68. public function testShouldHaveNoParamsByDefault()
  69. {
  70. $params = $this->request->getParams();
  71. $this->assertTrue(empty($params));
  72. }
  73. public function testShouldBeAbleToAddAParamAsValueOnly()
  74. {
  75. $this->request->addParam('foo');
  76. $params = $this->request->getParams();
  77. $this->assertEquals(1, count($params));
  78. $test = array_shift($params);
  79. $this->assertEquals('foo', $test);
  80. }
  81. public function testShouldBeAbleToAddAParamAsKeyValuePair()
  82. {
  83. $this->request->addParam('bar', 'foo');
  84. $params = $this->request->getParams();
  85. $this->assertEquals(1, count($params));
  86. $this->assertTrue(array_key_exists('foo', $params));
  87. $this->assertEquals('bar', $params['foo']);
  88. }
  89. public function testInvalidKeysShouldBeIgnored()
  90. {
  91. $count = 0;
  92. foreach (array(array('foo', true), array('foo', new stdClass), array('foo', array())) as $spec) {
  93. $this->request->addParam($spec[0], $spec[1]);
  94. $this->assertNull($this->request->getParam('foo'));
  95. $params = $this->request->getParams();
  96. ++$count;
  97. $this->assertEquals($count, count($params));
  98. }
  99. }
  100. public function testShouldBeAbleToAddMultipleIndexedParamsAtOnce()
  101. {
  102. $params = array(
  103. 'foo',
  104. 'bar',
  105. 'baz',
  106. );
  107. $this->request->addParams($params);
  108. $test = $this->request->getParams();
  109. $this->assertSame($params, $test);
  110. }
  111. public function testShouldBeAbleToAddMultipleNamedParamsAtOnce()
  112. {
  113. $params = array(
  114. 'foo' => 'bar',
  115. 'bar' => 'baz',
  116. 'baz' => 'bat',
  117. );
  118. $this->request->addParams($params);
  119. $test = $this->request->getParams();
  120. $this->assertSame($params, $test);
  121. }
  122. public function testShouldBeAbleToAddMixedIndexedAndNamedParamsAtOnce()
  123. {
  124. $params = array(
  125. 'foo' => 'bar',
  126. 'baz',
  127. 'baz' => 'bat',
  128. );
  129. $this->request->addParams($params);
  130. $test = $this->request->getParams();
  131. $this->assertEquals(array_values($params), array_values($test));
  132. $this->assertTrue(array_key_exists('foo', $test));
  133. $this->assertTrue(array_key_exists('baz', $test));
  134. $this->assertTrue(in_array('baz', $test));
  135. }
  136. public function testSetParamsShouldOverwriteParams()
  137. {
  138. $this->testShouldBeAbleToAddMixedIndexedAndNamedParamsAtOnce();
  139. $params = array(
  140. 'one',
  141. 'two',
  142. 'three',
  143. );
  144. $this->request->setParams($params);
  145. $this->assertSame($params, $this->request->getParams());
  146. }
  147. public function testShouldBeAbleToRetrieveParamByKeyOrIndex()
  148. {
  149. $this->testShouldBeAbleToAddMixedIndexedAndNamedParamsAtOnce();
  150. $params = $this->request->getParams();
  151. $this->assertEquals('bar', $this->request->getParam('foo'), var_export($params, 1));
  152. $this->assertEquals('baz', $this->request->getParam(1), var_export($params, 1));
  153. $this->assertEquals('bat', $this->request->getParam('baz'), var_export($params, 1));
  154. }
  155. public function testMethodShouldBeNullByDefault()
  156. {
  157. $this->assertNull($this->request->getMethod());
  158. }
  159. public function testMethodErrorShouldBeFalseByDefault()
  160. {
  161. $this->assertFalse($this->request->isMethodError());
  162. }
  163. public function testMethodAccessorsShouldWorkUnderNormalInput()
  164. {
  165. $this->request->setMethod('foo');
  166. $this->assertEquals('foo', $this->request->getMethod());
  167. }
  168. public function testSettingMethodWithInvalidNameShouldSetError()
  169. {
  170. foreach (array('1ad', 'abc-123', 'ad$$832r#@') as $method) {
  171. $this->request->setMethod($method);
  172. $this->assertNull($this->request->getMethod());
  173. $this->assertTrue($this->request->isMethodError());
  174. }
  175. }
  176. public function testIdShouldBeNullByDefault()
  177. {
  178. $this->assertNull($this->request->getId());
  179. }
  180. public function testIdAccessorsShouldWorkUnderNormalInput()
  181. {
  182. $this->request->setId('foo');
  183. $this->assertEquals('foo', $this->request->getId());
  184. }
  185. public function testVersionShouldBeJsonRpcV1ByDefault()
  186. {
  187. $this->assertEquals('1.0', $this->request->getVersion());
  188. }
  189. public function testVersionShouldBeLimitedToV1AndV2()
  190. {
  191. $this->testVersionShouldBeJsonRpcV1ByDefault();
  192. $this->request->setVersion('2.0');
  193. $this->assertEquals('2.0', $this->request->getVersion());
  194. $this->request->setVersion('foo');
  195. $this->assertEquals('1.0', $this->request->getVersion());
  196. }
  197. public function testShouldBeAbleToLoadRequestFromJsonString()
  198. {
  199. $options = $this->getOptions();
  200. $json = Zend_Json::encode($options);
  201. $this->request->loadJson($json);
  202. $this->assertEquals('foo', $this->request->getMethod());
  203. $this->assertEquals('foobar', $this->request->getId());
  204. $this->assertEquals($options['params'], $this->request->getParams());
  205. }
  206. public function testLoadingFromJsonShouldSetJsonRpcVersionWhenPresent()
  207. {
  208. $options = $this->getOptions();
  209. $options['jsonrpc'] = '2.0';
  210. $json = Zend_Json::encode($options);
  211. $this->request->loadJson($json);
  212. $this->assertEquals('2.0', $this->request->getVersion());
  213. }
  214. public function testShouldBeAbleToCastToJson()
  215. {
  216. $options = $this->getOptions();
  217. $this->request->setOptions($options);
  218. $json = $this->request->toJson();
  219. $this->validateJson($json, $options);
  220. }
  221. public function testCastingToStringShouldCastToJson()
  222. {
  223. $options = $this->getOptions();
  224. $this->request->setOptions($options);
  225. $json = $this->request->__toString();
  226. $this->validateJson($json, $options);
  227. }
  228. /**
  229. * @group ZF-6187
  230. */
  231. public function testMethodNamesShouldAllowDotNamespacing()
  232. {
  233. $this->request->setMethod('foo.bar');
  234. $this->assertEquals('foo.bar', $this->request->getMethod());
  235. }
  236. public function getOptions()
  237. {
  238. return array(
  239. 'method' => 'foo',
  240. 'params' => array(
  241. 5,
  242. 'four',
  243. true,
  244. ),
  245. 'id' => 'foobar'
  246. );
  247. }
  248. public function validateJson($json, array $options)
  249. {
  250. $test = Zend_Json::decode($json);
  251. $this->assertTrue(is_array($test), var_export($json, 1));
  252. $this->assertTrue(array_key_exists('id', $test));
  253. $this->assertTrue(array_key_exists('method', $test));
  254. $this->assertTrue(array_key_exists('params', $test));
  255. $this->assertTrue(is_string($test['id']));
  256. $this->assertTrue(is_string($test['method']));
  257. $this->assertTrue(is_array($test['params']));
  258. $this->assertEquals($options['id'], $test['id']);
  259. $this->assertEquals($options['method'], $test['method']);
  260. $this->assertSame($options['params'], $test['params']);
  261. }
  262. }
  263. // Call Zend_Json_Server_RequestTest::main() if this source file is executed directly.
  264. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_RequestTest::main") {
  265. Zend_Json_Server_RequestTest::main();
  266. }