UtilityTest.php 16 KB

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