UtilityTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. Configure::write('App.fullBaseUrl', 'http://foo.bar');
  188. $res = Utility::getReferer();
  189. $this->assertSame(env('HTTP_REFERER'), $res);
  190. $res = Utility::getReferer(true);
  191. $this->assertSame(env('HTTP_REFERER'), $res);
  192. $_SERVER['HTTP_REFERER'] = '/foo/bar';
  193. $res = Utility::getReferer(true);
  194. $base = Configure::read('App.fullBaseUrl');
  195. $this->assertSame($base . env('HTTP_REFERER'), $res);
  196. }
  197. /**
  198. * @covers ::getHeaderFromUrl
  199. * @return void
  200. */
  201. public function testGetHeaderFromUrl() {
  202. $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
  203. $this->assertTrue(is_array($res) && count($res) > 1);
  204. //$this->assertSame('HTTP/1.0 200 OK', $res[0]);
  205. }
  206. /**
  207. * @covers ::autoPrefixUrl
  208. * @return void
  209. */
  210. public function testAutoPrefixUrl() {
  211. $res = Utility::autoPrefixUrl('www.spiegel.de');
  212. $this->assertSame('http://www.spiegel.de', $res);
  213. }
  214. /**
  215. * @covers ::cleanUrl
  216. * @return void
  217. */
  218. public function testCleanUrl() {
  219. $res = Utility::cleanUrl('www.spiegel.de');
  220. $this->assertSame('http://www.spiegel.de', $res);
  221. $res = Utility::cleanUrl('http://');
  222. $this->assertSame('', $res);
  223. $res = Utility::cleanUrl('http://www');
  224. $this->assertSame('', $res);
  225. $res = Utility::cleanUrl('spiegel.de');
  226. $this->assertSame('http://spiegel.de', $res);
  227. $res = Utility::cleanUrl('spiegel.de', true);
  228. $this->assertSame('https://www.spiegel.de', $res);
  229. }
  230. /**
  231. * @return void
  232. */
  233. public function testStripUrl() {
  234. $urls = [
  235. 'http://www.cakephp.org/bla/bla' => 'www.cakephp.org/bla/bla',
  236. 'www.cakephp.org' => 'www.cakephp.org',
  237. 'https://spiegel.de' => 'spiegel.de',
  238. 'ftp://xyz' => 'ftp://xyz',
  239. ];
  240. foreach ($urls as $url => $expected) {
  241. $is = Utility::stripProtocol($url);
  242. $this->assertEquals($expected, $is, $url);
  243. }
  244. }
  245. /**
  246. * @covers ::trimDeep
  247. * @return void
  248. */
  249. public function testDeep() {
  250. $is = [
  251. 'f some',
  252. 'e 49r ' => 'rf r ',
  253. 'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]],
  254. 'bsh' => 1,
  255. 'bkd' => '1',
  256. 'bol' => true,
  257. 'bl' => 'true',
  258. 'flt' => 3.14,
  259. 'fl' => '3.14'
  260. ];
  261. $expected = [
  262. 'f some',
  263. 'e 49r ' => 'rf r',
  264. 'er' => [['ee' => ['rr ' => 'tt', 'empty' => null]]],
  265. 'bsh' => 1,
  266. 'bkd' => '1',
  267. 'bol' => true,
  268. 'bl' => 'true',
  269. 'flt' => 3.14,
  270. 'fl' => '3.14'
  271. ];
  272. $res = Utility::trimDeep($is);
  273. $this->assertSame($expected, $res);
  274. }
  275. /**
  276. * @covers ::trimDeep
  277. * @return void
  278. */
  279. public function testDeepTransformNullToString() {
  280. $is = [
  281. 'f some',
  282. 'e 49r ' => 'rf r ',
  283. 'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]]
  284. ];
  285. $expected = [
  286. 'f some',
  287. 'e 49r ' => 'rf r',
  288. 'er' => [['ee' => ['rr ' => 'tt', 'empty' => '']]]
  289. ];
  290. $res = Utility::trimDeep($is, true);
  291. $this->assertSame($expected, $res);
  292. }
  293. /**
  294. * //TODO
  295. *
  296. * @return void
  297. */
  298. public function _testDeepFunction() {
  299. $is = [
  300. 'f some',
  301. 'e 49r ' => 'rf r ',
  302. 'er' => [['ee' => ['rr ' => ' tt ']]]
  303. ];
  304. $expected = [
  305. 'f some',
  306. 'e 49r ' => 'rf r',
  307. 'er' => [['ee' => ['rr ' => 'tt']]]
  308. ];
  309. $result = Utility::deep('trim', $is);
  310. $this->assertSame($expected, $result);
  311. }
  312. /**
  313. * TestCountDim method
  314. *
  315. * @return void
  316. */
  317. public function testCountDim() {
  318. $data = ['one', '2', 'three'];
  319. $result = Utility::countDim($data);
  320. $this->assertSame(1, $result);
  321. $data = ['1' => '1.1', '2', '3'];
  322. $result = Utility::countDim($data);
  323. $this->assertSame(1, $result);
  324. $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => '3.1.1']];
  325. $result = Utility::countDim($data);
  326. $this->assertSame(2, $result);
  327. $data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']];
  328. $result = Utility::countDim($data);
  329. $this->assertSame(1, $result);
  330. $data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']];
  331. $result = Utility::countDim($data, true);
  332. $this->assertSame(2, $result);
  333. $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  334. $result = Utility::countDim($data);
  335. $this->assertSame(2, $result);
  336. $data = ['1' => ['1.1' => '1.1.1'], '2', '3' => ['3.1' => ['3.1.1' => '3.1.1.1']]];
  337. $result = Utility::countDim($data, true);
  338. $this->assertSame(3, $result);
  339. $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']]];
  340. $result = Utility::countDim($data, true);
  341. $this->assertSame(4, $result);
  342. $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']]];
  343. $result = Utility::countDim($data, true);
  344. $this->assertSame(5, $result);
  345. $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']]];
  346. $result = Utility::countDim($data, true);
  347. $this->assertSame(5, $result);
  348. $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']]];
  349. $result = Utility::countDim($set, false, 0);
  350. $this->assertSame(2, $result);
  351. $result = Utility::countDim($set, true);
  352. $this->assertSame(5, $result);
  353. $data = ['one' => [null], ['null' => null], 'three' => [true, false, null]];
  354. $result = Utility::countDim($data, true);
  355. $this->assertSame(2, $result);
  356. }
  357. /**
  358. * @return void
  359. */
  360. public function testExpandList() {
  361. $is = [
  362. 'Some.Deep.Value1',
  363. 'Some.Deep.Value2',
  364. 'Some.Even.Deeper.Nested.Value',
  365. 'Empty.',
  366. '0.1.2',
  367. '.EmptyString'
  368. ];
  369. $result = Utility::expandList($is);
  370. $expected = [
  371. 'Some' => [
  372. 'Deep' => ['Value1', 'Value2'],
  373. 'Even' => ['Deeper' => ['Nested' => ['Value']]]
  374. ],
  375. 'Empty' => [''],
  376. '0' => ['1' => ['2']],
  377. '' => ['EmptyString']
  378. ];
  379. $this->assertSame($expected, $result);
  380. }
  381. /**
  382. * @return void
  383. */
  384. public function testExpandListWithKeyLessListInvalid() {
  385. $is = [
  386. 'Some',
  387. 'ValueOnly',
  388. ];
  389. $this->expectException(\RuntimeException::class);
  390. Utility::expandList($is);
  391. }
  392. /**
  393. * @return void
  394. */
  395. public function testExpandListWithKeyLessList() {
  396. $is = [
  397. 'Some',
  398. 'Thing',
  399. '.EmptyString'
  400. ];
  401. $result = Utility::expandList($is, '.', '');
  402. $expected = [
  403. '' => ['Some', 'Thing', 'EmptyString'],
  404. ];
  405. $this->assertSame($expected, $result);
  406. }
  407. /**
  408. * @return void
  409. */
  410. public function testFlatten() {
  411. $is = [
  412. 'Some' => [
  413. 'Deep' => ['Value1', 'Value2'],
  414. 'Even' => ['Deeper' => ['Nested' => ['Value']]]
  415. ],
  416. 'Empty' => [''],
  417. '0' => ['1' => ['2']],
  418. //'ValueOnly',
  419. '' => ['EmptyString']
  420. ];
  421. $result = Utility::flattenList($is);
  422. $expected = [
  423. 'Some.Deep.Value1',
  424. 'Some.Deep.Value2',
  425. 'Some.Even.Deeper.Nested.Value',
  426. 'Empty.',
  427. '0.1.2',
  428. //'1.ValueOnly'
  429. '.EmptyString'
  430. ];
  431. $this->assertSame($expected, $result);
  432. // Test integers als booleans
  433. $is = [
  434. 'Some' => [
  435. 'Deep' => [true],
  436. 'Even' => ['Deeper' => ['Nested' => [false, true]]]
  437. ],
  438. 'Integer' => ['Value' => [-3]],
  439. ];
  440. $result = Utility::flattenList($is);
  441. $expected = [
  442. 'Some.Deep.1',
  443. 'Some.Even.Deeper.Nested.0',
  444. 'Some.Even.Deeper.Nested.1',
  445. 'Integer.Value.-3',
  446. ];
  447. $this->assertSame($expected, $result);
  448. }
  449. /**
  450. * @covers ::arrayFlatten
  451. * @return void
  452. */
  453. public function testArrayFlattenBasic() {
  454. $strings = [
  455. 'a' => ['a' => 'A'],
  456. 'b' => ['b' => 'B', 'c' => 'C'],
  457. 'c' => [],
  458. 'd' => [[['z' => 'Z'], 'y' => 'Y']]
  459. ];
  460. $result = Utility::arrayFlatten($strings);
  461. $expected = [
  462. 'a' => 'A',
  463. 'b' => 'B',
  464. 'c' => 'C',
  465. 'z' => 'Z',
  466. 'y' => 'Y'
  467. ];
  468. $this->assertSame($expected, $result);
  469. }
  470. /**
  471. * Test that deeper nested values overwrite higher ones.
  472. *
  473. * @covers ::arrayFlatten
  474. * @return void
  475. */
  476. public function testArrayFlatten() {
  477. $array = [
  478. 'a' => 1,
  479. 'b' => ['h' => false, 'c' => ['d' => ['f' => 'g', 'h' => true]]],
  480. 'k' => 'm',
  481. ];
  482. $res = Utility::arrayFlatten($array);
  483. $expected = [
  484. 'a' => 1,
  485. 'h' => true,
  486. 'f' => 'g',
  487. 'k' => 'm',
  488. ];
  489. $this->assertSame($expected, $res);
  490. }
  491. /**
  492. * @covers ::arrayFlatten
  493. * @return void
  494. */
  495. public function testArrayFlattenAndPreserveKeys() {
  496. $array = [
  497. 0 => 1,
  498. 1 => ['c' => ['d' => ['g', 'h' => true]]],
  499. 2 => 'm',
  500. ];
  501. $res = Utility::arrayFlatten($array, true);
  502. $expected = [
  503. 0 => 'g',
  504. 'h' => true,
  505. 2 => 'm',
  506. ];
  507. $this->assertSame($expected, $res);
  508. }
  509. /**
  510. * @covers ::arrayShiftKeys
  511. * @return void
  512. */
  513. public function testArrayShiftKeys() {
  514. $array = [
  515. 'a' => 1,
  516. 'b' => ['c' => ['d' => ['f' => 'g', 'h' => true]]],
  517. 'k' => 'm',
  518. ];
  519. $res = Utility::arrayShiftKeys($array);
  520. $expected = 'a';
  521. $this->assertSame($expected, $res);
  522. $expected = [
  523. 'b' => ['c' => ['d' => ['f' => 'g', 'h' => true]]],
  524. 'k' => 'm',
  525. ];
  526. $this->assertSame($expected, $array);
  527. }
  528. /**
  529. * @covers ::returnElapsedTime
  530. * @return void
  531. */
  532. public function testTime() {
  533. Utility::startClock();
  534. time_nanosleep(0, 200000000);
  535. $res = Utility::returnElapsedTime();
  536. $this->assertTrue(round($res, 1) === 0.2);
  537. time_nanosleep(0, 100000000);
  538. $res = Utility::returnElapsedTime(8, true);
  539. $this->assertTrue(round($res, 1) === 0.3);
  540. time_nanosleep(0, 100000000);
  541. $res = Utility::returnElapsedTime();
  542. $this->assertTrue(round($res, 1) === 0.1);
  543. }
  544. /**
  545. * @covers ::logicalAnd
  546. * @return void
  547. */
  548. public function testLogicalAnd() {
  549. $array = [
  550. 'a' => 1,
  551. 'b' => 1,
  552. 'c' => 0,
  553. 'd' => 1,
  554. ];
  555. $is = Utility::logicalAnd($array);
  556. $this->assertFalse($is);
  557. $array = [
  558. 'a' => 1,
  559. 'b' => 1,
  560. 'c' => 1,
  561. 'd' => 1,
  562. ];
  563. $is = Utility::logicalAnd($array);
  564. $this->assertTrue($is);
  565. }
  566. /**
  567. * @covers ::logicalOr
  568. * @return void
  569. */
  570. public function testLogicalOr() {
  571. $array = [
  572. 'a' => 0,
  573. 'b' => 1,
  574. 'c' => 0,
  575. 'd' => 1,
  576. ];
  577. $is = Utility::logicalOr($array);
  578. $this->assertTrue($is);
  579. $array = [
  580. 'a' => 1,
  581. 'b' => 1,
  582. 'c' => 1,
  583. 'd' => 1,
  584. ];
  585. $is = Utility::logicalOr($array);
  586. $this->assertTrue($is);
  587. $array = [
  588. 'a' => 0,
  589. 'b' => 0,
  590. 'c' => 0,
  591. 'd' => 0,
  592. ];
  593. $is = Utility::logicalOr($array);
  594. $this->assertFalse($is);
  595. }
  596. }