UtilityTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. App::uses('Utility', 'Tools.Utility');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. /**
  5. * @covers Utility
  6. */
  7. class UtilityTest extends MyCakeTestCase {
  8. /**
  9. * UtilityTest::testInArray()
  10. *
  11. * @covers Utility::inArray
  12. * @return void
  13. */
  14. public function testInArray() {
  15. $res = Utility::inArray(2, array(1, 2, 3));
  16. $this->assertTrue($res);
  17. $res = Utility::inArray(4, array(1, 2, 7));
  18. $this->assertFalse($res);
  19. $res = Utility::inArray('2', array(1, 2, 3));
  20. $this->assertTrue($res);
  21. $res = Utility::inArray(2, array('1x', '2x', '3x'));
  22. $this->assertFalse($res);
  23. $res = Utility::inArray('3x', array('1x', '2x', '3x'));
  24. $this->assertTrue($res);
  25. $res = Utility::inArray(3, array('1', '2', '3'));
  26. $this->assertTrue($res);
  27. $res = Utility::inArray('2x', array(1, 2, 3));
  28. $this->assertFalse($res);
  29. }
  30. /**
  31. * UtilityTest::testPregMatch()
  32. *
  33. * @covers Utility::pregMatch
  34. * @return void
  35. */
  36. public function testPregMatch() {
  37. $string = '<abc>';
  38. preg_match('/\<(\w+)\>/', $string, $matches);
  39. $this->assertSame(array($string, 'abc'), $matches);
  40. $matches = Utility::pregMatch('/\<(\w+)\>/', $string);
  41. $this->assertSame(array($string, 'abc'), $matches);
  42. $string = '<äöü>';
  43. preg_match('/\<(.+)\>/', $string, $matches);
  44. $this->assertSame(array($string, 'äöü'), $matches);
  45. $matches = Utility::pregMatch('/\<(.+)\>/', $string);
  46. $this->assertSame(array($string, 'äöü'), $matches);
  47. $string = 'D-81245 München';
  48. preg_match('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches);
  49. $expected = array(
  50. $string,
  51. 'D',
  52. '81245',
  53. 'München'
  54. );
  55. $this->assertSame($expected, $matches);
  56. // we dont need the utf8 hack:
  57. $matches = Utility::pregMatch('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  58. $this->assertSame($expected, $matches);
  59. }
  60. /**
  61. * UtilityTest::testPregMatchWithPatternEscape()
  62. *
  63. * @covers Utility::pregMatch
  64. * @return void
  65. */
  66. public function testPregMatchWithPatternEscape() {
  67. $string = 'http://www.example.com/s?q=php.net+docs';
  68. $res = preg_quote($string);
  69. $this->assertSame('http\://www\.example\.com/s\?q\=php\.net\+docs', $res);
  70. $string = 'http://www.example.com/s?q=php.net+docs';
  71. $res = preg_quote($string, '/');
  72. $this->assertSame('http\:\/\/www\.example\.com\/s\?q\=php\.net\+docs', $res);
  73. $res = '/a\s*' . $res . '\s*z/i';
  74. $string = 'a ' . $string . ' z';
  75. $matches = Utility::pregMatch($res, $string);
  76. $expected = array($string);
  77. $this->assertSame($expected, $matches);
  78. }
  79. /**
  80. * UtilityTest::testPregMatchAll()
  81. *
  82. * @covers Utility::pregMatchAll
  83. * @return void
  84. */
  85. public function testPregMatchAll() {
  86. $string = 'D-81245 München';
  87. preg_match_all('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches, PREG_SET_ORDER);
  88. $expected = array(
  89. array(
  90. $string,
  91. 'D',
  92. '81245',
  93. 'München'
  94. )
  95. );
  96. $this->assertSame($expected, $matches);
  97. // we dont need the utf8 hack:
  98. $matches = Utility::pregMatchAll('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  99. $this->assertSame($expected, $matches);
  100. }
  101. /**
  102. * UtilityTest::testStrSplit()
  103. *
  104. * @covers Utility::strSplit
  105. * @return void
  106. */
  107. public function testStrSplit() {
  108. $res = str_split('some äöü string', 7);
  109. $expected = array('some äö', 'ü strin', 'g');
  110. $this->assertNotSame($expected, $res);
  111. $res = Utility::strSplit('some äöü string', 7);
  112. $this->assertSame($expected, $res);
  113. }
  114. /**
  115. * UtilityTest::testUrlEncode()
  116. *
  117. * @covers Utility::urlEncode
  118. * @return void
  119. */
  120. public function testUrlEncode() {
  121. $res = Utility::urlEncode('Some/cool=value+more-infos');
  122. $this->assertSame('U29tZS9jb29sPXZhbHVlK21vcmUtaW5mb3M_', $res);
  123. }
  124. /**
  125. * UtilityTest::testUrlDecode()
  126. *
  127. * @covers Utility::urlDecode
  128. * @return void
  129. */
  130. public function testUrlDecode() {
  131. $res = Utility::urlDecode('U29tZS9jb29sPXZhbHVlK21vcmUtaW5mb3M_');
  132. $this->assertSame('Some/cool=value+more-infos', $res);
  133. }
  134. /**
  135. * UtilityTest::testTypeCast()
  136. *
  137. * @covers Utility::typeCast
  138. * @return void
  139. */
  140. public function testTypeCast() {
  141. $res = Utility::typeCast(2, 'string');
  142. $this->assertNotSame(2, $res);
  143. $this->assertSame('2', $res);
  144. }
  145. /**
  146. * UtilityTest::testGetClientIp()
  147. *
  148. * @covers Utility::getClientIp
  149. * @return void
  150. */
  151. public function testGetClientIp() {
  152. $res = Utility::getClientIp();
  153. $this->assertEquals(env('REMOTE_ADDR'), $res);
  154. }
  155. /**
  156. * UtilityTest::testGetReferer()
  157. *
  158. * @covers Utility::getReferer
  159. * @return void
  160. */
  161. public function testGetReferer() {
  162. $res = Utility::getReferer();
  163. $this->assertEquals(env('HTTP_REFERER'), $res);
  164. $res = Utility::getReferer(true);
  165. $this->assertEquals(env('HTTP_REFERER'), $res);
  166. $_SERVER['HTTP_REFERER'] = '/foo/bar';
  167. $res = Utility::getReferer(true);
  168. $base = HTTP_BASE;
  169. if (!$base) {
  170. $base = 'http://localhost';
  171. }
  172. $this->assertEquals($base . env('HTTP_REFERER'), $res);
  173. }
  174. /**
  175. * UtilityTest::testGetHeaderFromUrl()
  176. *
  177. * @covers Utility::getHeaderFromUrl
  178. * @return void
  179. */
  180. public function testGetHeaderFromUrl() {
  181. $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
  182. $this->assertTrue(is_array($res) && count($res) > 10);
  183. $this->assertEquals('HTTP/1.0 200 OK', $res[0]);
  184. }
  185. /**
  186. * UtilityTest::testAutoPrefixUrl()
  187. *
  188. * @covers Utility::autoPrefixUrl
  189. * @return void
  190. */
  191. public function testAutoPrefixUrl() {
  192. $res = Utility::autoPrefixUrl('www.spiegel.de');
  193. $this->assertEquals('http://www.spiegel.de', $res);
  194. }
  195. /**
  196. * UtilityTest::testCleanUrl()
  197. *
  198. * @covers Utility::cleanUrl
  199. * @return void
  200. */
  201. public function testCleanUrl() {
  202. $res = Utility::cleanUrl('www.spiegel.de');
  203. $this->assertEquals('http://www.spiegel.de', $res);
  204. $res = Utility::cleanUrl('http://');
  205. $this->assertEquals('', $res);
  206. $res = Utility::cleanUrl('http://www');
  207. $this->assertEquals('', $res);
  208. $res = Utility::cleanUrl('spiegel.de');
  209. $this->assertEquals('http://spiegel.de', $res);
  210. $res = Utility::cleanUrl('spiegel.de', true);
  211. //echo returns($res);
  212. $this->assertEquals('http://www.spiegel.de', $res);
  213. }
  214. /**
  215. * UtilityTest::testDeep()
  216. *
  217. * @covers Utility::trimDeep
  218. * @return void
  219. */
  220. public function testDeep() {
  221. $is = array(
  222. 'f some',
  223. 'e 49r ' => 'rf r ',
  224. 'er' => array(array('ee' => array('rr ' => ' tt ')))
  225. );
  226. $expected = array(
  227. 'f some',
  228. 'e 49r ' => 'rf r',
  229. 'er' => array(array('ee' => array('rr ' => 'tt')))
  230. );
  231. //$this->assertSame($expected, $is);
  232. $res = Utility::trimDeep($is);
  233. $this->assertSame($expected, $res);
  234. //$res = CommonComponent::trimDeep($is);
  235. //$this->assertSame($expected, $res);
  236. }
  237. //TODO: move to boostrap
  238. public function _testDeepFunction() {
  239. $is = array(
  240. 'f some',
  241. 'e 49r ' => 'rf r ',
  242. 'er' => array(array('ee' => array('rr ' => ' tt ')))
  243. );
  244. $expected = array(
  245. 'f some',
  246. 'e 49r ' => 'rf r',
  247. 'er' => array(array('ee' => array('rr ' => 'tt')))
  248. );
  249. $res = Utility::deep('trim', $is);
  250. $this->assertSame($expected, $res);
  251. }
  252. /**
  253. * UtilityTest::testArrayFlattenBasic()
  254. *
  255. * @covers Utility::arrayFlatten
  256. * @return void
  257. */
  258. public function testArrayFlattenBasic() {
  259. $strings = array(
  260. 'a' => array('a' => 'A'),
  261. 'b' => array('b' => 'B', 'c' => 'C'),
  262. 'c' => array(),
  263. 'd' => array(array(array('z' => 'Z'), 'y' => 'Y'))
  264. );
  265. $result = Utility::arrayFlatten($strings);
  266. $expected = array(
  267. 'a' => 'A',
  268. 'b' => 'B',
  269. 'c' => 'C',
  270. 'z' => 'Z',
  271. 'y' => 'Y'
  272. );
  273. $this->assertSame($expected, $result);
  274. }
  275. /**
  276. * Test that deeper nested values overwrite higher ones.
  277. *
  278. * @covers Utility::arrayFlatten
  279. * @return void
  280. */
  281. public function testArrayFlatten() {
  282. $array = array(
  283. 'a' => 1,
  284. 'b' => array('h' => false, 'c' => array('d' => array('f' => 'g', 'h' => true))),
  285. 'k' => 'm',
  286. );
  287. $res = Utility::arrayFlatten($array);
  288. $expected = array(
  289. 'a' => 1,
  290. 'h' => true,
  291. 'f' => 'g',
  292. 'k' => 'm',
  293. );
  294. $this->assertSame($expected, $res);
  295. }
  296. /**
  297. * UtilityTest::testArrayFlattenAndPreserveKeys()
  298. *
  299. * @covers Utility::arrayFlatten
  300. * @return void
  301. */
  302. public function testArrayFlattenAndPreserveKeys() {
  303. $array = array(
  304. 0 => 1,
  305. 1 => array('c' => array('d' => array('g', 'h' => true))),
  306. 2 => 'm',
  307. );
  308. $res = Utility::arrayFlatten($array, true);
  309. $expected = array(
  310. 0 => 'g',
  311. 'h' => true,
  312. 2 => 'm',
  313. );
  314. $this->assertSame($expected, $res);
  315. }
  316. /**
  317. * UtilityTest::testArrayShiftKeys()
  318. *
  319. * @covers Utility::arrayShiftKeys
  320. * @return void
  321. */
  322. public function testArrayShiftKeys() {
  323. $array = array(
  324. 'a' => 1,
  325. 'b' => array('c' => array('d' => array('f' => 'g', 'h' => true))),
  326. 'k' => 'm',
  327. );
  328. $res = Utility::arrayShiftKeys($array);
  329. $expected = 'a';
  330. $this->assertSame($expected, $res);
  331. $expected = array(
  332. 'b' => array('c' => array('d' => array('f' => 'g', 'h' => true))),
  333. 'k' => 'm',
  334. );
  335. $this->assertSame($expected, $array);
  336. }
  337. /**
  338. * UtilityTest::testTime()
  339. *
  340. * @covers Utility::returnElapsedTime
  341. * @return void
  342. */
  343. public function testTime() {
  344. Utility::startClock();
  345. time_nanosleep(0, 200000000);
  346. $res = Utility::returnElapsedTime();
  347. $this->assertTrue(round($res, 1) === 0.2);
  348. time_nanosleep(0, 100000000);
  349. $res = Utility::returnElapsedTime(8, true);
  350. $this->assertTrue(round($res, 1) === 0.3);
  351. time_nanosleep(0, 100000000);
  352. $res = Utility::returnElapsedTime();
  353. $this->assertTrue(round($res, 1) === 0.1);
  354. }
  355. /**
  356. * UtilityTest::testLogicalAnd()
  357. *
  358. * @covers Utility::logicalAnd
  359. * @return void
  360. */
  361. public function testLogicalAnd() {
  362. $array = array(
  363. 'a' => 1,
  364. 'b' => 1,
  365. 'c' => 0,
  366. 'd' => 1,
  367. );
  368. $is = Utility::logicalAnd($array);
  369. $this->assertFalse($is);
  370. $array = array(
  371. 'a' => 1,
  372. 'b' => 1,
  373. 'c' => 1,
  374. 'd' => 1,
  375. );
  376. $is = Utility::logicalAnd($array);
  377. $this->assertTrue($is);
  378. }
  379. /**
  380. * UtilityTest::testLogicalOr()
  381. *
  382. * @covers Utility::logicalOr
  383. * @return void
  384. */
  385. public function testLogicalOr() {
  386. $array = array(
  387. 'a' => 0,
  388. 'b' => 1,
  389. 'c' => 0,
  390. 'd' => 1,
  391. );
  392. $is = Utility::logicalOr($array);
  393. $this->assertTrue($is);
  394. $array = array(
  395. 'a' => 1,
  396. 'b' => 1,
  397. 'c' => 1,
  398. 'd' => 1,
  399. );
  400. $is = Utility::logicalOr($array);
  401. $this->assertTrue($is);
  402. $array = array(
  403. 'a' => 0,
  404. 'b' => 0,
  405. 'c' => 0,
  406. 'd' => 0,
  407. );
  408. $is = Utility::logicalOr($array);
  409. $this->assertFalse($is);
  410. }
  411. /**
  412. * UtilityTest::testIsValidSaveAll()
  413. *
  414. * @covers Utility::isValidSaveAll
  415. * @return void
  416. */
  417. public function testIsValidSaveAll() {
  418. $result = Utility::isValidSaveAll(array());
  419. $this->assertFalse($result);
  420. $result = Utility::isValidSaveAll(array(true, true));
  421. $this->assertTrue($result);
  422. $result = Utility::isValidSaveAll(array(true, false));
  423. $this->assertFalse($result);
  424. }
  425. }