UtilityTest.php 15 KB

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