UtilityTest.php 8.0 KB

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