TwoLevelsBackendTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Cache
  24. */
  25. require_once 'Zend/Cache.php';
  26. require_once 'Zend/Cache/Backend/TwoLevels.php';
  27. /**
  28. * Common tests for backends
  29. */
  30. require_once 'CommonExtendedBackendTest.php';
  31. /**
  32. * PHPUnit test case
  33. */
  34. require_once 'PHPUnit/Framework/TestCase.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Cache
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_Cache
  42. */
  43. class Zend_Cache_TwoLevelsBackendTest extends Zend_Cache_CommonExtendedBackendTest {
  44. protected $_instance;
  45. private $_cache_dir;
  46. public function __construct($name = null, array $data = array(), $dataName = '')
  47. {
  48. parent::__construct('Zend_Cache_Backend_TwoLevels', $data, $dataName);
  49. }
  50. public function setUp($notag = false)
  51. {
  52. @mkdir($this->getTmpDir());
  53. $this->_cache_dir = $this->getTmpDir() . DIRECTORY_SEPARATOR;
  54. $slowBackend = 'File';
  55. $fastBackend = 'Apc';
  56. $slowBackendOptions = array(
  57. 'cache_dir' => $this->_cache_dir
  58. );
  59. $fastBackendOptions = array(
  60. );
  61. $this->_instance = new Zend_Cache_Backend_TwoLevels(array(
  62. 'fast_backend' => $fastBackend,
  63. 'slow_backend' => $slowBackend,
  64. 'fast_backend_options' => $fastBackendOptions,
  65. 'slow_backend_options' => $slowBackendOptions
  66. ));
  67. parent::setUp($notag);
  68. }
  69. public function tearDown()
  70. {
  71. parent::tearDown();
  72. unset($this->_instance);
  73. }
  74. public function testConstructorCorrectCall()
  75. {
  76. $slowBackend = 'File';
  77. $fastBackend = 'Apc';
  78. $slowBackendOptions = array(
  79. 'cache_dir' => $this->_cache_dir
  80. );
  81. $fastBackendOptions = array(
  82. );
  83. $test = new Zend_Cache_Backend_TwoLevels(array(
  84. 'fast_backend' => $fastBackend,
  85. 'slow_backend' => $slowBackend,
  86. 'fast_backend_options' => $fastBackendOptions,
  87. 'slow_backend_options' => $slowBackendOptions
  88. ));
  89. }
  90. public function testSaveOverwritesIfFastIsFull()
  91. {
  92. $slowBackend = 'File';
  93. $fastBackend = $this->getMock('Zend_Cache_Backend_Apc', array('getFillingPercentage'));
  94. $fastBackend->expects($this->at(0))
  95. ->method('getFillingPercentage')
  96. ->will($this->returnValue(0));
  97. $fastBackend->expects($this->at(1))
  98. ->method('getFillingPercentage')
  99. ->will($this->returnValue(90));
  100. $slowBackendOptions = array(
  101. 'cache_dir' => $this->_cache_dir
  102. );
  103. $cache = new Zend_Cache_Backend_TwoLevels(array(
  104. 'fast_backend' => $fastBackend,
  105. 'slow_backend' => $slowBackend,
  106. 'slow_backend_options' => $slowBackendOptions,
  107. 'stats_update_factor' => 1
  108. ));
  109. $id = 'test'.uniqid();
  110. $this->assertTrue($cache->save(10, $id)); //fast usage at 0%
  111. $this->assertTrue($cache->save(100, $id)); //fast usage at 90%
  112. $this->assertEquals(100, $cache->load($id));
  113. }
  114. /**
  115. * @group ZF-9855
  116. */
  117. public function testSaveReturnsTrueIfFastIsFullOnFirstSave()
  118. {
  119. $slowBackend = 'File';
  120. $fastBackend = $this->getMock('Zend_Cache_Backend_Apc', array('getFillingPercentage'));
  121. $fastBackend->expects($this->any())
  122. ->method('getFillingPercentage')
  123. ->will($this->returnValue(90));
  124. $slowBackendOptions = array(
  125. 'cache_dir' => $this->_cache_dir
  126. );
  127. $cache = new Zend_Cache_Backend_TwoLevels(array(
  128. 'fast_backend' => $fastBackend,
  129. 'slow_backend' => $slowBackend,
  130. 'slow_backend_options' => $slowBackendOptions,
  131. 'stats_update_factor' => 1
  132. ));
  133. $id = 'test'.uniqid();
  134. $this->assertTrue($cache->save(90, $id)); //fast usage at 90%, failing for
  135. $this->assertEquals(90, $cache->load($id));
  136. $this->assertTrue($cache->save(100, $id)); //fast usage at 90%
  137. $this->assertEquals(100, $cache->load($id));
  138. }
  139. }