UtilityTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. $this->assertEquals(HTTP_BASE . env('HTTP_REFERER'), $res);
  159. }
  160. /**
  161. * UtilityTest::testGetHeaderFromUrl()
  162. *
  163. * @return void
  164. */
  165. public function testGetHeaderFromUrl() {
  166. $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
  167. $this->assertTrue(is_array($res) && count($res) > 10);
  168. $this->assertEquals('HTTP/1.0 200 OK', $res[0]);
  169. }
  170. /**
  171. * UtilityTest::testAutoPrefixUrl()
  172. *
  173. * @return void
  174. */
  175. public function testAutoPrefixUrl() {
  176. $res = Utility::autoPrefixUrl('www.spiegel.de');
  177. $this->assertEquals('http://www.spiegel.de', $res);
  178. }
  179. /**
  180. * UtilityTest::testCleanUrl()
  181. *
  182. * @return void
  183. */
  184. public function testCleanUrl() {
  185. $res = Utility::cleanUrl('www.spiegel.de');
  186. $this->assertEquals('http://www.spiegel.de', $res);
  187. $res = Utility::cleanUrl('http://');
  188. $this->assertEquals('', $res);
  189. $res = Utility::cleanUrl('http://www');
  190. $this->assertEquals('', $res);
  191. $res = Utility::cleanUrl('spiegel.de');
  192. $this->assertEquals('http://spiegel.de', $res);
  193. $res = Utility::cleanUrl('spiegel.de', true);
  194. //echo returns($res);
  195. $this->assertEquals('http://www.spiegel.de', $res);
  196. }
  197. /**
  198. * UtilityTest::testDeep()
  199. *
  200. * @return void
  201. */
  202. public function testDeep() {
  203. $is = array(
  204. 'f some',
  205. 'e 49r ' => 'rf r ',
  206. 'er' => array(array('ee' => array('rr ' => ' tt ')))
  207. );
  208. $expected = array(
  209. 'f some',
  210. 'e 49r ' => 'rf r',
  211. 'er' => array(array('ee' => array('rr ' => 'tt')))
  212. );
  213. //$this->assertSame($is, $expected);
  214. $res = Utility::trimDeep($is);
  215. $this->assertSame($res, $expected);
  216. //$res = CommonComponent::trimDeep($is);
  217. //$this->assertSame($res, $expected);
  218. }
  219. //TODO: move to boostrap
  220. public function _testDeepFunction() {
  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. $res = Utility::deep('trim', $is);
  232. $this->assertSame($res, $expected);
  233. }
  234. /**
  235. * UtilityTest::testArrayFlatten()
  236. *
  237. * @return void
  238. */
  239. public function testArrayFlatten() {
  240. $array = array(
  241. 'a' => 1,
  242. 'b' => array('c' => array('d' => array('f' => 'g', 'h' => true))),
  243. 'k' => 'm',
  244. );
  245. $res = Utility::arrayFlatten($array);
  246. $expected = array(
  247. 'a' => 1,
  248. 'f' => 'g',
  249. 'h'=> true,
  250. 'k' => 'm',
  251. );
  252. $this->assertSame($expected, $res);
  253. }
  254. /**
  255. * UtilityTest::testArrayShiftKeys()
  256. *
  257. * @return void
  258. */
  259. public function testArrayShiftKeys() {
  260. $array = array(
  261. 'a' => 1,
  262. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  263. 'k' => 'm',
  264. );
  265. $res = Utility::arrayShiftKeys($array);
  266. $expected = 'a';
  267. $this->assertSame($expected, $res);
  268. $expected = array(
  269. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  270. 'k' => 'm',
  271. );
  272. $this->assertSame($expected, $array);
  273. }
  274. /**
  275. * UtilityTest::testTime()
  276. *
  277. * @return void
  278. */
  279. public function testTime() {
  280. Utility::startClock();
  281. time_nanosleep(0, 200000000);
  282. $res = Utility::returnElapsedTime();
  283. $this->assertTrue(round($res, 1) === 0.2);
  284. time_nanosleep(0, 100000000);
  285. $res = Utility::returnElapsedTime(8, true);
  286. $this->assertTrue(round($res, 1) === 0.3);
  287. time_nanosleep(0, 100000000);
  288. $res = Utility::returnElapsedTime();
  289. $this->assertTrue(round($res, 1) === 0.1);
  290. }
  291. /**
  292. * UtilityTest::testLogicalAnd()
  293. *
  294. * @return void
  295. */
  296. public function testLogicalAnd() {
  297. $array = array(
  298. 'a' => 1,
  299. 'b' => 1,
  300. 'c' => 0,
  301. 'd' => 1,
  302. );
  303. $is = Utility::logicalAnd($array);
  304. $this->assertSame($is, false);
  305. $array = array(
  306. 'a' => 1,
  307. 'b' => 1,
  308. 'c' => 1,
  309. 'd' => 1,
  310. );
  311. $is = Utility::logicalAnd($array);
  312. $this->assertSame($is, true);
  313. }
  314. /**
  315. * UtilityTest::testLogicalOr()
  316. *
  317. * @return void
  318. */
  319. public function testLogicalOr() {
  320. $array = array(
  321. 'a' => 0,
  322. 'b' => 1,
  323. 'c' => 0,
  324. 'd' => 1,
  325. );
  326. $is = Utility::logicalOr($array);
  327. $this->assertSame($is, true);
  328. $array = array(
  329. 'a' => 1,
  330. 'b' => 1,
  331. 'c' => 1,
  332. 'd' => 1,
  333. );
  334. $is = Utility::logicalOr($array);
  335. $this->assertSame($is, true);
  336. $array = array(
  337. 'a' => 0,
  338. 'b' => 0,
  339. 'c' => 0,
  340. 'd' => 0,
  341. );
  342. $is = Utility::logicalOr($array);
  343. $this->assertSame($is, false);
  344. }
  345. }