UtilityTest.php 13 KB

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