UtilityTest.php 15 KB

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