UtilityTest.php 15 KB

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