public function testFileCacheClassShouldImplementCacheInterface() { $fileCache = new FileCache(); $this->assertInstanceOf('CacheInterface', $fileCache); }
interface CacheInterface { /** * @abstract * @param string $id * @param mixed $data * @return bool */ public function save($id, $data); /** * @abstract * @param string $id * @return mixed */ public function load($id); }
class FileCache implements CacheInterface { public function save($id, $data){} public function load($id) {} }
class FileCacheTest extends PHPUnit_Framework_TestCase { protected $cacheDir = './cache_data'; protected function setUp() { //Create cache dir if (file_exists($this->cacheDir)) { $this->_removeCacheDir(); } mkdir($this->cacheDir); } public function tearDown() { //remove cache dir $this->_removeCacheDir(); } protected function _removeCacheDir() { $dir = opendir($this->cacheDir); if ($dir) { while ($file = readdir($dir)) { if ($file != '.' && $file != '..') { unlink($this->cacheDir . '/' . $file); } } } closedir($dir); rmdir($this->cacheDir); } }
public function testSettingCacheDir() { $beforeFilesCount = count(scandir($this->cacheDir)); $fileCache = new FileCache($this->cacheDir); $fileCache->save('data_name', 'some data'); $afterFilesCount = count(scandir($this->cacheDir)); $this->assertTrue($afterFilesCount > $beforeFilesCount); }
class FileCache implements CacheInterface { /** * @var string */ protected $cacheDir; /** * @param string $cacheDir */ public function __construct($cacheDir = '.') { $this->cacheDir = $cacheDir; } /** * @param string $id * @param mixed $data * @return bool */ public function save($id, $data) { $filename = $this->cacheDir . '/' . $id . '.dat'; $f = fopen($filename, 'w'); fwrite($f, serialize($data)); fclose($f); return true; } }
public function testSettingCacheLifetime() { $lifetime = 2; $cacheData = 'data'; $cacheId = 'expires'; $fileCache = new FileCache($this->cacheDir, $lifetime); $fileCache->save($cacheId, $cacheData); $this->assertEquals($cacheData, $fileCache->load($cacheId)); sleep(3); $this->assertFalse($fileCache->load($cacheId)); }
/** * @param string $cacheDir * @param int $lifetime */ public function __construct($cacheDir = '.', $lifetime = 3600) { $this->cacheDir = $cacheDir; $this->lifetime = $lifetime; }
/** * @param string $id * @return mixed */ public function load($id) { $filename = $this->cacheDir . '/' . $id . '.dat'; if (time() - fileatime($filename) > $this->lifetime) { return false; } return unserialize(file_get_contents($filename)); }
protected function _createFilename($id) { return $this->cacheDir . '/' . $id . '.dat'; }
public function testLoadShouldReturnFalseOnNonexistId() { $fileCache = new FileCache($this->cacheDir); $fileCache->save('id', 'some data'); $this->assertFalse($fileCache->load('non_exist')); }
public function load($id) { $filename = $this->_createFilename($id); if (!file_exists($filename)) { return false; } if (time() - fileatime($filename) > $this->lifetime) { return false; } return unserialize(file_get_contents($filename)); }
public function testTwitterShouldCallHttpClientWithCorrectUrl() { $httpClient = $this->getMock('HttpClientInterface'); $nickname = 'test_nick'; $twitter = new Twitter($httpClient); $httpClient ->expects($this->once()) ->method('get') ->with($this->equalTo('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $nickname)); $twitter->getStatuses($nickname); }
public function testTwitterShouldLoadDataFromCacheIfIsPossible() { $cache = $this->getMock('CacheInterface'); $httpClient = $this->getMock('HttpClientInterface'); $nickname = 'test_nick'; $twitter = new Twitter($httpClient); $url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $nickname; $urlMd5 = md5($url); $resultCached = array('status1', 'status2', 'status3'); $resultNotCached = array('save_to_cache'); $twitter->setCache($cache); $cache->expects($this->at(0))->method('load')->with($this->equalTo($urlMd5))->will($this->returnValue($resultCached)); $cache->expects($this->at(1))->method('load')->with($this->equalTo($urlMd5))->will($this->returnValue(false)); $httpClient->expects($this->once())->method('get')->with($this->equalTo($url))->will($this->returnValue($resultNotCached)); $cache->expects($this->once())->method('save')->with($this->equalTo($urlMd5), $this->equalTo($resultNotCached)); $this->assertEquals($resultCached, $twitter->getStatuses($nickname)); $this->assertEquals($resultNotCached, $twitter->getStatuses($nickname)); }
class Twitter { /** * @var HttpClientInterface */ protected $httpClient; /** * @var string */ protected $methodUrl = 'http://api.twitter.com/1/statuses/user_timeline.json'; /** * @var CacheInterface */ protected $cache = null; /** * @param HttpClientInterface $httpClient */ public function __construct(HttpClientInterface $httpClient) { $this->httpClient = $httpClient; } /** * @param CacheInterface $cache * @return Twitter */ public function setCache(CacheInterface $cache) { $this->cache = $cache; return $this; } /** * @param string $nickname * @return mixed */ public function getStatuses($nickname) { $url = $this->methodUrl . '?screen_name=' . $nickname; $cache = $this->cache; $cacheId = md5($url); $data = false; if ($cache !== null) { $data = $cache->load($cacheId); } if ($data === false) { $data = $this->httpClient->get($url); if ($cache !== null) { $cache->save($cacheId, $data); } } return $data; } }
Source: https://habr.com/ru/post/130086/
All Articles