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