colly_wyx
2018-04-27 74adf3a72663f151dc2c1b87ecb4ea4b0e080a50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
 * PhpUnderControl_PhalApiTool_Test
 *
 * 针对 ../PhalApi/Tool.php PhalApi_Tool 类的PHPUnit单元测试
 *
 * @author: dogstar 20150212
 */
 
require_once dirname(__FILE__) . '/test_env.php';
 
if (!class_exists('PhalApi_Tool')) {
    require dirname(__FILE__) . '/../PhalApi/Tool.php';
}
 
class PhpUnderControl_PhalApiTool_Test extends PHPUnit_Framework_TestCase
{
    public $phalApiTool;
 
    protected function setUp()
    {
        parent::setUp();
 
        $this->phalApiTool = new PhalApi_Tool();
    }
 
    protected function tearDown()
    {
    }
 
 
    /**
     * @group testGetClientIp
     */ 
    public function testGetClientIp()
    {
        $rs = PhalApi_Tool::getClientIp();
    }
 
    public function testGetClientIpWithEnvMock() {
        $_SERVER['REMOTE_ADDR'] = '127.0.0.4';
        $this->assertEquals('127.0.0.4', PhalApi_Tool::getClientIp());
 
        putenv('REMOTE_ADDR=127.0.0.3');
        $this->assertEquals('127.0.0.3', PhalApi_Tool::getClientIp());
 
        putenv('HTTP_X_FORWARDED_FOR=127.0.0.2');
        $this->assertEquals('127.0.0.2', PhalApi_Tool::getClientIp());
 
        putenv('HTTP_CLIENT_IP=127.0.0.1');
        $this->assertEquals('127.0.0.1', PhalApi_Tool::getClientIp());
    }
 
    /**
     * @group testCreateRandStr
     */ 
    public function testCreateRandStr()
    {
        $len = '5';
 
        $rs = PhalApi_Tool::createRandStr($len);
 
        $this->assertEquals($len, strlen($rs));
    }
 
    public function testCreateDir()
    {
        PhalApi_Tool::createDir("./test/test2/test3");
 
        $this->assertEquals(true, is_dir("./test/test2/test3"));
        PhalApi_Tool::deleteDir("./test");
    }
 
    public function testDeleteDir()
    {
        mkdir("./test");
        mkdir("./test/test2");
        file_put_contents("./test/test2/test3","test");
        PhalApi_Tool::deleteDir("./test");
 
        $this->assertEquals(false, is_dir("./test"));
    }
 
    public function testArrIndex()
    {
 
        $arr = array(
            "test" => "test"
        );
        ;
 
        $this->assertEquals("test", PhalApi_Tool::arrIndex($arr,"test"));
        $this->assertEquals("default", PhalApi_Tool::arrIndex($arr,"test2","default"));
        $this->assertEquals('', PhalApi_Tool::arrIndex($arr,"test3"));
    }
 
}