Skip to content

Commit 2fb7133

Browse files
committed
Test: Improve Cache code coverage
1 parent a73d8ac commit 2fb7133

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

system/Cache/Handlers/WincacheHandler.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
* @since Version 3.0.0
3737
* @filesource
3838
*/
39+
3940
use CodeIgniter\Cache\CacheInterface;
4041

4142
/**
4243
* Cache handler for WinCache from Microsoft & IIS.
4344
* Windows-only, so not testable on travis-ci.
4445
* Unusable methods flagged for code coverage ignoring.
45-
*
4646
*/
4747
class WincacheHandler implements CacheInterface
4848
{
@@ -65,6 +65,7 @@ public function __construct($config)
6565

6666
/**
6767
* Takes care of any handler-specific setup that must be done.
68+
*
6869
* @codeCoverageIgnore
6970
*/
7071
public function initialize()
@@ -76,6 +77,7 @@ public function initialize()
7677

7778
/**
7879
* Attempts to fetch an item from the cache store.
80+
*
7981
* @codeCoverageIgnore
8082
*
8183
* @param string $key Cache item name
@@ -87,7 +89,7 @@ public function get(string $key)
8789
$key = $this->prefix . $key;
8890

8991
$success = false;
90-
$data = wincache_ucache_get($key, $success);
92+
$data = wincache_ucache_get($key, $success);
9193

9294
// Success returned by reference from wincache_ucache_get()
9395
return ($success) ? $data : false;
@@ -97,6 +99,7 @@ public function get(string $key)
9799

98100
/**
99101
* Saves an item to the cache store.
102+
*
100103
* @codeCoverageIgnore
101104
*
102105
* @param string $key Cache item name
@@ -116,6 +119,7 @@ public function save(string $key, $value, int $ttl = 60)
116119

117120
/**
118121
* Deletes a specific item from the cache store.
122+
*
119123
* @codeCoverageIgnore
120124
*
121125
* @param string $key Cache item name
@@ -135,8 +139,8 @@ public function delete(string $key)
135139
* Performs atomic incrementation of a raw stored value.
136140
*
137141
* @codeCoverageIgnore
138-
* @param string $key Cache ID
139-
* @param integer $offset Step/value to increase by
142+
* @param string $key Cache ID
143+
* @param integer $offset Step/value to increase by
140144
*
141145
* @return mixed
142146
*/
@@ -145,7 +149,7 @@ public function increment(string $key, int $offset = 1)
145149
$key = $this->prefix . $key;
146150

147151
$success = false;
148-
$value = wincache_ucache_inc($key, $offset, $success);
152+
$value = wincache_ucache_inc($key, $offset, $success);
149153

150154
return ($success === true) ? $value : false;
151155
}
@@ -154,6 +158,7 @@ public function increment(string $key, int $offset = 1)
154158

155159
/**
156160
* Performs atomic decrementation of a raw stored value.
161+
*
157162
* @codeCoverageIgnore
158163
*
159164
* @param string $key Cache ID
@@ -166,7 +171,7 @@ public function decrement(string $key, int $offset = 1)
166171
$key = $this->prefix . $key;
167172

168173
$success = false;
169-
$value = wincache_ucache_dec($key, $offset, $success);
174+
$value = wincache_ucache_dec($key, $offset, $success);
170175

171176
return ($success === true) ? $value : false;
172177
}
@@ -175,6 +180,7 @@ public function decrement(string $key, int $offset = 1)
175180

176181
/**
177182
* Will delete all items in the entire cache.
183+
*
178184
* @codeCoverageIgnore
179185
*
180186
* @return mixed
@@ -188,6 +194,7 @@ public function clean()
188194

189195
/**
190196
* Returns information on the entire cache.
197+
*
191198
* @codeCoverageIgnore
192199
*
193200
* The information returned and the structure of the data
@@ -206,7 +213,7 @@ public function getCacheInfo()
206213
* Returns detailed information about the specific item in the cache.
207214
*
208215
* @codeCoverageIgnore
209-
* @param string $key Cache item name.
216+
* @param string $key Cache item name.
210217
*
211218
* @return mixed
212219
*/
@@ -216,15 +223,15 @@ public function getMetaData(string $key)
216223

217224
if ($stored = wincache_ucache_info(false, $key))
218225
{
219-
$age = $stored['ucache_entries'][1]['age_seconds'];
220-
$ttl = $stored['ucache_entries'][1]['ttl_seconds'];
226+
$age = $stored['ucache_entries'][1]['age_seconds'];
227+
$ttl = $stored['ucache_entries'][1]['ttl_seconds'];
221228
$hitcount = $stored['ucache_entries'][1]['hitcount'];
222229

223230
return [
224-
'expire' => $ttl - $age,
225-
'hitcount' => $hitcount,
226-
'age' => $age,
227-
'ttl' => $ttl,
231+
'expire' => $ttl - $age,
232+
'hitcount' => $hitcount,
233+
'age' => $age,
234+
'ttl' => $ttl,
228235
];
229236
}
230237

tests/system/Cache/CacheFactoryTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function setUp()
1515
$this->cacheFactory = new CacheFactory();
1616

1717
//Initialize path
18-
$this->config = new \Config\Cache();
18+
$this->config = new \Config\Cache();
1919
$this->config->storePath .= self::$directory;
2020
}
2121

@@ -79,7 +79,7 @@ public function testGetHandlerExceptionCacheHandlerNotFound()
7979

8080
public function testGetDummyHandler()
8181
{
82-
if ( ! is_dir($this->config->storePath))
82+
if (! is_dir($this->config->storePath))
8383
{
8484
mkdir($this->config->storePath, 0555, true);
8585
}
@@ -89,26 +89,30 @@ public function testGetDummyHandler()
8989
$this->assertInstanceOf(\CodeIgniter\Cache\Handlers\DummyHandler::class, $this->cacheFactory->getHandler($this->config));
9090

9191
//Initialize path
92-
$this->config = new \Config\Cache();
92+
$this->config = new \Config\Cache();
9393
$this->config->storePath .= self::$directory;
9494
}
9595

9696
public function testHandlesBadHandler()
9797
{
98-
if ( ! is_dir($this->config->storePath))
98+
if (! is_dir($this->config->storePath))
9999
{
100100
mkdir($this->config->storePath, 0555, true);
101101
}
102102

103103
$this->config->handler = 'dummy';
104104

105105
if (stripos('win', php_uname()) === 0)
106+
{
106107
$this->assertTrue(true); // can't test properly if we are on Windows
108+
}
107109
else
110+
{
108111
$this->assertInstanceOf(\CodeIgniter\Cache\Handlers\DummyHandler::class, $this->cacheFactory->getHandler($this->config, 'wincache', 'wincache'));
112+
}
109113

110114
//Initialize path
111-
$this->config = new \Config\Cache();
115+
$this->config = new \Config\Cache();
112116
$this->config->storePath .= self::$directory;
113117
}
114118

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
<?php namespace CodeIgniter\Cache\Handlers;
1+
<?php
2+
namespace CodeIgniter\Cache\Handlers;
23

3-
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
4+
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline, array $errcontext)
5+
{
46
//throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
57
});
68

79
class FileHandlerTest extends \CIUnitTestCase
810
{
11+
912
private static $directory = 'FileHandler';
10-
private static $key1 = 'key1';
11-
private static $key2 = 'key2';
12-
private static $key3 = 'key3';
13+
private static $key1 = 'key1';
14+
private static $key2 = 'key2';
15+
private static $key3 = 'key3';
1316

1417
private static function getKeyArray()
1518
{
@@ -29,10 +32,10 @@ protected function setUp()
2932
parent::setUp();
3033

3134
//Initialize path
32-
$this->config = new \Config\Cache();
35+
$this->config = new \Config\Cache();
3336
$this->config->storePath .= self::$directory;
3437

35-
if (! is_dir($this->config->storePath))
38+
if ( ! is_dir($this->config->storePath))
3639
{
3740
mkdir($this->config->storePath, 0777, true);
3841
}
@@ -77,7 +80,7 @@ public function testNewWithNonWritablePath()
7780
public function testSetDefaultPath()
7881
{
7982
//Initialize path
80-
$config = new \Config\Cache();
83+
$config = new \Config\Cache();
8184
$config->storePath = null;
8285

8386
$this->fileHandler = new FileHandler($config);
@@ -127,7 +130,9 @@ public function testDecrement()
127130
{
128131
$this->fileHandler->save(self::$key1, 10);
129132
$this->fileHandler->save(self::$key2, 'value');
130-
$this->fileHandler->save(self::$key3, 0);
133+
134+
// Line following commented out to force the cache to add a zero entry for key3
135+
// $this->fileHandler->save(self::$key3, 0);
131136

132137
$this->assertSame(9, $this->fileHandler->decrement(self::$key1, 1));
133138
$this->assertFalse($this->fileHandler->decrement(self::$key2, 1));
@@ -158,6 +163,17 @@ public function testGetMetaData()
158163
$this->assertSame('value', $actual['data']);
159164
}
160165

166+
public function testGetCacheInfo()
167+
{
168+
$time = time();
169+
$this->fileHandler->save(self::$key1, 'value');
170+
171+
$actual = $this->fileHandler->getCacheInfo();
172+
$this->assertArrayHasKey(self::$key1, $actual);
173+
$this->assertEquals(self::$key1,$actual[self::$key1]['name']);
174+
$this->assertArrayHasKey('server_path', $actual[self::$key1]);
175+
}
176+
161177
public function testIsSupported()
162178
{
163179
$this->assertTrue($this->fileHandler->isSupported());
@@ -179,16 +195,18 @@ public function testFileHandler()
179195
$this->assertArrayHasKey('executable', $actual);
180196
$this->assertArrayHasKey('fileperms', $actual);
181197
}
198+
182199
}
183200

184201
final class BaseTestFileHandler extends FileHandler
185202
{
203+
186204
private static $directory = 'FileHandler';
187205
private $config;
188206

189207
public function __construct()
190208
{
191-
$this->config = new \Config\Cache();
209+
$this->config = new \Config\Cache();
192210
$this->config->storePath .= self::$directory;
193211

194212
parent::__construct($this->config);
@@ -200,14 +218,15 @@ public function getFileInfoTest()
200218
stream_get_meta_data($tmp_handle)['uri'];
201219

202220
return $this->getFileInfo(stream_get_meta_data($tmp_handle)['uri'], [
203-
'name',
204-
'server_path',
205-
'size',
206-
'date',
207-
'readable',
208-
'writable',
209-
'executable',
210-
'fileperms',
221+
'name',
222+
'server_path',
223+
'size',
224+
'date',
225+
'readable',
226+
'writable',
227+
'executable',
228+
'fileperms',
211229
]);
212230
}
231+
213232
}

0 commit comments

Comments
 (0)