UtilityTest.php 11 KB

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