Repository navigation
Expand file tree
/
Copy pathMemcache.php
More file actions
113 lines (104 loc) · 2.74 KB
/
Copy pathMemcache.php
File metadata and controls
113 lines (104 loc) · 2.74 KB
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
108
109
110
111
112
113
<?php
namespace Codeception\Module;
use Codeception\Module as CodeceptionModule;
use Codeception\TestCase;
use Codeception\Exception\ModuleConfigException;
/**
* Connects to [memcached](http://www.memcached.org/) using either _Memcache_ or _Memcached_ extension.
*
* Performs a cleanup by flushing all values after each test run.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **beta**
* * Contact: codecept@davert.mail.ua
*
* ## Configuration
*
* * host: localhost - memcached host to connect
* * port: 11211 - default memcached port.
*
* Be sure you don't use the production server to connect.
*
* ## Public Properties
*
* * memcache - instance of Memcache object
*
*/
class Memcache extends CodeceptionModule
{
/**
* @var \Memcache|\Memcached
*/
public $memcache = null;
protected $config = ['host' => 'localhost', 'port' => 11211];
public function _before(TestCase $test)
{
if (class_exists('\Memcache')) {
$this->memcache = new \Memcache;
$this->memcache->close();
$this->memcache->connect($this->config['host'], $this->config['port']);
} elseif (class_exists('\Memcached')) {
$this->memcache = new \Memcached;
$this->memcache->addServer($this->config['host'], $this->config['port']);
} else {
throw new ModuleConfigException(__CLASS__, 'Memcache classes not loaded');
}
}
public function _after(TestCase $test)
{
$this->memcache->flush();
}
/**
* Grabs value from memcached by key
*
* Example:
*
* ``` php
* <?php
* $users_count = $I->grabValueFromMemcached('users_count');
* ?>
* ```
*
* @param $key
* @return array|string
*/
public function grabValueFromMemcached($key)
{
$value = $this->memcache->get($key);
$this->debugSection("Value", $value);
return $value;
}
/**
* Checks item in Memcached exists and the same as expected.
*
* @param $key
* @param $value
*/
public function seeInMemcached($key, $value = false)
{
$actual = $this->memcache->get($key);
$this->debugSection("Value", $actual);
$this->assertEquals($value, $actual);
}
/**
* Checks item in Memcached doesn't exist or is the same as expected.
*
* @param $key
* @param bool $value
*/
public function dontSeeInMemcached($key, $value = false)
{
$actual = $this->memcache->get($key);
$this->debugSection("Value", $actual);
$this->assertNotEquals($value, $actual);
}
/**
* Flushes all Memcached data.
*/
public function clearMemcache()
{
$this->memcache->flush();
}
}