Skip to content

Test: Improve Cache coverage #1796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php namespace CodeIgniter\Cache\Handlers;
<?php
namespace CodeIgniter\Cache\Handlers;

/**
* CodeIgniter
Expand Down Expand Up @@ -38,6 +39,11 @@

use CodeIgniter\Cache\CacheInterface;

/**
* Cache handler for WinCache from Microsoft & IIS.
* Windows-only, so not testable on travis-ci.
* Unusable methods flagged for code coverage ignoring.
*/
class WincacheHandler implements CacheInterface
{

Expand All @@ -59,6 +65,8 @@ public function __construct($config)

/**
* Takes care of any handler-specific setup that must be done.
*
* @codeCoverageIgnore
*/
public function initialize()
{
Expand All @@ -70,6 +78,8 @@ public function initialize()
/**
* Attempts to fetch an item from the cache store.
*
* @codeCoverageIgnore
*
* @param string $key Cache item name
*
* @return mixed
Expand All @@ -90,6 +100,8 @@ public function get(string $key)
/**
* Saves an item to the cache store.
*
* @codeCoverageIgnore
*
* @param string $key Cache item name
* @param mixed $value The data to save
* @param integer $ttl Time To Live, in seconds (default 60)
Expand All @@ -108,6 +120,8 @@ public function save(string $key, $value, int $ttl = 60)
/**
* Deletes a specific item from the cache store.
*
* @codeCoverageIgnore
*
* @param string $key Cache item name
*
* @return mixed
Expand All @@ -124,8 +138,9 @@ public function delete(string $key)
/**
* Performs atomic incrementation of a raw stored value.
*
* @param string $key Cache ID
* @param integer $offset Step/value to increase by
* @codeCoverageIgnore
* @param string $key Cache ID
* @param integer $offset Step/value to increase by
*
* @return mixed
*/
Expand All @@ -144,6 +159,8 @@ public function increment(string $key, int $offset = 1)
/**
* Performs atomic decrementation of a raw stored value.
*
* @codeCoverageIgnore
*
* @param string $key Cache ID
* @param integer $offset Step/value to increase by
*
Expand All @@ -164,6 +181,8 @@ public function decrement(string $key, int $offset = 1)
/**
* Will delete all items in the entire cache.
*
* @codeCoverageIgnore
*
* @return mixed
*/
public function clean()
Expand All @@ -176,6 +195,8 @@ public function clean()
/**
* Returns information on the entire cache.
*
* @codeCoverageIgnore
*
* The information returned and the structure of the data
* varies depending on the handler.
*
Expand All @@ -191,7 +212,8 @@ public function getCacheInfo()
/**
* Returns detailed information about the specific item in the cache.
*
* @param string $key Cache item name.
* @codeCoverageIgnore
* @param string $key Cache item name.
*
* @return mixed
*/
Expand Down
28 changes: 27 additions & 1 deletion tests/system/Cache/CacheFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace CodeIgniter\Cache;
<?php
namespace CodeIgniter\Cache;

class CacheFactoryTest extends \CIUnitTestCase
{

private static $directory = 'CacheFactory';
private $cacheFactory;
private $config;
Expand Down Expand Up @@ -90,4 +92,28 @@ public function testGetDummyHandler()
$this->config = new \Config\Cache();
$this->config->storePath .= self::$directory;
}

public function testHandlesBadHandler()
{
if (! is_dir($this->config->storePath))
{
mkdir($this->config->storePath, 0555, true);
}

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

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

//Initialize path
$this->config = new \Config\Cache();
$this->config->storePath .= self::$directory;
}

}
22 changes: 20 additions & 2 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php namespace CodeIgniter\Cache\Handlers;
<?php
namespace CodeIgniter\Cache\Handlers;

set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {
//throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

class FileHandlerTest extends \CIUnitTestCase
{

private static $directory = 'FileHandler';
private static $key1 = 'key1';
private static $key2 = 'key2';
Expand Down Expand Up @@ -127,7 +129,9 @@ public function testDecrement()
{
$this->fileHandler->save(self::$key1, 10);
$this->fileHandler->save(self::$key2, 'value');
$this->fileHandler->save(self::$key3, 0);

// Line following commented out to force the cache to add a zero entry for key3
// $this->fileHandler->save(self::$key3, 0);

$this->assertSame(9, $this->fileHandler->decrement(self::$key1, 1));
$this->assertFalse($this->fileHandler->decrement(self::$key2, 1));
Expand Down Expand Up @@ -158,6 +162,17 @@ public function testGetMetaData()
$this->assertSame('value', $actual['data']);
}

public function testGetCacheInfo()
{
$time = time();
$this->fileHandler->save(self::$key1, 'value');

$actual = $this->fileHandler->getCacheInfo();
$this->assertArrayHasKey(self::$key1, $actual);
$this->assertEquals(self::$key1, $actual[self::$key1]['name']);
$this->assertArrayHasKey('server_path', $actual[self::$key1]);
}

public function testIsSupported()
{
$this->assertTrue($this->fileHandler->isSupported());
Expand All @@ -179,10 +194,12 @@ public function testFileHandler()
$this->assertArrayHasKey('executable', $actual);
$this->assertArrayHasKey('fileperms', $actual);
}

}

final class BaseTestFileHandler extends FileHandler
{

private static $directory = 'FileHandler';
private $config;

Expand Down Expand Up @@ -210,4 +227,5 @@ public function getFileInfoTest()
'fileperms',
]);
}

}