Guest
Public paste!

Untitled

By: a guest | Feb 20th, 2010 | Syntax: PHP | Size: 4.36 KB | Hits: 78 | Expires: Never
Copy text to clipboard
  1. <?php
  2. /*
  3.  * Limb PHP Framework
  4.  *
  5.  * @link http://limb-project.com
  6.  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  7.  * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  8.  */
  9.  
  10. lmb_require('limb/cache2/src/lmbCacheFactory.class.php');
  11. lmb_require('limb/cache2/src/lmbCacheHelper.class.php');
  12.  
  13. class lmbCacheFactoryTest extends UnitTestCase
  14. {
  15.   function testVars()
  16.   {
  17.     $conf = array(
  18.       'mtime' => array(
  19.         'ttl' => 60,
  20.         'vars' => array('to_int'),
  21.       )
  22.     );
  23.     $helper = $this->_createHelper($conf);
  24.     $time1 = $helper->callCache('mtime', 'microtime', array(false), array('to_int' => false));
  25.     $time2 = $helper->callCache('mtime', 'microtime', array(false), array('to_int' => false));
  26.     $time3 = $helper->callCache('mtime', 'microtime', array(true), array('to_int' => true));
  27.     $time4 = $helper->callCache('mtime', 'microtime', array(true), array('to_int' => true));
  28.     $this->assertEqual($time1, $time2);
  29.     $this->assertEqual($time3, $time4);
  30.     $this->assertNotEqual($time1, $time3);
  31.   }
  32.  
  33.   function testTags()
  34.   {
  35.     $conf = array(
  36.       'mtime' => array(
  37.         'ttl' => 60,
  38.         'tags' => array('reset', 'member'),
  39.       )
  40.     );
  41.     $helper = $this->_createHelper($conf, 'prefix');
  42.     $time1 = $helper->callCache('mtime', 'microtime');
  43.     $time2 = $helper->callCache('mtime', 'microtime');
  44.  
  45.     $this->assertEqual($time1, $time2);
  46.  
  47.     // string
  48.     $helper->deleteByTagsFor('reset');
  49.  
  50.     $time1 = $helper->callCache('mtime', 'microtime');
  51.    
  52.     $this->assertNotEqual($time1, $time2);
  53.  
  54.     $time2 = $helper->callCache('mtime', 'microtime');
  55.     $this->assertEqual($time1, $time2);
  56.  
  57.     // emulate lmbActiveRecoed
  58.     $helper->deleteByTagsFor(new lmbObject(array('table_name' => 'member')));
  59.  
  60.     $time1 = $helper->callCache('mtime', 'microtime');
  61.     $this->assertNotEqual($time1, $time2);
  62.   }
  63.  
  64.   function testTagsIds()
  65.   {
  66.     $conf = array(
  67.       'mtime' => array(
  68.         'ttl' => 60,
  69.         'tags' => array('reset'),
  70.         'tag_ids' => array('member' => 'member_id'),
  71.       )
  72.     );
  73.     $helper = $this->_createHelper($conf, 'prefix');
  74.     $time1 = $helper->callCache('mtime', 'microtime', array(), array('member_id' => 100));
  75.     $time2 = $helper->callCache('mtime', 'microtime', array(), array('member_id' => 100));
  76.  
  77.     $this->assertEqual($time1, $time2);
  78.    
  79.     // refresh by id
  80.     $helper->deleteByTagsFor(new lmbObject(array('table_name' => 'member', 'id' => 100)));
  81.    
  82.     $time1 = $helper->callCache('mtime', 'microtime', array(), array('member_id' => 100));
  83.     $this->assertNotEqual($time1, $time2);
  84.  
  85.     // refresh if change relation
  86.     $time2 = $helper->callCache('mtime', 'microtime', array(), array('member_id' => 100));
  87.     $this->assertEqual($time1, $time2);
  88.  
  89.     $time1 = $helper->callCache('mtime', 'microtime', array(), array('member_id' => 200));
  90.     $this->assertNotEqual($time1, $time2);
  91.   }
  92.  
  93.   function testRequireVars()
  94.   {
  95.     $conf = array(
  96.       'mtime' => array(
  97.         'ttl' => 60,
  98.         'vars' => array('to_int'),
  99.       ),
  100.       'm_time' => array(
  101.         'ttl' => 10,
  102.         'tag_ids' => array('member' => 'member_id'),
  103.       ),
  104.     );
  105.     $helper = $this->_createHelper($conf);
  106.  
  107.     try
  108.     {
  109.       $helper->callCache('mtime', 'microtime');
  110.       $this->assertTrue(false);
  111.     }
  112.     catch(lmbException $e)
  113.     {
  114.       $this->assertTrue(true);
  115.     }
  116.    
  117.     try
  118.     {
  119.       $helper->callCache('m_time', 'microtime');
  120.       $this->assertTrue(false);
  121.     }
  122.     catch(lmbException $e)
  123.     {
  124.       $this->assertTrue(true);
  125.     }
  126.    
  127.     try
  128.     {
  129.       $helper->callCache('m_time', 'microtime', array(), array('member_id' => 1));
  130.       $this->assertTrue(true);
  131.     }
  132.     catch(lmbException $e)
  133.     {
  134.       $this->assertTrue(false);
  135.     }
  136.   }
  137.  
  138.   function testBadCacheName()
  139.   {
  140.     $helper = $this->_createHelper(array('mtime' => array('ttl' => 100)));
  141.     try
  142.     {
  143.       $helper->callCache('m_time', 'microtime');
  144.       $this->assertTrue(false);
  145.     }
  146.     catch(lmbException $e)
  147.     {
  148.       $this->assertTrue(true);
  149.     }
  150.   }
  151.  
  152.   protected function _createHelper($conf, $prefix = '')
  153.   {
  154.     $connection = lmbCacheFactory :: createConnection('file:///'.lmb_var_dir().'/cache2_helper');
  155.     $connection->flush();
  156.     return new lmbCacheHelper($connection, $conf, $prefix);
  157.   }
  158. }