UtilityTest.php 15 KB

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