UtilityTest.php 16 KB

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