UtilityTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. public function testInArray() {
  9. $res = Utility::inArray(2, array(1, 2, 3));
  10. $this->assertTrue($res);
  11. $res = Utility::inArray(4, array(1, 2, 7));
  12. $this->assertFalse($res);
  13. $res = Utility::inArray('2', array(1, 2, 3));
  14. $this->assertTrue($res);
  15. $res = Utility::inArray(2, array('1x', '2x', '3x'));
  16. $this->assertFalse($res);
  17. $res = Utility::inArray('3x', array('1x', '2x', '3x'));
  18. $this->assertTrue($res);
  19. $res = Utility::inArray(3, array('1', '2', '3'));
  20. $this->assertTrue($res);
  21. $res = Utility::inArray('2x', array(1, 2, 3));
  22. $this->assertFalse($res);
  23. }
  24. public function testPregMatch() {
  25. $string = '<abc>';
  26. preg_match('/\<(\w+)\>/', $string, $matches);
  27. $this->assertSame(array($string, 'abc'), $matches);
  28. $matches = Utility::pregMatch('/\<(\w+)\>/', $string);
  29. $this->assertSame(array($string, 'abc'), $matches);
  30. $string = '<äöü>';
  31. preg_match('/\<(.+)\>/', $string, $matches);
  32. $this->assertSame(array($string, 'äöü'), $matches);
  33. $matches = Utility::pregMatch('/\<(.+)\>/', $string);
  34. $this->assertSame(array($string, 'äöü'), $matches);
  35. $string = 'D-81245 München';
  36. preg_match('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches);
  37. $expected = array(
  38. $string,
  39. 'D',
  40. '81245',
  41. 'München'
  42. );
  43. $this->assertSame($expected, $matches);
  44. // we dont need the utf8 hack:
  45. $matches = Utility::pregMatch('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  46. $this->assertSame($expected, $matches);
  47. }
  48. public function testPregMatchAll() {
  49. $string = 'D-81245 München';
  50. preg_match_all('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches, PREG_SET_ORDER);
  51. $expected = array(
  52. array(
  53. $string,
  54. 'D',
  55. '81245',
  56. 'München'
  57. )
  58. );
  59. $this->assertSame($expected, $matches);
  60. // we dont need the utf8 hack:
  61. $matches = Utility::pregMatchAll('/([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string);
  62. $this->assertSame($expected, $matches);
  63. }
  64. public function testStrSplit() {
  65. $res = str_split('some äöü string', 7);
  66. $expected = array('some äö', 'ü strin', 'g');
  67. $this->assertNotSame($expected, $res);
  68. $res = Utility::strSplit('some äöü string', 7);
  69. $this->assertSame($expected, $res);
  70. }
  71. public function testTypeCast() {
  72. $res = Utility::typeCast(2, 'string');
  73. $this->assertNotSame(2, $res);
  74. $this->assertSame('2', $res);
  75. }
  76. public function testGetClientIp() {
  77. $res = Utility::getClientIp();
  78. $this->assertEquals(env('REMOTE_ADDR'), $res);
  79. }
  80. public function testGetReferer() {
  81. $res = Utility::getReferer();
  82. //$this->assertTrue(env(''), $res);
  83. $this->assertEquals(env('HTTP_REFERER'), $res);
  84. }
  85. public function testGetHeaderFromUrl() {
  86. $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
  87. $this->assertTrue(is_array($res) && count($res) > 10);
  88. $this->assertEquals('HTTP/1.0 200 OK', $res[0]);
  89. }
  90. public function testAutoPrefixUrl() {
  91. $res = Utility::autoPrefixUrl('www.spiegel.de');
  92. $this->assertEquals('http://www.spiegel.de', $res);
  93. }
  94. public function testCleanUrl() {
  95. $res = Utility::cleanUrl('www.spiegel.de');
  96. $this->assertEquals('http://www.spiegel.de', $res);
  97. $res = Utility::cleanUrl('http://');
  98. $this->assertEquals('', $res);
  99. $res = Utility::cleanUrl('http://www');
  100. $this->assertEquals('', $res);
  101. $res = Utility::cleanUrl('spiegel.de');
  102. $this->assertEquals('http://spiegel.de', $res);
  103. $res = Utility::cleanUrl('spiegel.de', true);
  104. //echo returns($res);
  105. $this->assertEquals('http://www.spiegel.de', $res);
  106. }
  107. public function testDeep() {
  108. $is = array(
  109. 'f some',
  110. 'e 49r ' => 'rf r ',
  111. 'er' => array(array('ee'=>array('rr '=>' tt ')))
  112. );
  113. $expected = array(
  114. 'f some',
  115. 'e 49r ' => 'rf r',
  116. 'er' => array(array('ee'=>array('rr '=>'tt')))
  117. );
  118. //$this->assertSame($is, $expected);
  119. $res = Utility::trimDeep($is);
  120. $this->assertSame($res, $expected);
  121. //$res = CommonComponent::trimDeep($is);
  122. //$this->assertSame($res, $expected);
  123. }
  124. //TODO: move to boostrap
  125. public function _testDeepFunction() {
  126. $is = array(
  127. 'f some',
  128. 'e 49r ' => 'rf r ',
  129. 'er' => array(array('ee'=>array('rr '=>' tt ')))
  130. );
  131. $expected = array(
  132. 'f some',
  133. 'e 49r ' => 'rf r',
  134. 'er' => array(array('ee'=>array('rr '=>'tt')))
  135. );
  136. $res = Utility::deep('trim', $is);
  137. $this->assertSame($res, $expected);
  138. }
  139. public function testArrayFlatten() {
  140. $array=array(
  141. 'a' => 1,
  142. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  143. 'k' => 'm',
  144. );
  145. $res = Utility::arrayFlatten($array);
  146. $expected = array(
  147. 'a' => 1,
  148. 'f' => 'g',
  149. 'h'=> true,
  150. 'k' => 'm',
  151. );
  152. $this->assertSame($expected, $res);
  153. }
  154. public function testArrayShiftKeys() {
  155. $array=array(
  156. 'a' => 1,
  157. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  158. 'k' => 'm',
  159. );
  160. $res = Utility::arrayShiftKeys($array);
  161. $expected = 'a';
  162. $this->assertSame($expected, $res);
  163. $expected = array(
  164. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  165. 'k' => 'm',
  166. );
  167. $this->assertSame($expected, $array);
  168. }
  169. public function testTime() {
  170. Utility::startClock();
  171. time_nanosleep(0, 200000000);
  172. $res = Utility::returnElapsedTime();
  173. $this->assertTrue(round($res, 1) === 0.2);
  174. time_nanosleep(0, 100000000);
  175. $res = Utility::returnElapsedTime(8, true);
  176. $this->assertTrue(round($res, 1) === 0.3);
  177. time_nanosleep(0, 100000000);
  178. $res = Utility::returnElapsedTime();
  179. $this->assertTrue(round($res, 1) === 0.1);
  180. }
  181. public function testLogicalAnd() {
  182. $array=array(
  183. 'a' => 1,
  184. 'b' => 1,
  185. 'c' => 0,
  186. 'd' => 1,
  187. );
  188. $is = Utility::logicalAnd($array);
  189. $this->assertSame($is, false);
  190. $array=array(
  191. 'a' => 1,
  192. 'b' => 1,
  193. 'c' => 1,
  194. 'd' => 1,
  195. );
  196. $is = Utility::logicalAnd($array);
  197. $this->assertSame($is, true);
  198. }
  199. public function testLogicalOr() {
  200. $array=array(
  201. 'a' => 0,
  202. 'b' => 1,
  203. 'c' => 0,
  204. 'd' => 1,
  205. );
  206. $is = Utility::logicalOr($array);
  207. $this->assertSame($is, true);
  208. $array=array(
  209. 'a' => 1,
  210. 'b' => 1,
  211. 'c' => 1,
  212. 'd' => 1,
  213. );
  214. $is = Utility::logicalOr($array);
  215. $this->assertSame($is, true);
  216. $array=array(
  217. 'a' => 0,
  218. 'b' => 0,
  219. 'c' => 0,
  220. 'd' => 0,
  221. );
  222. $is = Utility::logicalOr($array);
  223. $this->assertSame($is, false);
  224. }
  225. }