Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
romeOz committed Oct 12, 2015
1 parent ef30e2b commit 4334eed
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 192 deletions.
156 changes: 0 additions & 156 deletions tests/MongoCacheAdvancedTest.php

This file was deleted.

149 changes: 114 additions & 35 deletions tests/MongoCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace rockunit;


use rock\cache\CacheInterface;
use rock\helpers\Instance;
use rock\cache\MongoCache;

Expand All @@ -22,6 +22,18 @@ protected function tearDown()
parent::tearDown();
}

public function setUp()
{
$this->createCache()->flush();
}

public function providerCache()
{
return [
[$this->createCache()],
];
}

/**
* Creates test cache instance.
* @return \rock\cache\MongoCache cache instance.
Expand All @@ -39,70 +51,137 @@ protected function createCache()
$collection->createIndex('expire', ['expireAfterSeconds' => 0]);
return Instance::ensure([
'class' => MongoCache::className(),
'storage' => $connection,
'storage' => $connection,
'cacheCollection' => static::$cacheCollection,
'hashKey' => 0,
]);
}

// Tests:

public function testSet()
/**
* @dataProvider providerCache
*/
public function testGetStorage(CacheInterface $cache)
{
$cache = $this->createCache();
//$cache->flush();
$key = 'test_key';
$value = ['name' => 'Tom', 'age' => 20];
$this->assertTrue($cache->set($key, $value), 'Unable to set value!');
$this->assertEquals($value, $cache->get($key), 'Unable to set value correctly!');

$newValue = 'test_new_value';
$this->assertTrue($cache->set($key, $newValue), 'Unable to update value!');
$this->assertEquals($newValue, $cache->get($key), 'Unable to update value correctly!');
$this->assertTrue($cache->getStorage() instanceof \rock\mongodb\Connection);
}

public function testAdd()

/**
* @dataProvider providerCache
* @expectedException \rock\cache\CacheException
*/
public function testGetTag(CacheInterface $cache)
{
$cache = $this->createCache();
/** @var $this \PHPUnit_Framework_TestCase */
$cache->getTag('foo');
}


$key = 'test_key';
$value = 'test_value';
$this->assertTrue($cache->add($key, $value), 'Unable to add value!');
$this->assertEquals($value, $cache->get($key), 'Unable to add value correctly!');
/**
* @dataProvider providerCache
* @expectedException \rock\cache\CacheException
*/
public function testGetTags(CacheInterface $cache)
{
/** @var $this \PHPUnit_Framework_TestCase */

$newValue = 'test_new_value';
$this->assertFalse($cache->add($key, $newValue), 'Unable to re-add value!');
$this->assertEquals($value, $cache->get($key), 'Original value is lost!');
$cache->getMultiTags(['bar', 'foo']);
}
/**
* @dataProvider providerCache
* @param CacheInterface $cache
* @expectedException \rock\cache\CacheException
*/
public function testGetHashMd5Tags(CacheInterface $cache)
{
parent::testGetHashMd5Tags($cache);
}

/**
* @depends testSet
* @dataProvider providerCache
* @param CacheInterface $cache
* @expectedException \rock\cache\CacheException
*/
public function testDelete()
public function testGetHashSHATags(CacheInterface $cache)
{
$cache = $this->createCache();
parent::testGetHashSHATags($cache);
}

$key = 'test_key';
$value = 'test_value';
$cache->set($key, $value);
/**
* @dataProvider providerCache
* @expectedException \rock\cache\CacheException
*/
public function testExistsTag(CacheInterface $cache)
{
$cache->existsTag('foo');
}

/**
* @dataProvider providerCache
* @expectedException \rock\cache\CacheException
*/
public function testExistsTagFalse(CacheInterface $cache)
{
$cache->existsTag('baz');
}

/**
* @dataProvider providerCache
*/
public function testRemoveTag(CacheInterface $cache)
{
/** @var $this \PHPUnit_Framework_TestCase */

$this->assertTrue($cache->remove($key), 'Unable to delete key!');
$this->assertEquals(false, $cache->get($key), 'Value is not deleted!');
$this->assertTrue($cache->set('key1', ['one', 'two'], 0, ['foo', 'bar']));
$this->assertTrue($cache->set('key2', 'three', 0, ['foo']));
$this->assertTrue($cache->removeTag('bar'), 'should be get: true');
$this->assertFalse($cache->get('key1'), 'should be get: false');
}

/**
* @depends testSet
* @dataProvider providerCache
*/
public function testFlush()
public function testGetAllKeys(CacheInterface $cache)
{
$cache = $this->createCache();
/** @var $this \PHPUnit_Framework_TestCase */

$this->assertTrue($cache->set('key1', ['one', 'two'], 0, ['foo', 'bar']));
$this->assertTrue($cache->set('key2', 'three', 0, ['foo']));
$expected = $cache->getAllKeys();
if ($expected !== false) {
$actual = [
$cache->prepareKey('key1'),
$cache->prepareKey('key2'),
];
sort($expected);
sort($actual);
$this->assertEquals($expected, $actual, 'should be get: ' . json_encode($actual));
}
}

/**
* @dataProvider providerCache
* @expectedException \rock\cache\CacheException
*/
public function testStatus(CacheInterface $cache)
{
$cache->status();
}

/**
* @dataProvider providerCache
*/
public function testFlush(CacheInterface $cache)
{
$cache->set('key1', 'value1');
$cache->set('key2', 'value2');

$this->assertTrue($cache->flush(), 'Unable to flush cache!');

$collection = $cache->getStorage()->getCollection($cache->cacheCollection);
$collection = $cache->getStorage()->getCollection(self::$cacheCollection);
$rows = $this->findAll($collection);
$this->assertCount(0, $rows, 'Unable to flush records!');
}
}
}
6 changes: 5 additions & 1 deletion tests/MongoDbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use rock\base\Alias;
use rock\mongodb\MongoException;

class MongoDbTestCase extends \PHPUnit_Framework_TestCase
class MongoDbTestCase extends CommonCache
{
public static $params;
/**
Expand Down Expand Up @@ -33,6 +33,10 @@ protected function setUp()
}
}

public function init($serialize)
{
}

protected function tearDown()
{
if ($this->mongodb) {
Expand Down

0 comments on commit 4334eed

Please sign in to comment.