colly_wyx
2018-05-18 50a53f7db63c5729b0ddbc93367117cf35ccf03c
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
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env php
<?php
/**
 * PhpUnderControl_PhalApiCacheMemcache_Test
 *
 * 针对 ../../PhalApi/Cache/Memcache.php PhalApi_Cache_Memcache 类的PHPUnit单元测试
 *
 * @author: dogstar 20150507
 */
 
require_once dirname(__FILE__) . '/../test_env.php';
 
if (!class_exists('PhalApi_Cache_Memcache')) {
    require dirname(__FILE__) . '/../../PhalApi/Cache/Memcache.php';
}
 
class PhpUnderControl_PhalApiCacheMemcache_Test extends PHPUnit_Framework_TestCase
{
    public $phalApiCacheMemcache;
 
    protected function setUp()
    {
        parent::setUp();
 
        $config = array('host' => '127.0.0.1', 'port' => '11211');
        $this->phalApiCacheMemcache = new PhalApi_Cache_Memcache($config);
    }
 
    protected function tearDown()
    {
    }
 
 
    /**
     * @group testSet
     */ 
    public function testSet()
    {
        $key = 'key-2015-05-07';
        $value = 'phalapi';
        $expire = 60;
 
        $this->phalApiCacheMemcache->set($key, $value, $expire);
 
        $this->assertEquals('phalapi', $this->phalApiCacheMemcache->get($key));
    }
 
    /**
     * @group testGet
     */ 
    public function testGet()
    {
        $key = 'no-this-key';
 
        $rs = $this->phalApiCacheMemcache->get($key);
 
        $this->assertSame(NULL, $rs);
    }
 
    /**
     * @group testDelete
     */ 
    public function testDelete()
    {
        $key = 'key-2015-05-07';
 
        $this->assertNotNull($this->phalApiCacheMemcache->get($key));
 
        $this->phalApiCacheMemcache->delete($key);
 
        $this->assertNull($this->phalApiCacheMemcache->get($key));
    }
 
    public function testMultiMemcacheInstance()
    {
        $config = array(
            'host' => '127.0.0.1, 127.0.0.1', 
            'port' => '11211, 11212',
            'weight' => '20, 80',
        );
 
        $memcache = new PhalApi_Cache_Memcache($config);
 
        $memcache->set('multi-key-1', 'M1', 60);
        $memcache->set('multi-key-2', 'M2', 60);
        $memcache->set('multi-key-3', 'M3', 60);
 
        $this->assertEquals('M1', $memcache->get('multi-key-1'));
        $this->assertEquals('M2', $memcache->get('multi-key-2'));
        $this->assertEquals('M3', $memcache->get('multi-key-3'));
    }
 
    public function testMultiMemcacheInstanceUnformer()
    {
        $config = array(
            'host' => '127.0.0.1, 127.0.0.1', 
            'port' => '11211, 11212',   // same as 11211, 11212
            'weight' => '20',           // same as 20, 0
        );
 
        $memcache = new PhalApi_Cache_Memcache($config);
 
        $memcache->set('multi-key-4', 'M4', 60);
        $this->assertEquals('M4', $memcache->get('multi-key-4'));
    }
 
}