UtilityTest.php 8.7 KB

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