Skip to content
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

Improve tests for cache drivers #64

Merged
merged 1 commit into from
Sep 1, 2021
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
17 changes: 0 additions & 17 deletions tests/CacheCoreTest.php

This file was deleted.

4 changes: 3 additions & 1 deletion tests/CacheFileTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

use Nano\Cache;

/**
* Set of unit tests for Cache file driver
*/

class CacheFileTest extends CacheTest
{
protected function getCacheInstance($settings = [])
protected function getCacheInstance(array $settings = []): Cache
{
$dir = dirname(__FILE__) . '/app/cache';

Expand Down
22 changes: 20 additions & 2 deletions tests/CacheRedisTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Nano\Cache;

/**
* Set of unit tests for Cache redis driver
*
Expand All @@ -10,9 +12,10 @@ class CacheRedisTest extends CacheTest

/**
* @param array $settings
* @return \Nano\Cache
* @return Cache
* @throws Exception
*/
protected function getCacheInstance($settings = [])
protected function getCacheInstance(array $settings = []): Cache
{
$settings = array_merge([
'host' => '127.0.0.1',
Expand All @@ -21,4 +24,19 @@ protected function getCacheInstance($settings = [])

return $this->getCache('redis', $settings);
}

/**
* @throws Exception
*/
public function testSetCacheInfinity()
{
$cache = $this->getCacheInstance();

$key = 'foo';
$value = 'bar';

$this->assertTrue($cache->set($key, $value));
$this->assertTrue($cache->exists($key));
$this->assertEquals($value, $cache->get($key));
}
}
46 changes: 29 additions & 17 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

use Nano\Cache;
use Nano\NanoBaseTest;

/**
* Generic class for unit tests for Cache drivers
*/
abstract class CacheTest extends \Nano\NanoBaseTest
abstract class CacheTest extends NanoBaseTest
{
protected function getCache($driver, array $settings = [])
/**
* @throws Exception
*/
protected function getCache($driver, array $settings = []): Cache
{
// use test application's directory
$dir = realpath(dirname(__FILE__) . '/app');
Expand All @@ -21,11 +25,29 @@ protected function getCache($driver, array $settings = [])
return Cache::factory($settings);
}

public function testCacheFactory()
/**
* @param string $cacheDriver
* @param array $cacheOptions
* @param string $expectedClass
* @dataProvider cacheFactoryProvider
* @throws Exception
*/
public function testCacheFactory(string $cacheDriver, array $cacheOptions, string $expectedClass)
{
$this->assertInstanceOf('Nano\Cache\CacheFile', $this->getCache('file'));
$this->assertInstanceOf('Nano\Cache\CacheFile', $this->getCache('File'));
$this->assertInstanceOf('Nano\Cache\CacheRedis', $this->getCache('redis', ['host' => '127.0.0.1']));
$this->assertInstanceOf($expectedClass, $this->getCache($cacheDriver, $cacheOptions));
}

public function cacheFactoryProvider(): Generator
{
yield 'file' => [
'file', [], Cache\CacheFile::class
];
yield 'File' => [
'File', [], Cache\CacheFile::class
];
yield 'redis' => [
'redis', ['host' => '127.0.0.1'], Cache\CacheRedis::class
];
}

/**
Expand All @@ -34,14 +56,11 @@ public function testCacheFactory()
* @param array $settings
* @return Cache
*/
abstract protected function getCacheInstance($settings = []);
abstract protected function getCacheInstance(array $settings = []): Cache;

public function testCacheGetSet()
{
$cache = $this->getCacheInstance();
if ($cache === false) {
return;
}

$key = 'foo';
$value = [
Expand Down Expand Up @@ -82,9 +101,6 @@ public function testCacheGetSet()
public function testCacheIncrDecr()
{
$cache = $this->getCacheInstance();
if ($cache === false) {
return;
}

$key = 'bar';
$value = 12;
Expand Down Expand Up @@ -114,10 +130,6 @@ public function testCachePrefix()
$cacheA = $this->getCacheInstance();
$cacheB = $this->getCacheInstance(['prefix' => 'foo']);

if ($cacheA === false) {
return;
}

$key = 'bar';
$value = 12;

Expand Down