UtilityTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 = Configure::read('App.fullBaseUrl');
  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. * testCountDim method
  264. *
  265. * @return void
  266. */
  267. public function testCountDim() {
  268. $data = array('one', '2', 'three');
  269. $result = Utility::countDim($data);
  270. $this->assertEquals(1, $result);
  271. $data = array('1' => '1.1', '2', '3');
  272. $result = Utility::countDim($data);
  273. $this->assertEquals(1, $result);
  274. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  275. $result = Utility::countDim($data);
  276. $this->assertEquals(2, $result);
  277. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  278. $result = Utility::countDim($data);
  279. $this->assertEquals(1, $result);
  280. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  281. $result = Utility::countDim($data, true);
  282. $this->assertEquals(2, $result);
  283. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  284. $result = Utility::countDim($data);
  285. $this->assertEquals(2, $result);
  286. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  287. $result = Utility::countDim($data, true);
  288. $this->assertEquals(3, $result);
  289. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  290. $result = Utility::countDim($data, true);
  291. $this->assertEquals(4, $result);
  292. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  293. $result = Utility::countDim($data, true);
  294. $this->assertEquals(5, $result);
  295. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  296. $result = Utility::countDim($data, true);
  297. $this->assertEquals(5, $result);
  298. $set = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  299. $result = Utility::countDim($set, false, 0);
  300. $this->assertEquals(2, $result);
  301. $result = Utility::countDim($set, true);
  302. $this->assertEquals(5, $result);
  303. }
  304. /**
  305. * UtilityTest::testExpand()
  306. *
  307. * @return void
  308. */
  309. public function testExpandList() {
  310. $is = array(
  311. 'Some.Deep.Value1',
  312. 'Some.Deep.Value2',
  313. 'Some.Even.Deeper.Nested.Value',
  314. 'Empty.',
  315. '0.1.2',
  316. '.EmptyString'
  317. );
  318. $result = Utility::expandList($is);
  319. $expected = array(
  320. 'Some' => array(
  321. 'Deep' => array('Value1', 'Value2'),
  322. 'Even' => array('Deeper' => array('Nested' => array('Value')))
  323. ),
  324. 'Empty' => array(''),
  325. '0' => array('1' => array('2')),
  326. '' => array('EmptyString')
  327. );
  328. $this->assertSame($expected, $result);
  329. }
  330. /**
  331. * UtilityTest::testExpandListWithKeyLessListInvalid()
  332. *
  333. * @expectedException RuntimeException
  334. * @return void
  335. */
  336. public function testExpandListWithKeyLessListInvalid() {
  337. $is = array(
  338. 'Some',
  339. 'ValueOnly',
  340. );
  341. Utility::expandList($is);
  342. }
  343. /**
  344. * UtilityTest::testExpandListWithKeyLessList()
  345. *
  346. * @return void
  347. */
  348. public function testExpandListWithKeyLessList() {
  349. $is = array(
  350. 'Some',
  351. 'Thing',
  352. '.EmptyString'
  353. );
  354. $result = Utility::expandList($is, '.', '');
  355. $expected = array(
  356. '' => array('Some', 'Thing', 'EmptyString'),
  357. );
  358. $this->assertSame($expected, $result);
  359. }
  360. /**
  361. * UtilityTest::testFlatten()
  362. *
  363. * @return void
  364. */
  365. public function testFlatten() {
  366. $is = array(
  367. 'Some' => array(
  368. 'Deep' => array('Value1', 'Value2'),
  369. 'Even' => array('Deeper' => array('Nested' => array('Value')))
  370. ),
  371. 'Empty' => array(''),
  372. '0' => array('1' => array('2')),
  373. //'ValueOnly',
  374. '' => array('EmptyString')
  375. );
  376. $result = Utility::flattenList($is);
  377. $expected = array(
  378. 'Some.Deep.Value1',
  379. 'Some.Deep.Value2',
  380. 'Some.Even.Deeper.Nested.Value',
  381. 'Empty.',
  382. '0.1.2',
  383. //'1.ValueOnly'
  384. '.EmptyString'
  385. );
  386. $this->assertSame($expected, $result);
  387. // Test integers als booleans
  388. $is = array(
  389. 'Some' => array(
  390. 'Deep' => array(true),
  391. 'Even' => array('Deeper' => array('Nested' => array(false, true)))
  392. ),
  393. 'Integer' => array('Value' => array(-3)),
  394. );
  395. $result = Utility::flattenList($is);
  396. $expected = array(
  397. 'Some.Deep.1',
  398. 'Some.Even.Deeper.Nested.0',
  399. 'Some.Even.Deeper.Nested.1',
  400. 'Integer.Value.-3',
  401. );
  402. $this->assertSame($expected, $result);
  403. }
  404. /**
  405. * UtilityTest::testArrayFlattenBasic()
  406. *
  407. * @covers Utility::arrayFlatten
  408. * @return void
  409. */
  410. public function testArrayFlattenBasic() {
  411. $strings = array(
  412. 'a' => array('a' => 'A'),
  413. 'b' => array('b' => 'B', 'c' => 'C'),
  414. 'c' => array(),
  415. 'd' => array(array(array('z' => 'Z'), 'y' => 'Y'))
  416. );
  417. $result = Utility::arrayFlatten($strings);
  418. $expected = array(
  419. 'a' => 'A',
  420. 'b' => 'B',
  421. 'c' => 'C',
  422. 'z' => 'Z',
  423. 'y' => 'Y'
  424. );
  425. $this->assertSame($expected, $result);
  426. }
  427. /**
  428. * Test that deeper nested values overwrite higher ones.
  429. *
  430. * @covers Utility::arrayFlatten
  431. * @return void
  432. */
  433. public function testArrayFlatten() {
  434. $array = array(
  435. 'a' => 1,
  436. 'b' => array('h' => false, 'c' => array('d' => array('f' => 'g', 'h' => true))),
  437. 'k' => 'm',
  438. );
  439. $res = Utility::arrayFlatten($array);
  440. $expected = array(
  441. 'a' => 1,
  442. 'h' => true,
  443. 'f' => 'g',
  444. 'k' => 'm',
  445. );
  446. $this->assertSame($expected, $res);
  447. }
  448. /**
  449. * UtilityTest::testArrayFlattenAndPreserveKeys()
  450. *
  451. * @covers Utility::arrayFlatten
  452. * @return void
  453. */
  454. public function testArrayFlattenAndPreserveKeys() {
  455. $array = array(
  456. 0 => 1,
  457. 1 => array('c' => array('d' => array('g', 'h' => true))),
  458. 2 => 'm',
  459. );
  460. $res = Utility::arrayFlatten($array, true);
  461. $expected = array(
  462. 0 => 'g',
  463. 'h' => true,
  464. 2 => 'm',
  465. );
  466. $this->assertSame($expected, $res);
  467. }
  468. /**
  469. * UtilityTest::testArrayShiftKeys()
  470. *
  471. * @covers Utility::arrayShiftKeys
  472. * @return void
  473. */
  474. public function testArrayShiftKeys() {
  475. $array = array(
  476. 'a' => 1,
  477. 'b' => array('c' => array('d' => array('f' => 'g', 'h' => true))),
  478. 'k' => 'm',
  479. );
  480. $res = Utility::arrayShiftKeys($array);
  481. $expected = 'a';
  482. $this->assertSame($expected, $res);
  483. $expected = array(
  484. 'b' => array('c' => array('d' => array('f' => 'g', 'h' => true))),
  485. 'k' => 'm',
  486. );
  487. $this->assertSame($expected, $array);
  488. }
  489. /**
  490. * UtilityTest::testTime()
  491. *
  492. * @covers Utility::returnElapsedTime
  493. * @return void
  494. */
  495. public function testTime() {
  496. Utility::startClock();
  497. time_nanosleep(0, 200000000);
  498. $res = Utility::returnElapsedTime();
  499. $this->assertTrue(round($res, 1) === 0.2);
  500. time_nanosleep(0, 100000000);
  501. $res = Utility::returnElapsedTime(8, true);
  502. $this->assertTrue(round($res, 1) === 0.3);
  503. time_nanosleep(0, 100000000);
  504. $res = Utility::returnElapsedTime();
  505. $this->assertTrue(round($res, 1) === 0.1);
  506. }
  507. /**
  508. * UtilityTest::testLogicalAnd()
  509. *
  510. * @covers Utility::logicalAnd
  511. * @return void
  512. */
  513. public function testLogicalAnd() {
  514. $array = array(
  515. 'a' => 1,
  516. 'b' => 1,
  517. 'c' => 0,
  518. 'd' => 1,
  519. );
  520. $is = Utility::logicalAnd($array);
  521. $this->assertFalse($is);
  522. $array = array(
  523. 'a' => 1,
  524. 'b' => 1,
  525. 'c' => 1,
  526. 'd' => 1,
  527. );
  528. $is = Utility::logicalAnd($array);
  529. $this->assertTrue($is);
  530. }
  531. /**
  532. * UtilityTest::testLogicalOr()
  533. *
  534. * @covers Utility::logicalOr
  535. * @return void
  536. */
  537. public function testLogicalOr() {
  538. $array = array(
  539. 'a' => 0,
  540. 'b' => 1,
  541. 'c' => 0,
  542. 'd' => 1,
  543. );
  544. $is = Utility::logicalOr($array);
  545. $this->assertTrue($is);
  546. $array = array(
  547. 'a' => 1,
  548. 'b' => 1,
  549. 'c' => 1,
  550. 'd' => 1,
  551. );
  552. $is = Utility::logicalOr($array);
  553. $this->assertTrue($is);
  554. $array = array(
  555. 'a' => 0,
  556. 'b' => 0,
  557. 'c' => 0,
  558. 'd' => 0,
  559. );
  560. $is = Utility::logicalOr($array);
  561. $this->assertFalse($is);
  562. }
  563. /**
  564. * UtilityTest::testIsValidSaveAll()
  565. *
  566. * @covers Utility::isValidSaveAll
  567. * @return void
  568. */
  569. public function testIsValidSaveAll() {
  570. $result = Utility::isValidSaveAll(array());
  571. $this->assertFalse($result);
  572. $result = Utility::isValidSaveAll(array(true, true));
  573. $this->assertTrue($result);
  574. $result = Utility::isValidSaveAll(array(true, false));
  575. $this->assertFalse($result);
  576. }
  577. }