UtilityTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. namespace Tools\Test\TestCase\Utility;
  3. use Cake\Core\Configure;
  4. use Cake\Core\Plugin;
  5. use Tools\TestSuite\TestCase;
  6. use Tools\Utility\Utility;
  7. /**
  8. * @coversDefaultClass \Tools\Utility\Utility
  9. */
  10. class UtilityTest extends TestCase {
  11. /**
  12. * @return void
  13. */
  14. public function testNotBlank() {
  15. $res = Utility::notBlank('a');
  16. $this->assertTrue($res);
  17. $res = Utility::notBlank(2);
  18. $this->assertTrue($res);
  19. $res = Utility::notBlank(0);
  20. $this->assertTrue($res);
  21. $res = Utility::notBlank('0');
  22. $this->assertTrue($res);
  23. $res = Utility::notBlank(null);
  24. $this->assertFalse($res);
  25. $res = Utility::notBlank(false);
  26. $this->assertFalse($res);
  27. $res = Utility::notBlank('');
  28. $this->assertFalse($res);
  29. $res = Utility::notBlank([]);
  30. $this->assertFalse($res);
  31. }
  32. /**
  33. * @covers ::inArray
  34. * @return void
  35. */
  36. public function testInArray() {
  37. $res = Utility::inArray(2, [1, 2, 3]);
  38. $this->assertTrue($res);
  39. $res = Utility::inArray(4, [1, 2, 7]);
  40. $this->assertFalse($res);
  41. $res = Utility::inArray('2', [1, 2, 3]);
  42. $this->assertTrue($res);
  43. $res = Utility::inArray(2, ['1x', '2x', '3x']);
  44. $this->assertFalse($res);
  45. $res = Utility::inArray('3x', ['1x', '2x', '3x']);
  46. $this->assertTrue($res);
  47. $res = Utility::inArray(3, ['1', '2', '3']);
  48. $this->assertTrue($res);
  49. $res = Utility::inArray('2x', [1, 2, 3]);
  50. $this->assertFalse($res);
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function testTokenize() {
  56. $res = Utility::tokenize('');
  57. $this->assertSame([], $res);
  58. $res = Utility::tokenize('some');
  59. $this->assertSame(['some'], $res);
  60. $res = Utility::tokenize('some, thing');
  61. $this->assertSame(['some', 'thing'], $res);
  62. $res = Utility::tokenize(',some,,, ,, thing,');
  63. $this->assertSame(['some', 'thing'], array_values($res));
  64. }
  65. /**
  66. * @covers ::pregMatch
  67. * @return void
  68. */
  69. public function testPregMatch() {
  70. $string = '<abc>';
  71. preg_match('/\<(\w+)\>/', $string, $matches);
  72. $this->assertSame([$string, 'abc'], $matches);
  73. $matches = Utility::pregMatch('/\<(\w+)\>/', $string);
  74. $this->assertSame([$string, 'abc'], $matches);
  75. $string = '<äöü>';
  76. preg_match('/\<(.+)\>/', $string, $matches);
  77. $this->assertSame([$string, 'äöü'], $matches);
  78. $matches = Utility::pregMatch('/\<(.+)\>/', $string);
  79. $this->assertSame([$string, 'äöü'], $matches);
  80. $string = 'D-81245 München';
  81. preg_match('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches);
  82. $expected = [
  83. $string,
  84. 'D',
  85. '81245',
  86. 'München'
  87. ];
  88. $this->assertSame($expected, $matches);
  89. // we dont need the utf8 hack:
  90. $matches = Utility::pregMatch('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  91. $this->assertSame($expected, $matches);
  92. }
  93. /**
  94. * @covers ::pregMatch
  95. * @return void
  96. */
  97. public function testPregMatchWithPatternEscape() {
  98. $string = 'http://www.example.com/s?q=php.net+docs';
  99. $res = preg_quote($string);
  100. $this->assertSame('http\://www\.example\.com/s\?q\=php\.net\+docs', $res);
  101. $string = 'http://www.example.com/s?q=php.net+docs';
  102. $res = preg_quote($string, '/');
  103. $this->assertSame('http\:\/\/www\.example\.com\/s\?q\=php\.net\+docs', $res);
  104. $res = '/a\s*' . $res . '\s*z/i';
  105. $string = 'a ' . $string . ' z';
  106. $matches = Utility::pregMatch($res, $string);
  107. $expected = [$string];
  108. $this->assertSame($expected, $matches);
  109. }
  110. /**
  111. * @covers ::pregMatchAll
  112. * @return void
  113. */
  114. public function testPregMatchAll() {
  115. $string = 'D-81245 München';
  116. preg_match_all('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches, PREG_SET_ORDER);
  117. $expected = [
  118. [
  119. $string,
  120. 'D',
  121. '81245',
  122. 'München'
  123. ]
  124. ];
  125. $this->assertSame($expected, $matches);
  126. // we dont need the utf8 hack:
  127. $matches = Utility::pregMatchAll('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  128. $this->assertSame($expected, $matches);
  129. }
  130. /**
  131. * @covers ::strSplit
  132. * @return void
  133. */
  134. public function testStrSplit() {
  135. $res = str_split('some äöü string', 7);
  136. $expected = ['some äö', 'ü strin', 'g'];
  137. $this->assertNotSame($expected, $res);
  138. $res = Utility::strSplit('some äöü string', 7);
  139. $this->assertSame($expected, $res);
  140. }
  141. /**
  142. * @covers ::typeCast
  143. * @return void
  144. */
  145. public function testTypeCast() {
  146. $res = Utility::typeCast(2, 'string');
  147. $this->assertNotSame(2, $res);
  148. $this->assertSame('2', $res);
  149. }
  150. /**
  151. * @covers ::getClientIp
  152. * @return void
  153. */
  154. public function testGetClientIp() {
  155. $res = Utility::getClientIp();
  156. $this->assertSame((string)env('REMOTE_ADDR'), $res);
  157. }
  158. /**
  159. * @covers ::fileExists
  160. * @return void
  161. */
  162. public function testFileExists() {
  163. $res = Utility::fileExists('http://www.spiegel.de/static/sys/v10/icons/home_v2.png');
  164. $this->assertTrue($res);
  165. $res = Utility::fileExists(Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.jpg');
  166. $this->assertTrue($res);
  167. $res = Utility::fileExists('http://www.spiegel.de/static/sys/v10/icons/home_v2_inexistent.png');
  168. $this->assertFalse($res);
  169. $res = Utility::fileExists(Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'fooooo.jpg');
  170. $this->assertFalse($res);
  171. }
  172. /**
  173. * @covers ::urlExists
  174. * @return void
  175. */
  176. public function testUrlExists() {
  177. $res = Utility::urlExists('https://www.spiegel.de');
  178. $this->assertTrue($res);
  179. $res = Utility::urlExists('https://www.spiegel.de/some/inexistent.img');
  180. $this->assertFalse($res);
  181. }
  182. /**
  183. * @covers ::getReferer
  184. * @return void
  185. */
  186. public function testGetReferer() {
  187. $res = Utility::getReferer();
  188. $this->assertSame(env('HTTP_REFERER'), $res);
  189. $res = Utility::getReferer(true);
  190. $this->assertSame(env('HTTP_REFERER'), $res);
  191. $_SERVER['HTTP_REFERER'] = '/foo/bar';
  192. $res = Utility::getReferer(true);
  193. $base = Configure::read('App.fullBaseUrl');
  194. if (!$base) {
  195. $base = ''; //'http://localhost';
  196. }
  197. $this->assertSame($base . env('HTTP_REFERER'), $res);
  198. }
  199. /**
  200. * @covers ::getHeaderFromUrl
  201. * @return void
  202. */
  203. public function testGetHeaderFromUrl() {
  204. $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
  205. $this->assertTrue(is_array($res) && count($res) > 1);
  206. //$this->assertSame('HTTP/1.0 200 OK', $res[0]);
  207. }
  208. /**
  209. * @covers ::autoPrefixUrl
  210. * @return void
  211. */
  212. public function testAutoPrefixUrl() {
  213. $res = Utility::autoPrefixUrl('www.spiegel.de');
  214. $this->assertSame('http://www.spiegel.de', $res);
  215. }
  216. /**
  217. * @covers ::cleanUrl
  218. * @return void
  219. */
  220. public function testCleanUrl() {
  221. $res = Utility::cleanUrl('www.spiegel.de');
  222. $this->assertSame('http://www.spiegel.de', $res);
  223. $res = Utility::cleanUrl('http://');
  224. $this->assertSame('', $res);
  225. $res = Utility::cleanUrl('http://www');
  226. $this->assertSame('', $res);
  227. $res = Utility::cleanUrl('spiegel.de');
  228. $this->assertSame('http://spiegel.de', $res);
  229. $res = Utility::cleanUrl('spiegel.de', true);
  230. $this->assertSame('https://www.spiegel.de', $res);
  231. }
  232. /**
  233. * @return void
  234. */
  235. public function testStripUrl() {
  236. $urls = [
  237. 'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
  238. 'www.cakephp.org' => 'www.cakephp.org',
  239. 'https://spiegel.de' => 'spiegel.de',
  240. 'ftp://xyz' => 'ftp://xyz',
  241. ];
  242. foreach ($urls as $url => $expected) {
  243. $is = Utility::stripProtocol($url);
  244. $this->assertEquals($expected, $is, $url);
  245. }
  246. }
  247. /**
  248. * @covers ::trimDeep
  249. * @return void
  250. */
  251. public function testDeep() {
  252. $is = [
  253. 'f some',
  254. 'e 49r ' => 'rf r ',
  255. 'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]],
  256. 'bsh' => 1,
  257. 'bkd' => '1',
  258. 'bol' => true,
  259. 'bl' => 'true',
  260. 'flt' => 3.14,
  261. 'fl' => '3.14'
  262. ];
  263. $expected = [
  264. 'f some',
  265. 'e 49r ' => 'rf r',
  266. 'er' => [['ee' => ['rr ' => 'tt', 'empty' => null]]],
  267. 'bsh' => 1,
  268. 'bkd' => '1',
  269. 'bol' => true,
  270. 'bl' => 'true',
  271. 'flt' => 3.14,
  272. 'fl' => '3.14'
  273. ];
  274. $res = Utility::trimDeep($is);
  275. $this->assertSame($expected, $res);
  276. }
  277. /**
  278. * @covers ::trimDeep
  279. * @return void
  280. */
  281. public function testDeepTransformNullToString() {
  282. $is = [
  283. 'f some',
  284. 'e 49r ' => 'rf r ',
  285. 'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]]
  286. ];
  287. $expected = [
  288. 'f some',
  289. 'e 49r ' => 'rf r',
  290. 'er' => [['ee' => ['rr ' => 'tt', 'empty' => '']]]
  291. ];
  292. $res = Utility::trimDeep($is, true);
  293. $this->assertSame($expected, $res);
  294. }
  295. /**
  296. * //TODO
  297. *
  298. * @return void
  299. */
  300. public function _testDeepFunction() {
  301. $is = [
  302. 'f some',
  303. 'e 49r ' => 'rf r ',
  304. 'er' => [['ee' => ['rr ' => ' tt ']]]
  305. ];
  306. $expected = [
  307. 'f some',
  308. 'e 49r ' => 'rf r',
  309. 'er' => [['ee' => ['rr ' => 'tt']]]
  310. ];
  311. $result = Utility::deep('trim', $is);
  312. $this->assertSame($expected, $result);
  313. }
  314. /**
  315. * TestCountDim method
  316. *
  317. * @return void
  318. */
  319. public function testCountDim() {
  320. $data = ['one', '2', 'three'];
  321. $result = Utility::countDim($data);
  322. $this->assertSame(1, $result);
  323. $data = ['1' => '1.1', '2', '3'];
  324. $result = Utility::countDim($data);
  325. $this->assertSame(1, $result);
  326. $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => '3.1.1']];
  327. $result = Utility::countDim($data);
  328. $this->assertSame(2, $result);
  329. $data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']];
  330. $result = Utility::countDim($data);
  331. $this->assertSame(1, $result);
  332. $data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']];
  333. $result = Utility::countDim($data, true);
  334. $this->assertSame(2, $result);
  335. $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  336. $result = Utility::countDim($data);
  337. $this->assertSame(2, $result);
  338. $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  339. $result = Utility::countDim($data, true);
  340. $this->assertSame(3, $result);
  341. $data = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => '2.1.1.1']]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  342. $result = Utility::countDim($data, true);
  343. $this->assertSame(4, $result);
  344. $data = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => ['2.1.1.1']]]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  345. $result = Utility::countDim($data, true);
  346. $this->assertSame(5, $result);
  347. $data = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => ['2.1.1.1' => '2.1.1.1.1']]]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  348. $result = Utility::countDim($data, true);
  349. $this->assertSame(5, $result);
  350. $set = ['1' => ['1.1' => '1.1.1'], ['2' => ['2.1' => ['2.1.1' => ['2.1.1.1' => '2.1.1.1.1']]]], '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  351. $result = Utility::countDim($set, false, 0);
  352. $this->assertSame(2, $result);
  353. $result = Utility::countDim($set, true);
  354. $this->assertSame(5, $result);
  355. $data = ['one' => [null], ['null' => null], 'three' => [true, false, null]];
  356. $result = Utility::countDim($data, true);
  357. $this->assertSame(2, $result);
  358. }
  359. /**
  360. * @return void
  361. */
  362. public function testExpandList() {
  363. $is = [
  364. 'Some.Deep.Value1',
  365. 'Some.Deep.Value2',
  366. 'Some.Even.Deeper.Nested.Value',
  367. 'Empty.',
  368. '0.1.2',
  369. '.EmptyString'
  370. ];
  371. $result = Utility::expandList($is);
  372. $expected = [
  373. 'Some' => [
  374. 'Deep' => ['Value1', 'Value2'],
  375. 'Even' => ['Deeper' => ['Nested' => ['Value']]]
  376. ],
  377. 'Empty' => [''],
  378. '0' => ['1' => ['2']],
  379. '' => ['EmptyString']
  380. ];
  381. $this->assertSame($expected, $result);
  382. }
  383. /**
  384. * @expectedException \RuntimeException
  385. * @return void
  386. */
  387. public function testExpandListWithKeyLessListInvalid() {
  388. $is = [
  389. 'Some',
  390. 'ValueOnly',
  391. ];
  392. Utility::expandList($is);
  393. }
  394. /**
  395. * @return void
  396. */
  397. public function testExpandListWithKeyLessList() {
  398. $is = [
  399. 'Some',
  400. 'Thing',
  401. '.EmptyString'
  402. ];
  403. $result = Utility::expandList($is, '.', '');
  404. $expected = [
  405. '' => ['Some', 'Thing', 'EmptyString'],
  406. ];
  407. $this->assertSame($expected, $result);
  408. }
  409. /**
  410. * @return void
  411. */
  412. public function testFlatten() {
  413. $is = [
  414. 'Some' => [
  415. 'Deep' => ['Value1', 'Value2'],
  416. 'Even' => ['Deeper' => ['Nested' => ['Value']]]
  417. ],
  418. 'Empty' => [''],
  419. '0' => ['1' => ['2']],
  420. //'ValueOnly',
  421. '' => ['EmptyString']
  422. ];
  423. $result = Utility::flattenList($is);
  424. $expected = [
  425. 'Some.Deep.Value1',
  426. 'Some.Deep.Value2',
  427. 'Some.Even.Deeper.Nested.Value',
  428. 'Empty.',
  429. '0.1.2',
  430. //'1.ValueOnly'
  431. '.EmptyString'
  432. ];
  433. $this->assertSame($expected, $result);
  434. // Test integers als booleans
  435. $is = [
  436. 'Some' => [
  437. 'Deep' => [true],
  438. 'Even' => ['Deeper' => ['Nested' => [false, true]]]
  439. ],
  440. 'Integer' => ['Value' => [-3]],
  441. ];
  442. $result = Utility::flattenList($is);
  443. $expected = [
  444. 'Some.Deep.1',
  445. 'Some.Even.Deeper.Nested.0',
  446. 'Some.Even.Deeper.Nested.1',
  447. 'Integer.Value.-3',
  448. ];
  449. $this->assertSame($expected, $result);
  450. }
  451. /**
  452. * @covers ::arrayFlatten
  453. * @return void
  454. */
  455. public function testArrayFlattenBasic() {
  456. $strings = [
  457. 'a' => ['a' => 'A'],
  458. 'b' => ['b' => 'B', 'c' => 'C'],
  459. 'c' => [],
  460. 'd' => [[['z' => 'Z'], 'y' => 'Y']]
  461. ];
  462. $result = Utility::arrayFlatten($strings);
  463. $expected = [
  464. 'a' => 'A',
  465. 'b' => 'B',
  466. 'c' => 'C',
  467. 'z' => 'Z',
  468. 'y' => 'Y'
  469. ];
  470. $this->assertSame($expected, $result);
  471. }
  472. /**
  473. * Test that deeper nested values overwrite higher ones.
  474. *
  475. * @covers ::arrayFlatten
  476. * @return void
  477. */
  478. public function testArrayFlatten() {
  479. $array = [
  480. 'a' => 1,
  481. 'b' => ['h' => false, 'c' => ['d' => ['f' => 'g', 'h' => true]]],
  482. 'k' => 'm',
  483. ];
  484. $res = Utility::arrayFlatten($array);
  485. $expected = [
  486. 'a' => 1,
  487. 'h' => true,
  488. 'f' => 'g',
  489. 'k' => 'm',
  490. ];
  491. $this->assertSame($expected, $res);
  492. }
  493. /**
  494. * @covers ::arrayFlatten
  495. * @return void
  496. */
  497. public function testArrayFlattenAndPreserveKeys() {
  498. $array = [
  499. 0 => 1,
  500. 1 => ['c' => ['d' => ['g', 'h' => true]]],
  501. 2 => 'm',
  502. ];
  503. $res = Utility::arrayFlatten($array, true);
  504. $expected = [
  505. 0 => 'g',
  506. 'h' => true,
  507. 2 => 'm',
  508. ];
  509. $this->assertSame($expected, $res);
  510. }
  511. /**
  512. * @covers ::arrayShiftKeys
  513. * @return void
  514. */
  515. public function testArrayShiftKeys() {
  516. $array = [
  517. 'a' => 1,
  518. 'b' => ['c' => ['d' => ['f' => 'g', 'h' => true]]],
  519. 'k' => 'm',
  520. ];
  521. $res = Utility::arrayShiftKeys($array);
  522. $expected = 'a';
  523. $this->assertSame($expected, $res);
  524. $expected = [
  525. 'b' => ['c' => ['d' => ['f' => 'g', 'h' => true]]],
  526. 'k' => 'm',
  527. ];
  528. $this->assertSame($expected, $array);
  529. }
  530. /**
  531. * @covers ::returnElapsedTime
  532. * @return void
  533. */
  534. public function testTime() {
  535. Utility::startClock();
  536. time_nanosleep(0, 200000000);
  537. $res = Utility::returnElapsedTime();
  538. $this->assertTrue(round($res, 1) === 0.2);
  539. time_nanosleep(0, 100000000);
  540. $res = Utility::returnElapsedTime(8, true);
  541. $this->assertTrue(round($res, 1) === 0.3);
  542. time_nanosleep(0, 100000000);
  543. $res = Utility::returnElapsedTime();
  544. $this->assertTrue(round($res, 1) === 0.1);
  545. }
  546. /**
  547. * @covers ::logicalAnd
  548. * @return void
  549. */
  550. public function testLogicalAnd() {
  551. $array = [
  552. 'a' => 1,
  553. 'b' => 1,
  554. 'c' => 0,
  555. 'd' => 1,
  556. ];
  557. $is = Utility::logicalAnd($array);
  558. $this->assertFalse($is);
  559. $array = [
  560. 'a' => 1,
  561. 'b' => 1,
  562. 'c' => 1,
  563. 'd' => 1,
  564. ];
  565. $is = Utility::logicalAnd($array);
  566. $this->assertTrue($is);
  567. }
  568. /**
  569. * @covers ::logicalOr
  570. * @return void
  571. */
  572. public function testLogicalOr() {
  573. $array = [
  574. 'a' => 0,
  575. 'b' => 1,
  576. 'c' => 0,
  577. 'd' => 1,
  578. ];
  579. $is = Utility::logicalOr($array);
  580. $this->assertTrue($is);
  581. $array = [
  582. 'a' => 1,
  583. 'b' => 1,
  584. 'c' => 1,
  585. 'd' => 1,
  586. ];
  587. $is = Utility::logicalOr($array);
  588. $this->assertTrue($is);
  589. $array = [
  590. 'a' => 0,
  591. 'b' => 0,
  592. 'c' => 0,
  593. 'd' => 0,
  594. ];
  595. $is = Utility::logicalOr($array);
  596. $this->assertFalse($is);
  597. }
  598. }