UtilityTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 testTypeCast() {
  25. $res = Utility::typeCast(2, 'string');
  26. $this->assertNotSame(2, $res);
  27. $this->assertSame('2', $res);
  28. }
  29. public function testGetClientIp() {
  30. $res = Utility::getClientIp();
  31. $this->assertEquals(env('REMOTE_ADDR'), $res);
  32. }
  33. public function testGetReferer() {
  34. $res = Utility::getReferer();
  35. //$this->assertTrue(env(''), $res);
  36. $this->assertEquals(env('HTTP_REFERER'), $res);
  37. }
  38. public function testGetHeaderFromUrl() {
  39. $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
  40. $this->assertTrue(is_array($res) && count($res) > 10);
  41. $this->assertEquals('HTTP/1.0 200 OK', $res[0]);
  42. }
  43. public function testAutoPrefixUrl() {
  44. $res = Utility::autoPrefixUrl('www.spiegel.de');
  45. $this->assertEquals('http://www.spiegel.de', $res);
  46. }
  47. public function testCleanUrl() {
  48. $res = Utility::cleanUrl('www.spiegel.de');
  49. $this->assertEquals('http://www.spiegel.de', $res);
  50. $res = Utility::cleanUrl('http://');
  51. $this->assertEquals('', $res);
  52. $res = Utility::cleanUrl('http://www');
  53. $this->assertEquals('', $res);
  54. $res = Utility::cleanUrl('spiegel.de');
  55. $this->assertEquals('http://spiegel.de', $res);
  56. $res = Utility::cleanUrl('spiegel.de', true);
  57. echo returns($res);
  58. $this->assertEquals('http://www.spiegel.de', $res);
  59. }
  60. public function testDeep() {
  61. $is = array(
  62. 'f some',
  63. 'e 49r ' => 'rf r ',
  64. 'er' => array(array('ee'=>array('rr '=>' tt ')))
  65. );
  66. $expected = array(
  67. 'f some',
  68. 'e 49r ' => 'rf r',
  69. 'er' => array(array('ee'=>array('rr '=>'tt')))
  70. );
  71. //$this->assertSame($is, $expected);
  72. $res = Utility::trimDeep($is);
  73. $this->assertSame($res, $expected);
  74. //$res = CommonComponent::trimDeep($is);
  75. //$this->assertSame($res, $expected);
  76. }
  77. //TODO: move to boostrap
  78. public function _testDeepFunction() {
  79. $is = array(
  80. 'f some',
  81. 'e 49r ' => 'rf r ',
  82. 'er' => array(array('ee'=>array('rr '=>' tt ')))
  83. );
  84. $expected = array(
  85. 'f some',
  86. 'e 49r ' => 'rf r',
  87. 'er' => array(array('ee'=>array('rr '=>'tt')))
  88. );
  89. $res = Utility::deep('trim', $is);
  90. $this->assertSame($res, $expected);
  91. }
  92. public function testArrayFlatten() {
  93. $array=array(
  94. 'a' => 1,
  95. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  96. 'k' => 'm',
  97. );
  98. $res = Utility::arrayFlatten($array);
  99. $expected = array(
  100. 'a' => 1,
  101. 'f' => 'g',
  102. 'h'=> true,
  103. 'k' => 'm',
  104. );
  105. $this->assertSame($expected, $res);
  106. }
  107. public function testArrayShiftKeys() {
  108. $array=array(
  109. 'a' => 1,
  110. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  111. 'k' => 'm',
  112. );
  113. $res = Utility::arrayShiftKeys($array);
  114. $expected = 'a';
  115. $this->assertSame($expected, $res);
  116. $expected = array(
  117. 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
  118. 'k' => 'm',
  119. );
  120. $this->assertSame($expected, $array);
  121. }
  122. public function testTime() {
  123. Utility::startClock();
  124. time_nanosleep(0, 200000000);
  125. $res = Utility::returnElapsedTime();
  126. $this->assertTrue(round($res, 1) === 0.2);
  127. time_nanosleep(0, 100000000);
  128. $res = Utility::returnElapsedTime(8, true);
  129. $this->assertTrue(round($res, 1) === 0.3);
  130. time_nanosleep(0, 100000000);
  131. $res = Utility::returnElapsedTime();
  132. $this->assertTrue(round($res, 1) === 0.1);
  133. }
  134. public function testLogicalAnd() {
  135. $array=array(
  136. 'a' => 1,
  137. 'b' => 1,
  138. 'c' => 0,
  139. 'd' => 1,
  140. );
  141. $is = Utility::logicalAnd($array);
  142. $this->assertSame($is, false);
  143. $array=array(
  144. 'a' => 1,
  145. 'b' => 1,
  146. 'c' => 1,
  147. 'd' => 1,
  148. );
  149. $is = Utility::logicalAnd($array);
  150. $this->assertSame($is, true);
  151. }
  152. public function testLogicalOr() {
  153. $array=array(
  154. 'a' => 0,
  155. 'b' => 1,
  156. 'c' => 0,
  157. 'd' => 1,
  158. );
  159. $is = Utility::logicalOr($array);
  160. $this->assertSame($is, true);
  161. $array=array(
  162. 'a' => 1,
  163. 'b' => 1,
  164. 'c' => 1,
  165. 'd' => 1,
  166. );
  167. $is = Utility::logicalOr($array);
  168. $this->assertSame($is, true);
  169. $array=array(
  170. 'a' => 0,
  171. 'b' => 0,
  172. 'c' => 0,
  173. 'd' => 0,
  174. );
  175. $is = Utility::logicalOr($array);
  176. $this->assertSame($is, false);
  177. }
  178. }