[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/fs/tests/cases/ -> lmbCachingFileLocatorTest.class.php (source)

   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  lmb_require('limb/fs/src/lmbFileLocator.class.php');
  10  lmb_require('limb/fs/src/lmbCachingFileLocator.class.php');
  11  
  12  Mock :: generate('lmbFileLocator', 'MockFileLocator');
  13  
  14  class lmbCachingFileLocatorTest extends UnitTestCase
  15  {
  16    var $locator;
  17    var $wrapped_locator;
  18  
  19    function setUp()
  20    {
  21      $this->wrapped_locator = new MockFileLocator();
  22  
  23      $this->locator = new lmbCachingFileLocator($this->wrapped_locator, LIMB_VAR_DIR);
  24      $this->locator->flushCache();
  25  
  26      $this->cache_file = $this->locator->getCacheFile();
  27    }
  28  
  29    function testLocateCachingFromWrappedLocator()
  30    {
  31      $this->wrapped_locator->expectOnce('locate');
  32      $this->wrapped_locator->setReturnValue('locate', 'located-path-to-file', array('path-to-file', array()));
  33  
  34      $this->assertEqual($this->locator->locate('path-to-file'), 'located-path-to-file');
  35    }
  36  
  37    function testLocateCacheHit()
  38    {
  39      $this->wrapped_locator->expectOnce('locate');
  40      $this->wrapped_locator->setReturnValue('locate', 'located-path-to-file', array('path-to-file', array()));
  41  
  42      $this->locator->locate('path-to-file');
  43  
  44      $this->assertEqual($this->locator->locate('path-to-file'), 'located-path-to-file');
  45    }
  46  
  47    function testLocaleNotCacheHitOnOtherParams()
  48    {
  49      $this->wrapped_locator->expectCallCount('locate', 2);
  50      $this->wrapped_locator->setReturnValueAt(0, 'locate', 'located-path-to-file1', array('path-to-file', array()));
  51      $this->wrapped_locator->setReturnValueAt(1, 'locate', 'located-path-to-file2', array('path-to-file', array('param' => 'value')));
  52  
  53      $this->locator->locate('path-to-file');
  54  
  55      $path = $this->locator->locate('path-to-file', array('param' => 'value'));
  56      $this->assertEqual($path, 'located-path-to-file2');
  57    }
  58  
  59    function testWriteToCacheOnDestroy()
  60    {
  61      $this->wrapped_locator->setReturnValue('locate', 'located-path-to-file', array('path-to-file', array()));
  62      $this->locator->locate('path-to-file');
  63  
  64      unset($this->locator);
  65  
  66      $this->assertTrue(file_exists($this->cache_file));
  67  
  68      $cached_locations = unserialize(file_get_contents($this->cache_file));
  69  
  70      $this->assertEqual($cached_locations, array('path-to-file' => 'located-path-to-file'));
  71  
  72      unlink($this->cache_file);
  73    }
  74  
  75    function testWriteToCacheOnlyIfChanged()
  76    {
  77      $this->wrapped_locator->setReturnValue('locate', 'located-path-to-file', array('path-to-file', array()));
  78      $this->locator->locate('path-to-file');
  79  
  80      unset($this->locator);
  81  
  82      $this->assertTrue(file_exists($this->cache_file));
  83  
  84      $locator = new lmbCachingFileLocator($this->wrapped_locator, LIMB_VAR_DIR);
  85  
  86      unlink($this->cache_file);
  87  
  88      $locator->locate('path-to-file');
  89      unset($locator);
  90  
  91      $this->assertFalse(file_exists($this->cache_file));
  92    }
  93  
  94    function testFlushCache()
  95    {
  96      $this->wrapped_locator->setReturnValue('locate', 'located-path-to-file', array('path-to-file', array()));
  97      $this->locator->locate('path-to-file');
  98  
  99      $this->locator->saveCache();
 100      $this->assertTrue(file_exists($this->cache_file));
 101      $this->locator->flushCache();
 102      $this->assertFalse(file_exists($this->cache_file));
 103    }
 104  
 105    function testLoadFromCache()
 106    {
 107      $php = serialize(array("path-to-file" => "located-path-to-file"));
 108      file_put_contents($this->cache_file, $php);
 109  
 110      $this->wrapped_locator->expectNever('locate');
 111  
 112      $local_locator = new lmbCachingFileLocator($this->wrapped_locator, LIMB_VAR_DIR);
 113  
 114      $this->assertEqual($local_locator->locate('path-to-file'), 'located-path-to-file');
 115  
 116      unlink($this->cache_file);
 117    }
 118  }
 119  
 120  ?>


Generated: Sat Aug 30 04:38:32 2008 Cross-referenced by PHPXref 0.7