Skip to content

Commit a73d8ac

Browse files
committed
Test: Improve CacheFactory code coverage
1 parent bb97dbd commit a73d8ac

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

system/Cache/Handlers/WincacheHandler.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\Cache\Handlers;
1+
<?php
2+
namespace CodeIgniter\Cache\Handlers;
23

34
/**
45
* CodeIgniter
@@ -35,9 +36,14 @@
3536
* @since Version 3.0.0
3637
* @filesource
3738
*/
38-
3939
use CodeIgniter\Cache\CacheInterface;
4040

41+
/**
42+
* Cache handler for WinCache from Microsoft & IIS.
43+
* Windows-only, so not testable on travis-ci.
44+
* Unusable methods flagged for code coverage ignoring.
45+
*
46+
*/
4147
class WincacheHandler implements CacheInterface
4248
{
4349

@@ -59,6 +65,7 @@ public function __construct($config)
5965

6066
/**
6167
* Takes care of any handler-specific setup that must be done.
68+
* @codeCoverageIgnore
6269
*/
6370
public function initialize()
6471
{
@@ -69,6 +76,7 @@ public function initialize()
6976

7077
/**
7178
* Attempts to fetch an item from the cache store.
79+
* @codeCoverageIgnore
7280
*
7381
* @param string $key Cache item name
7482
*
@@ -79,7 +87,7 @@ public function get(string $key)
7987
$key = $this->prefix . $key;
8088

8189
$success = false;
82-
$data = wincache_ucache_get($key, $success);
90+
$data = wincache_ucache_get($key, $success);
8391

8492
// Success returned by reference from wincache_ucache_get()
8593
return ($success) ? $data : false;
@@ -89,6 +97,7 @@ public function get(string $key)
8997

9098
/**
9199
* Saves an item to the cache store.
100+
* @codeCoverageIgnore
92101
*
93102
* @param string $key Cache item name
94103
* @param mixed $value The data to save
@@ -107,6 +116,7 @@ public function save(string $key, $value, int $ttl = 60)
107116

108117
/**
109118
* Deletes a specific item from the cache store.
119+
* @codeCoverageIgnore
110120
*
111121
* @param string $key Cache item name
112122
*
@@ -124,6 +134,7 @@ public function delete(string $key)
124134
/**
125135
* Performs atomic incrementation of a raw stored value.
126136
*
137+
* @codeCoverageIgnore
127138
* @param string $key Cache ID
128139
* @param integer $offset Step/value to increase by
129140
*
@@ -134,7 +145,7 @@ public function increment(string $key, int $offset = 1)
134145
$key = $this->prefix . $key;
135146

136147
$success = false;
137-
$value = wincache_ucache_inc($key, $offset, $success);
148+
$value = wincache_ucache_inc($key, $offset, $success);
138149

139150
return ($success === true) ? $value : false;
140151
}
@@ -143,6 +154,7 @@ public function increment(string $key, int $offset = 1)
143154

144155
/**
145156
* Performs atomic decrementation of a raw stored value.
157+
* @codeCoverageIgnore
146158
*
147159
* @param string $key Cache ID
148160
* @param integer $offset Step/value to increase by
@@ -154,7 +166,7 @@ public function decrement(string $key, int $offset = 1)
154166
$key = $this->prefix . $key;
155167

156168
$success = false;
157-
$value = wincache_ucache_dec($key, $offset, $success);
169+
$value = wincache_ucache_dec($key, $offset, $success);
158170

159171
return ($success === true) ? $value : false;
160172
}
@@ -163,6 +175,7 @@ public function decrement(string $key, int $offset = 1)
163175

164176
/**
165177
* Will delete all items in the entire cache.
178+
* @codeCoverageIgnore
166179
*
167180
* @return mixed
168181
*/
@@ -175,6 +188,7 @@ public function clean()
175188

176189
/**
177190
* Returns information on the entire cache.
191+
* @codeCoverageIgnore
178192
*
179193
* The information returned and the structure of the data
180194
* varies depending on the handler.
@@ -191,6 +205,7 @@ public function getCacheInfo()
191205
/**
192206
* Returns detailed information about the specific item in the cache.
193207
*
208+
* @codeCoverageIgnore
194209
* @param string $key Cache item name.
195210
*
196211
* @return mixed
@@ -201,15 +216,15 @@ public function getMetaData(string $key)
201216

202217
if ($stored = wincache_ucache_info(false, $key))
203218
{
204-
$age = $stored['ucache_entries'][1]['age_seconds'];
205-
$ttl = $stored['ucache_entries'][1]['ttl_seconds'];
219+
$age = $stored['ucache_entries'][1]['age_seconds'];
220+
$ttl = $stored['ucache_entries'][1]['ttl_seconds'];
206221
$hitcount = $stored['ucache_entries'][1]['hitcount'];
207222

208223
return [
209-
'expire' => $ttl - $age,
210-
'hitcount' => $hitcount,
211-
'age' => $age,
212-
'ttl' => $ttl,
224+
'expire' => $ttl - $age,
225+
'hitcount' => $hitcount,
226+
'age' => $age,
227+
'ttl' => $ttl,
213228
];
214229
}
215230

tests/system/Cache/CacheFactoryTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
<?php namespace CodeIgniter\Cache;
1+
<?php
2+
namespace CodeIgniter\Cache;
23

34
class CacheFactoryTest extends \CIUnitTestCase
45
{
6+
57
private static $directory = 'CacheFactory';
68
private $cacheFactory;
79
private $config;
@@ -13,7 +15,7 @@ protected function setUp()
1315
$this->cacheFactory = new CacheFactory();
1416

1517
//Initialize path
16-
$this->config = new \Config\Cache();
18+
$this->config = new \Config\Cache();
1719
$this->config->storePath .= self::$directory;
1820
}
1921

@@ -77,7 +79,7 @@ public function testGetHandlerExceptionCacheHandlerNotFound()
7779

7880
public function testGetDummyHandler()
7981
{
80-
if (! is_dir($this->config->storePath))
82+
if ( ! is_dir($this->config->storePath))
8183
{
8284
mkdir($this->config->storePath, 0555, true);
8385
}
@@ -87,7 +89,27 @@ public function testGetDummyHandler()
8789
$this->assertInstanceOf(\CodeIgniter\Cache\Handlers\DummyHandler::class, $this->cacheFactory->getHandler($this->config));
8890

8991
//Initialize path
90-
$this->config = new \Config\Cache();
92+
$this->config = new \Config\Cache();
93+
$this->config->storePath .= self::$directory;
94+
}
95+
96+
public function testHandlesBadHandler()
97+
{
98+
if ( ! is_dir($this->config->storePath))
99+
{
100+
mkdir($this->config->storePath, 0555, true);
101+
}
102+
103+
$this->config->handler = 'dummy';
104+
105+
if (stripos('win', php_uname()) === 0)
106+
$this->assertTrue(true); // can't test properly if we are on Windows
107+
else
108+
$this->assertInstanceOf(\CodeIgniter\Cache\Handlers\DummyHandler::class, $this->cacheFactory->getHandler($this->config, 'wincache', 'wincache'));
109+
110+
//Initialize path
111+
$this->config = new \Config\Cache();
91112
$this->config->storePath .= self::$directory;
92113
}
114+
93115
}

0 commit comments

Comments
 (0)