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.
 
 
 
 
 

368 lines
9.5 KiB

  1. <?php
  2. /**
  3. * *************************************************************************
  4. *
  5. * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  6. *
  7. * ************************************************************************
  8. */
  9. /**
  10. *
  11. * @file test_AssertHelper.php
  12. * @encoding UTF-8
  13. *
  14. *
  15. * @date 2014年12月31日
  16. *
  17. */
  18. if (! defined('PUSH_SDK_HOME')) {
  19. define('PUSH_SDK_HOME', dirname(dirname(__FILE__)));
  20. }
  21. include_once PUSH_SDK_HOME . '/lib/AssertHelper.php';
  22. class AssertHelperTest extends PHPUnit_Framework_TestCase {
  23. /**
  24. * @return AssertHelper
  25. */
  26. public function testCreateAssert() {
  27. return new AssertHelper();
  28. }
  29. /**
  30. * @depends testCreateAssert
  31. *
  32. * @param AssertHelper $ah
  33. */
  34. public function testType($ah) {
  35. echo 'check test Type!!!';
  36. $this -> assertTrue($ah -> type(101, 'int'));
  37. $this -> assertTrue($ah -> type(11, 'integer'));
  38. $this -> assertTrue($ah -> type(2.123, 'number'));
  39. $this -> assertTrue($ah -> type('abc', 'string'));
  40. $this -> assertTrue($ah -> type(true, 'bool'));
  41. $this -> assertTrue($ah -> type(false, 'boolean'));
  42. $this -> assertTrue($ah -> type(array (
  43. 1,
  44. 2,
  45. 3,
  46. ), 'array'));
  47. $this -> assertTrue($ah -> type(array (
  48. 'a' => 100,
  49. 'b' => 200,
  50. ), 'array'));
  51. $this -> assertTrue($ah -> type($ah, 'object'));
  52. $this -> assertTrue($ah -> type(null, 'null'));
  53. $this -> assertTrue($ah -> type('abc', array (
  54. 'string',
  55. 'array',
  56. )));
  57. $this -> assertTrue($ah -> type(array (
  58. 1,
  59. 2,
  60. 3,
  61. ), array (
  62. 'string',
  63. 'array',
  64. )));
  65. $this -> assertFalse($ah -> type(array (
  66. 1,
  67. 2,
  68. 3,
  69. ), array (
  70. 'string',
  71. 'number',
  72. )));
  73. $this -> assertFalse($ah -> type(array (
  74. 1,
  75. 2,
  76. 3,
  77. ), 'number'));
  78. }
  79. /**
  80. * @depends testCreateAssert
  81. *
  82. * @param AssertHelper $ah
  83. */
  84. public function testMoreThat($ah) {
  85. $this -> assertTrue($ah -> moreThat(101, 100));
  86. $this -> assertFalse($ah -> moreThat(99, 100));
  87. }
  88. /**
  89. * @depends testCreateAssert
  90. *
  91. * @param AssertHelper $ah
  92. */
  93. public function testLessThat($ah) {
  94. $this -> assertTrue($ah -> lessThat(99, 100));
  95. $this -> assertFalse($ah -> lessThat(101, 100));
  96. }
  97. /**
  98. * @depends testCreateAssert
  99. *
  100. * @param AssertHelper $ah
  101. */
  102. public function testBetween($ah) {
  103. $this -> assertTrue($ah -> between(100, 0, 200));
  104. $this -> assertFalse($ah -> between(101, 0, 100));
  105. $this -> assertTrue($ah -> between(0, 0, 100));
  106. $this -> assertTrue($ah -> between(100, 0, 100));
  107. }
  108. /**
  109. * @depends testCreateAssert
  110. *
  111. * @param AssertHelper $ah
  112. */
  113. public function testMatch($ah) {
  114. $this -> assertTrue($ah -> match(100, '/\d{3}/'));
  115. $this -> assertFalse($ah -> match('abc', '/\d{3}/'));
  116. }
  117. /**
  118. * @depends testCreateAssert
  119. *
  120. * @param AssertHelper $ah
  121. */
  122. public function testEqual($ah) {
  123. $this -> assertFalse($ah -> equal('100', 'abc'));
  124. $this -> assertTrue($ah -> equal('100', 100));
  125. }
  126. /**
  127. * @depends testCreateAssert
  128. *
  129. * @param AssertHelper $ah
  130. */
  131. public function testEqualStrict($ah) {
  132. $this -> assertFalse($ah -> equalStrict('100', 'abc'));
  133. $this -> assertFalse($ah -> equalStrict('100', 100));
  134. $this -> assertTrue($ah -> equalStrict('100', '100'));
  135. }
  136. /**
  137. * @depends testCreateAssert
  138. *
  139. * @param AssertHelper $ah
  140. */
  141. public function testMaybe($ah) {
  142. $this -> assertFalse($ah -> maybe('100', 'abc'));
  143. $this -> assertFalse($ah -> maybe('100', array (
  144. 'a',
  145. 'b',
  146. 'c',
  147. )));
  148. $this -> assertFalse($ah -> maybe(100, array (
  149. '100',
  150. '200',
  151. '300',
  152. )));
  153. $this -> assertTrue($ah -> maybe('100', array (
  154. '100',
  155. '200',
  156. '300',
  157. )));
  158. $this -> assertTrue($ah -> maybe('a', array (
  159. 'a',
  160. 'b',
  161. 'c',
  162. )));
  163. }
  164. /**
  165. * @depends testCreateAssert
  166. *
  167. * @param AssertHelper $ah
  168. */
  169. public function testNot($ah) {
  170. $this -> assertTrue($ah -> not('100', '100'));
  171. $this -> assertFalse($ah -> not(true));
  172. $this -> assertFalse($ah -> not(1));
  173. $this -> assertTrue($ah -> not(false));
  174. $this -> assertTrue($ah -> not(0));
  175. $this -> assertFalse($ah -> not('100', array (
  176. '100',
  177. '200',
  178. '300',
  179. )));
  180. $this -> assertTrue($ah -> not('123', array (
  181. '100',
  182. '200',
  183. '300',
  184. )));
  185. $this -> assertFalse($ah -> not('a', array (
  186. 'a',
  187. 'b',
  188. 'c',
  189. )));
  190. }
  191. /**
  192. * @depends testCreateAssert
  193. *
  194. * @param AssertHelper $ah
  195. */
  196. public function testPossible($ah) {
  197. $condition1 = array (
  198. 'lessThat' => array (
  199. 50,
  200. ),
  201. 'moreThat' => array (
  202. 150,
  203. ),
  204. "maybe" => array (
  205. 60,
  206. 70,
  207. 80,
  208. 90,
  209. ),
  210. );
  211. $this -> assertTrue($ah -> possible(100, '100'));
  212. $this -> assertTrue($ah -> possible(100, array (
  213. 100,
  214. 200,
  215. 300,
  216. 400,
  217. )));
  218. $this -> assertFalse($ah -> possible(91, $condition1));
  219. $this -> assertTrue($ah -> possible(90, $condition1));
  220. // 10 < value < 20 || 50 < vlaue < 60 || vlaue == unlimit
  221. $this -> assertFalse($ah -> possible(90, array (
  222. 'between' => array (
  223. 10,
  224. 20,
  225. ),
  226. ), array (
  227. 'between' => array (
  228. 50,
  229. 60,
  230. ),
  231. ), array (
  232. 'match' => '/unlimit/i',
  233. )));
  234. // vlaue == unlimit || 10 < value < 20 || 50 < vlaue < 60
  235. $this -> assertTrue($ah -> possible('unlimit', array (
  236. 'match' => 'unlimit',
  237. 'between' => array (
  238. 10,
  239. 20,
  240. ),
  241. ), array (
  242. 'between' => array (
  243. 50,
  244. 60,
  245. ),
  246. )));
  247. $arr = array (
  248. 'type' => 'array',
  249. 'match' => '/\d{10,25}/',
  250. );
  251. $this -> assertTrue($ah -> possible('123345456678879', $arr));
  252. }
  253. /**
  254. * @depends testCreateAssert
  255. *
  256. * @param AssertHelper $ah
  257. */
  258. public function testMakesure($ah) {
  259. $this -> assertTrue($ah -> makesure('100', '100'));
  260. $this -> assertTrue($ah -> makesure(true));
  261. $this -> assertTrue($ah -> makesure(1));
  262. $this -> assertFalse($ah -> makesure(false));
  263. $this -> assertFalse($ah -> makesure(0));
  264. // this is impossibility be true;
  265. $this -> assertFalse($ah -> makesure('100', array (
  266. '100',
  267. '200',
  268. '300',
  269. )));
  270. $this -> assertFalse($ah -> makesure('a', array (
  271. 'a',
  272. 'b',
  273. 'c',
  274. )));
  275. $this -> assertFalse($ah -> makesure(array (
  276. '100',
  277. '200',
  278. '300',
  279. ), array (
  280. 'type' => 'number',
  281. )));
  282. // complex
  283. $this -> assertTrue($ah -> makesure(100, array (
  284. 'between' => array (
  285. 50,
  286. 150,
  287. ),
  288. 'lessThat' => 150,
  289. 'moreThat' => 80,
  290. 'maybe' => array (
  291. 90,
  292. 100,
  293. 110,
  294. ),
  295. )));
  296. $this -> assertFalse($ah -> makesure(100, array (
  297. 'between' => array (
  298. 50,
  299. 150,
  300. ),
  301. 'lessThat' => 150,
  302. 'moreThat' => 80,
  303. 'maybe' => array (
  304. 90,
  305. 110,
  306. ),
  307. )));
  308. $this -> assertFalse($ah -> makesure(100, array (
  309. 'between' => array (
  310. 101,
  311. 150,
  312. ),
  313. 'lessThat' => 150,
  314. 'moreThat' => 80,
  315. 'maybe' => array (
  316. 90,
  317. 100,
  318. 110,
  319. ),
  320. )));
  321. $this -> assertFalse($ah -> makesure('100', array (
  322. 'match' => '/\d{3}/',
  323. 'not' => array (
  324. '100',
  325. '200',
  326. '300',
  327. ),
  328. )));
  329. $this -> assertTrue($ah -> makesure('123', array (
  330. 'match' => '/\d{3}/',
  331. 'not' => array (
  332. '100',
  333. '200',
  334. '300',
  335. ),
  336. )));
  337. $this -> assertTrue($ah -> makesure(array("9044331805861663059", "8669180648405502819", "3333449854796766867", "5861757435658053859",), array (
  338. 'type' => array (
  339. 'array',
  340. 'string'
  341. )
  342. )));
  343. }
  344. }