| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- App::uses('Utility', 'Tools.Utility');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- /**
- * 2012-02-21 ms
- */
- class UtilityTest extends MyCakeTestCase {
- public function testInArray() {
- $res = Utility::inArray(2, array(1, 2, 3));
- $this->assertTrue($res);
- $res = Utility::inArray(4, array(1, 2, 7));
- $this->assertFalse($res);
- $res = Utility::inArray('2', array(1, 2, 3));
- $this->assertTrue($res);
- $res = Utility::inArray(2, array('1x', '2x', '3x'));
- $this->assertFalse($res);
- $res = Utility::inArray('3x', array('1x', '2x', '3x'));
- $this->assertTrue($res);
- $res = Utility::inArray(3, array('1', '2', '3'));
- $this->assertTrue($res);
- $res = Utility::inArray('2x', array(1, 2, 3));
- $this->assertFalse($res);
- }
- public function testTypeCast() {
- $res = Utility::typeCast(2, 'string');
- $this->assertNotSame(2, $res);
- $this->assertSame('2', $res);
- }
- public function testGetClientIp() {
- $res = Utility::getClientIp();
- $this->assertEquals(env('REMOTE_ADDR'), $res);
- }
- public function testGetReferer() {
- $res = Utility::getReferer();
- //$this->assertTrue(env(''), $res);
- $this->assertEquals(env('HTTP_REFERER'), $res);
- }
- public function testGetHeaderFromUrl() {
- $res = Utility::getHeaderFromUrl('http://www.spiegel.de');
- $this->assertTrue(is_array($res) && count($res) > 10);
- $this->assertEquals('HTTP/1.0 200 OK', $res[0]);
- }
- public function testAutoPrefixUrl() {
- $res = Utility::autoPrefixUrl('www.spiegel.de');
- $this->assertEquals('http://www.spiegel.de', $res);
- }
- public function testCleanUrl() {
- $res = Utility::cleanUrl('www.spiegel.de');
- $this->assertEquals('http://www.spiegel.de', $res);
- $res = Utility::cleanUrl('http://');
- $this->assertEquals('', $res);
- $res = Utility::cleanUrl('http://www');
- $this->assertEquals('', $res);
- $res = Utility::cleanUrl('spiegel.de');
- $this->assertEquals('http://spiegel.de', $res);
- $res = Utility::cleanUrl('spiegel.de', true);
- //echo returns($res);
- $this->assertEquals('http://www.spiegel.de', $res);
- }
- public function testDeep() {
- $is = array(
- 'f some',
- 'e 49r ' => 'rf r ',
- 'er' => array(array('ee'=>array('rr '=>' tt ')))
- );
- $expected = array(
- 'f some',
- 'e 49r ' => 'rf r',
- 'er' => array(array('ee'=>array('rr '=>'tt')))
- );
- //$this->assertSame($is, $expected);
- $res = Utility::trimDeep($is);
- $this->assertSame($res, $expected);
- //$res = CommonComponent::trimDeep($is);
- //$this->assertSame($res, $expected);
- }
- //TODO: move to boostrap
- public function _testDeepFunction() {
- $is = array(
- 'f some',
- 'e 49r ' => 'rf r ',
- 'er' => array(array('ee'=>array('rr '=>' tt ')))
- );
- $expected = array(
- 'f some',
- 'e 49r ' => 'rf r',
- 'er' => array(array('ee'=>array('rr '=>'tt')))
- );
- $res = Utility::deep('trim', $is);
- $this->assertSame($res, $expected);
- }
- public function testArrayFlatten() {
- $array=array(
- 'a' => 1,
- 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
- 'k' => 'm',
- );
- $res = Utility::arrayFlatten($array);
- $expected = array(
- 'a' => 1,
- 'f' => 'g',
- 'h'=> true,
- 'k' => 'm',
- );
- $this->assertSame($expected, $res);
- }
- public function testArrayShiftKeys() {
- $array=array(
- 'a' => 1,
- 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
- 'k' => 'm',
- );
- $res = Utility::arrayShiftKeys($array);
- $expected = 'a';
- $this->assertSame($expected, $res);
- $expected = array(
- 'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
- 'k' => 'm',
- );
- $this->assertSame($expected, $array);
- }
- public function testTime() {
- Utility::startClock();
- time_nanosleep(0, 200000000);
- $res = Utility::returnElapsedTime();
- $this->assertTrue(round($res, 1) === 0.2);
- time_nanosleep(0, 100000000);
- $res = Utility::returnElapsedTime(8, true);
- $this->assertTrue(round($res, 1) === 0.3);
- time_nanosleep(0, 100000000);
- $res = Utility::returnElapsedTime();
- $this->assertTrue(round($res, 1) === 0.1);
- }
- public function testLogicalAnd() {
- $array=array(
- 'a' => 1,
- 'b' => 1,
- 'c' => 0,
- 'd' => 1,
- );
- $is = Utility::logicalAnd($array);
- $this->assertSame($is, false);
- $array=array(
- 'a' => 1,
- 'b' => 1,
- 'c' => 1,
- 'd' => 1,
- );
- $is = Utility::logicalAnd($array);
- $this->assertSame($is, true);
- }
- public function testLogicalOr() {
- $array=array(
- 'a' => 0,
- 'b' => 1,
- 'c' => 0,
- 'd' => 1,
- );
- $is = Utility::logicalOr($array);
- $this->assertSame($is, true);
- $array=array(
- 'a' => 1,
- 'b' => 1,
- 'c' => 1,
- 'd' => 1,
- );
- $is = Utility::logicalOr($array);
- $this->assertSame($is, true);
- $array=array(
- 'a' => 0,
- 'b' => 0,
- 'c' => 0,
- 'd' => 0,
- );
- $is = Utility::logicalOr($array);
- $this->assertSame($is, false);
- }
- }
|