You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhalApi_Request_Var_Test.php 14 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApiRequestVar_Test
  4. *
  5. * 针对 ../../PhalApi/Request/Var.php PhalApi_Request_Var 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20141012
  8. */
  9. require_once dirname(__FILE__) . '/../test_env.php';
  10. if (!class_exists('PhalApi_Request_Var')) {
  11. require dirname(__FILE__) . '/../../PhalApi/Request/Var.php';
  12. }
  13. class PhpUnderControl_PhalApiRequestVar_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $coreRequestVar;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $this->coreRequestVar = new PhalApi_Request_Var();
  20. }
  21. protected function tearDown()
  22. {
  23. }
  24. /**
  25. * @group testFormat
  26. */
  27. public function testFormat()
  28. {
  29. $varName = 'testKey';
  30. $rule = array('type' => 'int', 'default' => '2014');
  31. $params = array();
  32. $rs = PhalApi_Request_Var::format($varName, $rule, $params);
  33. $this->assertSame(2014, $rs);
  34. }
  35. /**
  36. * @group testFormatString
  37. */
  38. public function testFormatString()
  39. {
  40. $rs = PhalApi_Request_Var::format(
  41. 'testKey', array('name' => 'testKey'), array('testKey' => 2014));
  42. $this->assertSame('2014', $rs);
  43. }
  44. /**
  45. * @group testFormatStringMinMax
  46. */
  47. public function testFormatStringMinMax()
  48. {
  49. $rs = PhalApi_Request_Var::format(
  50. 'testKey', array('name' => 'testKey', "max" => 9, 'min' => 9, "format" => 'utf8'), array('testKey' => 'PhalApi测试'));
  51. $this->assertSame('PhalApi测试', $rs);
  52. }
  53. /**
  54. * @group testFormatStringMinMax
  55. * @expectedException PhalApi_Exception_BadRequest
  56. */
  57. public function testFormatStringExceptionMinMax()
  58. {
  59. $rs = PhalApi_Request_Var::format(
  60. 'testKey', array('name' => 'testKey', "max" => 8, 'min' => 8, "format" => 'utf8'), array('testKey' => 'PhalApi测试'));
  61. }
  62. /**
  63. * @group testFormatString
  64. * @expectedException PhalApi_Exception_InternalServerError
  65. */
  66. public function testFormatStringWithRuleExceptionMinGtMax()
  67. {
  68. $rs = PhalApi_Request_Var::format(
  69. 'testKey', array('name' => 'testKey', 'min' => 9, 'max' => 5), array('testKey' => '2014'));
  70. }
  71. /**
  72. * @group testFormatString
  73. * @expectedException PhalApi_Exception_BadRequest
  74. */
  75. public function testFormatStringWithParamExceptionLtMin()
  76. {
  77. $rs = PhalApi_Request_Var::format(
  78. 'testKey', array('name' => 'testKey', 'min' => 8), array('testKey' => 2014));
  79. }
  80. /**
  81. * @group testFormatString
  82. * @expectedException PhalApi_Exception_BadRequest
  83. */
  84. public function testFormatStringWithParamExceptionGtMax()
  85. {
  86. $value = '2014';
  87. $rule = array('name' => 'testKey', 'max' => 2, );
  88. $rs = PhalApi_Request_Var::format(
  89. 'testKey', array('name' => 'testKey', 'max' => 2), array('testKey' => 2014));
  90. }
  91. /**
  92. * @group testFormatInt
  93. */
  94. public function testFormatInt()
  95. {
  96. $rs = PhalApi_Request_Var::format(
  97. 'testKey', array('name' => 'testKey', 'type' => 'int'), array('testKey' => 2014));
  98. $this->assertSame(2014, $rs);
  99. }
  100. /**
  101. * @group testFormatFloat
  102. */
  103. public function testFormatFloat()
  104. {
  105. $rs = PhalApi_Request_Var::format(
  106. 'testKey', array('name' => 'testKey', 'type' => 'float'), array('testKey' => '3.14'));
  107. $this->assertSame(3.14, $rs);
  108. }
  109. /**
  110. * @dataProvider provideDataForFormatBoolean
  111. * @group testFormatBoolean
  112. */
  113. public function testFormatBoolean($oriValue, $expValue)
  114. {
  115. $rs = PhalApi_Request_Var::format(
  116. 'testKey', array('name' => 'testKey', 'type' => 'boolean'), array('testKey' => $oriValue));
  117. $this->assertSame($expValue, $rs);
  118. }
  119. public function provideDataForFormatBoolean()
  120. {
  121. return array(
  122. array('on', true),
  123. array('yes', true),
  124. array('true', true),
  125. array('success', true),
  126. array('false', false),
  127. array('1', true),
  128. );
  129. }
  130. /**
  131. * @group testFormatDate
  132. */
  133. public function testFormatDate()
  134. {
  135. $rs = PhalApi_Request_Var::format(
  136. 'testKey', array('name' => 'testKey', 'type' => 'date', 'format' => 'timestamp'), array('testKey' => '2014-10-01 12:00:00'));
  137. $this->assertTrue(is_numeric($rs));
  138. $this->assertSame(1412136000, $rs);
  139. }
  140. /**
  141. * @group testFormatDate
  142. */
  143. public function testFormatDateIllegal()
  144. {
  145. $rs = PhalApi_Request_Var::format(
  146. 'testKey', array('name' => 'testKey', 'type' => 'date', 'format' => 'timestamp'), array('testKey' => '2014-99-99 XX:XX:XX'));
  147. $this->assertEquals(0, $rs);
  148. }
  149. /**
  150. * @group testFormatDate
  151. * @expectedException PhalApi_Exception_BadRequest
  152. */
  153. public function testFormatDateRange()
  154. {
  155. $rs = PhalApi_Request_Var::format(
  156. 'testKey', array('name' => 'testKey', 'type' => 'date', 'format' => 'timestamp', 'max' => 100), array('testKey' => '2014-10-01 12:00:00'));
  157. }
  158. /**
  159. * @group testFormatArray
  160. */
  161. public function testFormatArrayWithJson()
  162. {
  163. $arr = array('age' => 100, 'sex' => 'male');
  164. $rs = PhalApi_Request_Var::format(
  165. 'testKey',
  166. array('name' => 'testKey', 'type' => 'array', 'format' => 'json'),
  167. array('testKey' => json_encode($arr))
  168. );
  169. $this->assertSame($arr, $rs);
  170. }
  171. public function testFormatArrayWithExplode()
  172. {
  173. $rs = PhalApi_Request_Var::format(
  174. 'testKey',
  175. array('name' => 'testKey', 'type' => 'array', 'format' => 'explode', 'separator' => '|'),
  176. array('testKey' => '1|2|3|4|5')
  177. );
  178. $this->assertEquals(array(1, 2, 3, 4, 5), $rs);
  179. }
  180. public function testFormatArrayDefault()
  181. {
  182. $rs = PhalApi_Request_Var::format(
  183. 'testKey',
  184. array('name' => 'testKey', 'type' => 'array'),
  185. array('testKey' => 'phalapi')
  186. );
  187. $this->assertEquals(array('phalapi'), $rs);
  188. }
  189. /**
  190. * @expectedException PhalApi_Exception_BadRequest
  191. */
  192. public function testFormatArrayRange()
  193. {
  194. $rs = PhalApi_Request_Var::format(
  195. 'testKey',
  196. array('name' => 'testKey', 'type' => 'array', 'format' => 'explode', 'separator' => '|', 'max' => 3),
  197. array('testKey' => '1|2|3|4|5')
  198. );
  199. }
  200. /**
  201. * @group testFile
  202. */
  203. public function testFormatFile()
  204. {
  205. $_FILES['aFile'] = array('name' => 'aHa~', 'type' => 'image/jpeg', 'size' => 100, 'tmp_name' => '/tmp/123456', 'error' => 0);
  206. $rule = array('name' => 'aFile', 'range' => array('image/jpeg'), 'min' => 50, 'max' => 1024, 'require' => true, 'default' => array(), 'type' => 'file');
  207. $rs = PhalApi_Request_Var::format('aFile', $rule, array());
  208. $this->assertEquals($_FILES['aFile'], $rs);
  209. }
  210. /**
  211. * @group testFile
  212. */
  213. public function testFormatFileInsensiveCase()
  214. {
  215. $_FILES['aFile'] = array('name' => 'aHa~', 'type' => 'image/jpeg', 'size' => 100, 'tmp_name' => '/tmp/123456', 'error' => 0);
  216. $rule = array('name' => 'aFile', 'range' => array('image/JPEG'), 'min' => 50, 'max' => 1024, 'require' => true, 'default' => array(), 'type' => 'file');
  217. $rs = PhalApi_Request_Var::format('aFile', $rule, array());
  218. $this->assertEquals($_FILES['aFile'], $rs);
  219. }
  220. /**
  221. * @group testFile
  222. * @expectedException PhalApi_Exception_BadRequest
  223. */
  224. public function testFormatFileButTooLarge()
  225. {
  226. $_FILES['aFile'] = array('name' => 'aHa~', 'type' => 'image/jpeg', 'size' => 9999, 'tmp_name' => '/tmp/123456', 'error' => 0);
  227. $rule = array('name' => 'aFile', 'range' => array('image/jpeg'), 'min' => 50, 'max' => 1024, 'require' => true, 'default' => array(), 'type' => 'file');
  228. $rs = PhalApi_Request_Var::format('aFile', $rule, array());
  229. }
  230. /**
  231. * @group testFile
  232. * @expectedException PhalApi_Exception_BadRequest
  233. */
  234. public function testFormatFileButWrongType()
  235. {
  236. $_FILES['aFile'] = array('name' => 'aHa~', 'type' => 'image/png', 'size' => 100, 'tmp_name' => '/tmp/123456', 'error' => 0);
  237. $rule = array('name' => 'aFile', 'range' => array('image/jpeg'), 'min' => 50, 'max' => 1024, 'require' => true, 'default' => array(), 'type' => 'file');
  238. $rs = PhalApi_Request_Var::format('aFile', $rule, array());
  239. }
  240. /**
  241. * @group testFile
  242. * @expectedException PhalApi_Exception_BadRequest
  243. */
  244. public function testFormatFileButError()
  245. {
  246. $_FILES['aFile'] = array('name' => 'aHa~', 'type' => 'image/png', 'size' => 100, 'tmp_name' => '/tmp/123456', 'error' => 2);
  247. $rule = array('name' => 'aFile', 'range' => array('image/jpeg'), 'min' => 50, 'max' => 1024, 'require' => true, 'default' => array(), 'type' => 'file');
  248. $rs = PhalApi_Request_Var::format('aFile', $rule, array());
  249. }
  250. /**
  251. * @group testFile
  252. * @expectedException PhalApi_Exception_BadRequest
  253. */
  254. public function testFormatFileEmptyButRequire()
  255. {
  256. $rule = array('name' => 'aFile', 'type' => 'file');
  257. $rs = PhalApi_Request_Var::format('aFile', $rule, array());
  258. $this->assertEquals(NULL, $rs);
  259. }
  260. /**
  261. * $group testFile
  262. */
  263. public function testFormatFileEmptyWithDefualt()
  264. {
  265. $default = array('name' => 'test.txt', 'type' => 'txt', 'tmp_name' => '/tmp/test.txt');
  266. $rule = array('name' => 'aFile', 'default' => $default, 'type' => 'file');
  267. $_FILES['aFile'] = null;
  268. $rs = PhalApi_Request_Var::format('aFile', $rule, array());
  269. $this->assertEquals($default, $rs);
  270. }
  271. /**
  272. * @group testFormatEnum
  273. */
  274. public function testFormatEnum()
  275. {
  276. $rs = PhalApi_Request_Var::format(
  277. 'testKey', array('range' => array('ios', 'android'), 'type' => 'enum'), array('testKey' => 'ios'));
  278. $this->assertSame('ios', $rs);
  279. }
  280. /**
  281. * @group testFormatEnum
  282. * @expectedException PhalApi_Exception_InternalServerError
  283. */
  284. public function testFormatEnumWithRuleException()
  285. {
  286. $rs = PhalApi_Request_Var::format(
  287. 'testKey', array('type' => 'enum', 'name' => 'testKey'), array('testKey' => 'ios'));
  288. }
  289. /**
  290. * @group testFormatEnum
  291. * @expectedException PhalApi_Exception_BadRequest
  292. */
  293. public function testFormatEnumWithParamException()
  294. {
  295. $rs = PhalApi_Request_Var::format(
  296. 'testKey', array('type' => 'enum', 'name' => 'testKey', 'range' => array('ios', 'android')), array('testKey' => 'pc'));
  297. }
  298. public function testFormatAllTypes()
  299. {
  300. $params = array(
  301. 'floatVal' => '1.0',
  302. 'booleanVal' => '1',
  303. 'dateVal' => '2015-02-05 00:00:00',
  304. 'arrayVal' => 'a,b,c',
  305. 'enumVal' => 'male',
  306. );
  307. $rule = array('name' => 'floatVal', 'type' => 'float');
  308. $rs = PhalApi_Request_Var::format('floatVal', $rule, $params);
  309. $this->assertSame(1.0, $rs);
  310. $rule = array('name' => 'booleanVal', 'type' => 'boolean');
  311. $rs = PhalApi_Request_Var::format('booleanVal', $rule, $params);
  312. $this->assertSame(true, $rs);
  313. $rule = array('name' => 'dateVal', 'type' => 'date', 'format' => 'timestamp');
  314. $rs = PhalApi_Request_Var::format('dateVal', $rule, $params);
  315. $this->assertSame( 1423065600, $rs);
  316. $rule = array('name' => 'arrayVal', 'type' => 'array', 'format' => 'explode');
  317. $rs = PhalApi_Request_Var::format('arrayVal', $rule, $params);
  318. $this->assertSame(array('a', 'b', 'c'), $rs);
  319. $rule = array('name' => 'enumVal', 'type' => 'enum', 'range' => array('female', 'male'));
  320. $rs = PhalApi_Request_Var::format('enumVal', $rule, $params);
  321. $this->assertSame('male', $rs);
  322. $rule = array('name' => 'noThisKey');
  323. $rs = PhalApi_Request_Var::format('noThisKey', $rule, $params);
  324. $this->assertSame(null, $rs);
  325. $rule = array('name' => 'noThisKey', 'type' => 'noThisType');
  326. $rs = PhalApi_Request_Var::format('noThisKey', $rule, $params);
  327. $this->assertSame(null, $rs);
  328. $_FILES['aFile'] = array('name' => 'aHa~', 'type' => 'image/jpeg', 'size' => 100, 'tmp_name' => '/tmp/123456', 'error' => 0);
  329. $rule = array('name' => 'aFile', 'range' => array('image/jpeg'), 'min' => 50, 'max' => 1024, 'require' => true, 'default' => array(), 'type' => 'file');
  330. $rs = PhalApi_Request_Var::format('aFile', $rule, $params);
  331. $this->assertNotEmpty($rs);
  332. }
  333. /**
  334. * @expectedException PhalApi_Exception_InternalServerError
  335. */
  336. public function testGetEnumWithEmptyRange()
  337. {
  338. PhalApi_Request_Var::format('key', array('name' => 'key', 'type' => 'enum', 'range' => array()), array('key' => 'aHa~'));
  339. }
  340. public function testStringWithRegxRight()
  341. {
  342. //very simple mobile phone
  343. $rule = array('name' => 'key', 'type' => 'string', 'regex' => '/^[0-9]{11}/');
  344. PhalApi_Request_Var::format('testKey', $rule, array('testKey' => '13800138000'));
  345. }
  346. /**
  347. * @expectedException PhalApi_Exception_BadRequest
  348. */
  349. public function testStringWithRegxWrong()
  350. {
  351. $rule = array('name' => 'key', 'type' => 'string', 'regex' => '/^[0-9]{11}/');
  352. PhalApi_Request_Var::format('key', $rule, array('key' => 'no a number'));
  353. }
  354. public function testFormatCallable()
  355. {
  356. $rs = PhalApi_Request_Var::format(
  357. 'testKey',
  358. array('name' => 'testKey', 'type' => 'callable', 'callback' => array('PhalApi_Request_Var_MyCallback', 'go')),
  359. array('testKey' => 1)
  360. );
  361. $this->assertSame(2, $rs);
  362. }
  363. /**
  364. * @expectedException PhalApi_Exception_InternalServerError
  365. */
  366. public function testFormatCallableButWroing()
  367. {
  368. $rs = PhalApi_Request_Var::format(
  369. 'testKey',
  370. array('name' => 'testKey', 'type' => 'callable', 'callback' => 'xxx'),
  371. array('testKey' => 1)
  372. );
  373. }
  374. }
  375. class PhalApi_Request_Var_MyCallback {
  376. public static function go($value, $rule) {
  377. return $value + 1;
  378. }
  379. }