ExpiringCacheTest.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Test\Cache;
  11. use Assetic\Cache\ExpiringCache;
  12. class ExpiringCacheTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $inner;
  15. private $lifetime;
  16. private $cache;
  17. protected function setUp()
  18. {
  19. $this->inner = $this->getMock('Assetic\\Cache\\CacheInterface');
  20. $this->lifetime = 3600;
  21. $this->cache = new ExpiringCache($this->inner, $this->lifetime);
  22. }
  23. public function testHasExpired()
  24. {
  25. $key = 'asdf';
  26. $expiresKey = 'asdf.expires';
  27. $thePast = 0;
  28. $this->inner->expects($this->once())
  29. ->method('has')
  30. ->with($key)
  31. ->will($this->returnValue(true));
  32. $this->inner->expects($this->once())
  33. ->method('get')
  34. ->with($expiresKey)
  35. ->will($this->returnValue($thePast));
  36. $this->inner->expects($this->at(2))
  37. ->method('remove')
  38. ->with($expiresKey);
  39. $this->inner->expects($this->at(3))
  40. ->method('remove')
  41. ->with($key);
  42. $this->assertFalse($this->cache->has($key), '->has() returns false if an expired value exists');
  43. }
  44. public function testHasNotExpired()
  45. {
  46. $key = 'asdf';
  47. $expiresKey = 'asdf.expires';
  48. $theFuture = time() * 2;
  49. $this->inner->expects($this->once())
  50. ->method('has')
  51. ->with($key)
  52. ->will($this->returnValue(true));
  53. $this->inner->expects($this->once())
  54. ->method('get')
  55. ->with($expiresKey)
  56. ->will($this->returnValue($theFuture));
  57. $this->assertTrue($this->cache->has($key), '->has() returns true if a value the not expired');
  58. }
  59. public function testSetLifetime()
  60. {
  61. $key = 'asdf';
  62. $expiresKey = 'asdf.expires';
  63. $value = 'qwerty';
  64. $this->inner->expects($this->at(0))
  65. ->method('set')
  66. ->with($expiresKey, $this->greaterThanOrEqual(time() + $this->lifetime));
  67. $this->inner->expects($this->at(1))
  68. ->method('set')
  69. ->with($key, $value);
  70. $this->cache->set($key, $value);
  71. }
  72. public function testRemove()
  73. {
  74. $key = 'asdf';
  75. $expiresKey = 'asdf.expires';
  76. $this->inner->expects($this->at(0))
  77. ->method('remove')
  78. ->with($expiresKey);
  79. $this->inner->expects($this->at(1))
  80. ->method('remove')
  81. ->with($key);
  82. $this->cache->remove($key);
  83. }
  84. public function testGet()
  85. {
  86. $this->inner->expects($this->once())
  87. ->method('get')
  88. ->with('foo')
  89. ->will($this->returnValue('bar'));
  90. $this->assertEquals('bar', $this->cache->get('foo'), '->get() returns the cached value');
  91. }
  92. }