UtilityTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. App::uses('Utility', 'Tools.Utility');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. /**
  5. * @covers Utility
  6. * 2012-02-21 ms
  7. */
  8. class UtilityTest extends MyCakeTestCase {
  9. /**
  10. * UtilityTest::testInArray()
  11. *
  12. * @covers Utility::inArray
  13. * @return void
  14. */
  15. public function testInArray() {
  16. $res = Utility::inArray(2, array(1, 2, 3));
  17. $this->assertTrue($res);
  18. $res = Utility::inArray(4, array(1, 2, 7));
  19. $this->assertFalse($res);
  20. $res = Utility::inArray('2', array(1, 2, 3));
  21. $this->assertTrue($res);
  22. $res = Utility::inArray(2, array('1x', '2x', '3x'));
  23. $this->assertFalse($res);
  24. $res = Utility::inArray('3x', array('1x', '2x', '3x'));
  25. $this->assertTrue($res);
  26. $res = Utility::inArray(3, array('1', '2', '3'));
  27. $this->assertTrue($res);
  28. $res = Utility::inArray('2x', array(1, 2, 3));
  29. $this->assertFalse($res);
  30. }
  31. /**
  32. * UtilityTest::testPregMatch()
  33. *
  34. * @covers Utility::pregMatch
  35. * @return void
  36. */
  37. public function testPregMatch() {
  38. $string = '<abc>';
  39. preg_match('/\<(\w+)\>/', $string, $matches);
  40. $this->assertSame(array($string, 'abc'), $matches);
  41. $matches = Utility::pregMatch('/\<(\w+)\>/', $string);
  42. $this->assertSame(array($string, 'abc'), $matches);
  43. $string = '<äöü>';
  44. preg_match('/\<(.+)\>/', $string, $matches);
  45. $this->assertSame(array($string, 'äöü'), $matches);
  46. $matches = Utility::pregMatch('/\<(.+)\>/', $string);
  47. $this->assertSame(array($string, 'äöü'), $matches);
  48. $string = 'D-81245 München';
  49. preg_match('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches);
  50. $expected = array(
  51. $string,
  52. 'D',
  53. '81245',
  54. 'München'
  55. );
  56. $this->assertSame($expected, $matches);
  57. // we dont need the utf8 hack:
  58. $matches = Utility::pregMatch('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  59. $this->assertSame($expected, $matches);
  60. }
  61. /**
  62. * UtilityTest::testPregMatchWithPatternEscape()
  63. *
  64. * @covers Utility::pregMatch
  65. * @return void
  66. */
  67. public function testPregMatchWithPatternEscape() {
  68. $string = 'http://www.example.com/s?q=php.net+docs';
  69. $res = preg_quote($string);
  70. $this->assertSame('http\://www\.example\.com/s\?q\=php\.net\+docs', $res);
  71. $string = 'http://www.example.com/s?q=php.net+docs';
  72. $res = preg_quote($string, '/');
  73. $this->assertSame('http\:\/\/www\.example\.com\/s\?q\=php\.net\+docs', $res);
  74. $res = '/a\s*' . $res . '\s*z/i';
  75. $string = 'a ' . $string . ' z';
  76. $matches = Utility::pregMatch($res, $string);
  77. $expected = array($string);
  78. $this->assertSame($expected, $matches);
  79. }
  80. /**
  81. * UtilityTest::testPregMatchAll()
  82. *
  83. * @covers Utility::pregMatchAll
  84. * @return void
  85. */
  86. public function testPregMatchAll() {
  87. $string = 'D-81245 München';
  88. preg_match_all('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches, PREG_SET_ORDER);
  89. $expected = array(
  90. array(
  91. $string,
  92. 'D',
  93. '81245',
  94. 'München'
  95. )
  96. );
  97. $this->assertSame($expected, $matches);
  98. // we dont need the utf8 hack:
  99. $matches = Utility::pregMatchAll('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  100. $this->assertSame($expected, $matches);
  101. }
  102. /**
  103. * UtilityTest::testStrSplit()
  104. *
  105. * @covers Utility::strSplit
  106. * @return void
  107. */
  108. public function testStrSplit() {
  109. $res = str_split('some äöü string', 7);
  110. $expected = array('some äö', 'ü strin', 'g');
  111. $this->assertNotSame($expected, $res);
  112. $res = Utility::strSplit('some äöü string', 7);
  113. $this->assertSame($expected, $res);
  114. }
  115. /**
  116. * UtilityTest::testUrlEncode()
  117. *
  118. * @covers Utility::urlEncode
  119. * @return void
  120. */
  121. public function testUrlEncode() {
  122. $res = Utility::urlEncode('Some/cool=value+more-infos');
  123. $this->assertSame('U29tZS9jb29sPXZhbHVlK21vcmUtaW5mb3M_', $res);
  124. }
  125. /**
  126. * UtilityTest::testUrlDecode()
  127. *
  128. * @covers Utility::urlDecode
  129. * @return void
  130. */
  131. public function testUrlDecode() {
  132. $res = Utility::urlDecode('U29tZS9jb29sPXZhbHVlK21vcmUtaW5mb3M_');
  133. $this->assertSame('Some/cool=value+more-infos', $res);
  134. }
  135. /**
  136. * UtilityTest::testTypeCast()
  137. *
  138. * @covers Utility::typeCast
  139. * @return void
  140. */
  141. public function testTypeCast() {
  142. $res = Utility::typeCast(2, 'string');
  143. $this->assertNotSame(2, $res);
  144. $this->assertSame('2', $res);
  145. }
  146. /**
  147. * UtilityTest::testGetClientIp()
  148. *
  149. * @covers Utility::getClientIp
  150. * @return void
  151. */
  152. public function testGetClientIp() {
  153. $res = Utility::getClientIp();
  154. $this->assertEquals(env('REMOTE_ADDR'), $res);
  155. }
  156. /**
  157. * UtilityTest::testGetReferer()
  158. *
  159. * @covers Utility::getReferer
  160. * @return void
  161. */
  162. public function testGetReferer() {
  163. $res = Utility::getReferer();
  164. $this->assertEquals(env('HTTP_REFERER'), $res);
  165. $res = Utility::getReferer(true);
  166. $this->assertEquals(env('HTTP_REFERER'), $res);
  167. $_SERVER['HTTP_REFERER'] = '/foo/bar';
  168. $res = Utility::getReferer(true);
  169. $base = HTTP_BASE;
  170. if (!$base) {
  171. $base = 'http://localhost';
  172. }
  173. $this->assertEquals($base . env('HTTP_REFERER'), $res);
  174. }
  175. /**
  176. * UtilityTest::testGetHeaderFromUrl()
  177. *
  178. * @covers Utility::getHeaderFromUrl
  179. * @return void
  180. */
  181. public function testGetHeaderFromUrl() {
  182. $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
  183. $this->assertTrue(is_array($res) && count($res) > 10);
  184. $this->assertEquals('HTTP/1.0 200 OK', $res[0]);
  185. }
  186. /**
  187. * UtilityTest::testAutoPrefixUrl()
  188. *
  189. * @covers Utility::autoPrefixUrl
  190. * @return void
  191. */
  192. public function testAutoPrefixUrl() {
  193. $res = Utility::autoPrefixUrl('www.spiegel.de');
  194. $this->assertEquals('http://www.spiegel.de', $res);
  195. }
  196. /**
  197. * UtilityTest::testCleanUrl()
  198. *
  199. * @covers Utility::cleanUrl
  200. * @return void
  201. */
  202. public function testCleanUrl() {
  203. $res = Utility::cleanUrl('www.spiegel.de');
  204. $this->assertEquals('http://www.spiegel.de', $res);
  205. $res = Utility::cleanUrl('http://');
  206. $this->assertEquals('', $res);
  207. $res = Utility::cleanUrl('http://www');
  208. $this->assertEquals('', $res);
  209. $res = Utility::cleanUrl('spiegel.de');
  210. $this->assertEquals('http://spiegel.de', $res);
  211. $res = Utility::cleanUrl('spiegel.de', true);
  212. //echo returns($res);
  213. $this->assertEquals('http://www.spiegel.de', $res);
  214. }
  215. /**
  216. * UtilityTest::testDeep()
  217. *
  218. * @covers Utility::trimDeep
  219. * @return void
  220. */
  221. public function testDeep() {
  222. $is = array(
  223. 'f some',
  224. 'e 49r ' => 'rf r ',
  225. 'er' => array(array('ee' => array('rr ' => ' tt ')))
  226. );
  227. $expected = array(
  228. 'f some',
  229. 'e 49r ' => 'rf r',
  230. 'er' => array(array('ee' => array('rr ' => 'tt')))
  231. );
  232. //$this->assertSame($is, $expected);
  233. $res = Utility::trimDeep($is);
  234. $this->assertSame($res, $expected);
  235. //$res = CommonComponent::trimDeep($is);
  236. //$this->assertSame($res, $expected);
  237. }
  238. //TODO: move to boostrap
  239. public function _testDeepFunction() {
  240. $is = array(
  241. 'f some',
  242. 'e 49r ' => 'rf r ',
  243. 'er' => array(array('ee' => array('rr ' => ' tt ')))
  244. );
  245. $expected = array(
  246. 'f some',
  247. 'e 49r ' => 'rf r',
  248. 'er' => array(array('ee' => array('rr ' => 'tt')))
  249. );
  250. $res = Utility::deep('trim', $is);
  251. $this->assertSame($res, $expected);
  252. }
  253. /**
  254. * UtilityTest::testArrayFlattenBasic()
  255. *
  256. * @covers Utility::arrayFlatten
  257. * @return void
  258. */
  259. public function testArrayFlattenBasic() {
  260. $strings = array(
  261. 'a' => array('a' => 'A'),
  262. 'b' => array('b' => 'B', 'c' => 'C'),
  263. 'c' => array(),
  264. 'd' => array(array(array('z' => 'Z'), 'y' => 'Y'))
  265. );
  266. $result = Utility::arrayFlatten($strings);
  267. $expected = array(
  268. 'a' => 'A',
  269. 'b' => 'B',
  270. 'c' => 'C',
  271. 'z' => 'Z',
  272. 'y' => 'Y'
  273. );
  274. $this->assertSame($expected, $result);
  275. }
  276. /**
  277. * UtilityTest::testArrayFlatten()
  278. *
  279. * @covers Utility::arrayFlatten
  280. * @return void
  281. */
  282. public function testArrayFlatten() {
  283. $array = array(
  284. 'a' => 1,
  285. 'b' => array('c' => array('d' => array('f' => 'g', 'h' => true))),
  286. 'k' => 'm',
  287. );
  288. $res = Utility::arrayFlatten($array);
  289. $expected = array(
  290. 'a' => 1,
  291. 'f' => 'g',
  292. 'h' => true,
  293. 'k' => 'm',
  294. );
  295. $this->assertSame($expected, $res);
  296. }
  297. /**
  298. * UtilityTest::testArrayFlattenAndPreserveKeys()
  299. *
  300. * @covers Utility::arrayFlatten
  301. * @return void
  302. */
  303. public function testArrayFlattenAndPreserveKeys() {
  304. $array = array(
  305. 0 => 1,
  306. 1 => array('c' => array('d' => array('g', 'h' => true))),
  307. 2 => 'm',
  308. );
  309. $res = Utility::arrayFlatten($array, true);
  310. $expected = array(
  311. 0 => 'g',
  312. 'h' => true,
  313. 2 => 'm',
  314. );
  315. $this->assertSame($expected, $res);
  316. }
  317. /**
  318. * UtilityTest::testArrayShiftKeys()
  319. *
  320. * @covers Utility::arrayShiftKeys
  321. * @return void
  322. */
  323. public function testArrayShiftKeys() {
  324. $array = array(
  325. 'a' => 1,
  326. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  327. 'k' => 'm',
  328. );
  329. $res = Utility::arrayShiftKeys($array);
  330. $expected = 'a';
  331. $this->assertSame($expected, $res);
  332. $expected = array(
  333. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  334. 'k' => 'm',
  335. );
  336. $this->assertSame($expected, $array);
  337. }
  338. /**
  339. * UtilityTest::testTime()
  340. *
  341. * @covers Utility::returnElapsedTime
  342. * @return void
  343. */
  344. public function testTime() {
  345. Utility::startClock();
  346. time_nanosleep(0, 200000000);
  347. $res = Utility::returnElapsedTime();
  348. $this->assertTrue(round($res, 1) === 0.2);
  349. time_nanosleep(0, 100000000);
  350. $res = Utility::returnElapsedTime(8, true);
  351. $this->assertTrue(round($res, 1) === 0.3);
  352. time_nanosleep(0, 100000000);
  353. $res = Utility::returnElapsedTime();
  354. $this->assertTrue(round($res, 1) === 0.1);
  355. }
  356. /**
  357. * UtilityTest::testLogicalAnd()
  358. *
  359. * @covers Utility::logicalAnd
  360. * @return void
  361. */
  362. public function testLogicalAnd() {
  363. $array = array(
  364. 'a' => 1,
  365. 'b' => 1,
  366. 'c' => 0,
  367. 'd' => 1,
  368. );
  369. $is = Utility::logicalAnd($array);
  370. $this->assertFalse($is);
  371. $array = array(
  372. 'a' => 1,
  373. 'b' => 1,
  374. 'c' => 1,
  375. 'd' => 1,
  376. );
  377. $is = Utility::logicalAnd($array);
  378. $this->assertTrue($is);
  379. }
  380. /**
  381. * UtilityTest::testLogicalOr()
  382. *
  383. * @covers Utility::logicalOr
  384. * @return void
  385. */
  386. public function testLogicalOr() {
  387. $array = array(
  388. 'a' => 0,
  389. 'b' => 1,
  390. 'c' => 0,
  391. 'd' => 1,
  392. );
  393. $is = Utility::logicalOr($array);
  394. $this->assertTrue($is);
  395. $array = array(
  396. 'a' => 1,
  397. 'b' => 1,
  398. 'c' => 1,
  399. 'd' => 1,
  400. );
  401. $is = Utility::logicalOr($array);
  402. $this->assertTrue($is);
  403. $array = array(
  404. 'a' => 0,
  405. 'b' => 0,
  406. 'c' => 0,
  407. 'd' => 0,
  408. );
  409. $is = Utility::logicalOr($array);
  410. $this->assertFalse($is);
  411. }
  412. /**
  413. * UtilityTest::testIsValidSaveAll()
  414. *
  415. * @covers Utility::isValidSaveAll
  416. * @return void
  417. */
  418. public function testIsValidSaveAll() {
  419. $result = Utility::isValidSaveAll(array());
  420. $this->assertFalse($result);
  421. $result = Utility::isValidSaveAll(array(true, true));
  422. $this->assertTrue($result);
  423. $result = Utility::isValidSaveAll(array(true, false));
  424. $this->assertFalse($result);
  425. }
  426. }