Skip to content

Commit

Permalink
add test for model fpc
Browse files Browse the repository at this point in the history
  • Loading branch information
GordonLesti committed Nov 25, 2014
1 parent 6e2c520 commit e26ce65
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
6 changes: 3 additions & 3 deletions app/code/community/Lesti/Fpc/Model/Fpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function save($data, $id, $tags=array(), $lifeTime=null)
if (!in_array(self::CACHE_TAG, $tags)) {
$tags[] = self::CACHE_TAG;
}
if ($lifeTime === null) {
if (is_null($lifeTime)) {
$lifeTime = (int) $this->getFrontend()->getOption('lifetime');
}
// edit cached object
Expand All @@ -96,7 +96,7 @@ public function save($data, $id, $tags=array(), $lifeTime=null)
if ($compressLevel != -2) {
$data = gzcompress($data, $compressLevel);
}

return $this->_frontend->save(
$data,
$this->_id($id),
Expand All @@ -113,7 +113,7 @@ public function load($id)
{
$data = parent::load($id);
$compressLevel = Mage::getStoreConfig(self::GZCOMPRESS_LEVEL_XML_PATH);
if ($compressLevel != -2) {
if ($data !== false && $compressLevel != -2) {
$data = gzuncompress($data);
}

Expand Down
10 changes: 7 additions & 3 deletions app/code/community/Lesti/Fpc/Model/Observer/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ protected function _cmsBlockSaveAfter(Mage_Cms_Model_Block $block)
*/
protected function _catalogProductSaveAfterMassAction(array $productIds)
{
foreach ($productIds as $productId) {
$this->_getFpc()->clean(sha1('product_' . $productId));
if (!empty($productIds)) {
$tags = array();
foreach ($productIds as $productId) {
$tags[] = sha1('product_' . $productId);
}
$this->_getFpc()->clean($tags);
}
}
}
}
50 changes: 40 additions & 10 deletions app/code/community/Lesti/Fpc/Test/Model/Fpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,51 @@
/**
* Class Lesti_Fpc_Test_Model_Fpc
*/
class Lesti_Fpc_Test_Model_Fpc extends EcomDev_PHPUnit_Test_Case
class Lesti_Fpc_Test_Model_Fpc extends Lesti_Fpc_Test_TestCase
{
/**
* @var Lesti_Fpc_Model_Fpc
*/
protected $_fpc;

protected function setUp()
{
parent::setUp();
$this->_fpc = Mage::getSingleton('fpc/fpc');
}

/**
* @test
* @loadFixture save_load_clean.yaml
*/
public function saveAndLoad()
public function saveLoadClean()
{
$fpc = Mage::getSingleton('fpc/fpc');
$key = 'lesti_fpc';
$value = 'test';
$fpc->save($value, $key);
$this->assertTrue($fpc->load($key) === $value);
$fpc->remove($key);
$this->assertTrue($fpc->load($key) === false);
}
$data = 'fpc_data';
$id = 'fpc_id';
$tag = 'tag1';

// check if tag tag1 (clean without array)
$this->_fpc->save($data, $id, array($tag));
$this->assertEquals($data, $this->_fpc->load($id));
$this->_fpc->clean($tag);
$this->assertFalse($this->_fpc->load($id));

// check global tag FPC (clean with array)
$this->_fpc->save($data, $id, array($tag));
$this->assertEquals($data, $this->_fpc->load($id));
$this->_fpc->clean(array(Lesti_Fpc_Model_Fpc::CACHE_TAG));
$this->assertFalse($this->_fpc->load($id));

// (global clean)
$this->_fpc->save($data, $id, array($tag));
$this->assertEquals($data, $this->_fpc->load($id));
$this->_fpc->clean();
$this->assertFalse($this->_fpc->load($id));

// check timeout
$this->_fpc->save($data, $id, array($tag), 2);
$this->assertEquals($data, $this->_fpc->load($id));
sleep(3);
$this->assertFalse($this->_fpc->load($id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config:
default/system/fpc/gzcompress_level: 3

0 comments on commit e26ce65

Please sign in to comment.