TwoLevelsBackendTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-2014 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. * @category Zend
  33. * @package Zend_Cache
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Cache
  38. */
  39. class Zend_Cache_TwoLevelsBackendTest extends Zend_Cache_CommonExtendedBackendTest {
  40. protected $_instance;
  41. private $_cache_dir;
  42. public function __construct($name = null, array $data = array(), $dataName = '')
  43. {
  44. parent::__construct('Zend_Cache_Backend_TwoLevels', $data, $dataName);
  45. }
  46. public function setUp($notag = false)
  47. {
  48. @mkdir($this->getTmpDir());
  49. $this->_cache_dir = $this->getTmpDir() . DIRECTORY_SEPARATOR;
  50. $slowBackend = 'File';
  51. $fastBackend = 'Apc';
  52. $slowBackendOptions = array(
  53. 'cache_dir' => $this->_cache_dir
  54. );
  55. $fastBackendOptions = array(
  56. );
  57. $this->_instance = new Zend_Cache_Backend_TwoLevels(array(
  58. 'fast_backend' => $fastBackend,
  59. 'slow_backend' => $slowBackend,
  60. 'fast_backend_options' => $fastBackendOptions,
  61. 'slow_backend_options' => $slowBackendOptions
  62. ));
  63. parent::setUp($notag);
  64. }
  65. public function tearDown()
  66. {
  67. parent::tearDown();
  68. unset($this->_instance);
  69. }
  70. public function testConstructorCorrectCall()
  71. {
  72. $slowBackend = 'File';
  73. $fastBackend = 'Apc';
  74. $slowBackendOptions = array(
  75. 'cache_dir' => $this->_cache_dir
  76. );
  77. $fastBackendOptions = array(
  78. );
  79. $test = new Zend_Cache_Backend_TwoLevels(array(
  80. 'fast_backend' => $fastBackend,
  81. 'slow_backend' => $slowBackend,
  82. 'fast_backend_options' => $fastBackendOptions,
  83. 'slow_backend_options' => $slowBackendOptions
  84. ));
  85. }
  86. public function testSaveOverwritesIfFastIsFull()
  87. {
  88. $slowBackend = 'File';
  89. $fastBackend = $this->getMock('Zend_Cache_Backend_Apc', array('getFillingPercentage'));
  90. $fastBackend->expects($this->at(0))
  91. ->method('getFillingPercentage')
  92. ->will($this->returnValue(0));
  93. $fastBackend->expects($this->at(1))
  94. ->method('getFillingPercentage')
  95. ->will($this->returnValue(90));
  96. $slowBackendOptions = array(
  97. 'cache_dir' => $this->_cache_dir
  98. );
  99. $cache = new Zend_Cache_Backend_TwoLevels(array(
  100. 'fast_backend' => $fastBackend,
  101. 'slow_backend' => $slowBackend,
  102. 'slow_backend_options' => $slowBackendOptions,
  103. 'stats_update_factor' => 1
  104. ));
  105. $id = 'test'.uniqid();
  106. $this->assertTrue($cache->save(10, $id)); //fast usage at 0%
  107. $this->assertTrue($cache->save(100, $id)); //fast usage at 90%
  108. $this->assertEquals(100, $cache->load($id));
  109. }
  110. /**
  111. * @group ZF-9855
  112. */
  113. public function testSaveReturnsTrueIfFastIsFullOnFirstSave()
  114. {
  115. $slowBackend = 'File';
  116. $fastBackend = $this->getMock('Zend_Cache_Backend_Apc', array('getFillingPercentage'));
  117. $fastBackend->expects($this->any())
  118. ->method('getFillingPercentage')
  119. ->will($this->returnValue(90));
  120. $slowBackendOptions = array(
  121. 'cache_dir' => $this->_cache_dir
  122. );
  123. $cache = new Zend_Cache_Backend_TwoLevels(array(
  124. 'fast_backend' => $fastBackend,
  125. 'slow_backend' => $slowBackend,
  126. 'slow_backend_options' => $slowBackendOptions,
  127. 'stats_update_factor' => 1
  128. ));
  129. $id = 'test'.uniqid();
  130. $this->assertTrue($cache->save(90, $id)); //fast usage at 90%, failing for
  131. $this->assertEquals(90, $cache->load($id));
  132. $this->assertTrue($cache->save(100, $id)); //fast usage at 90%
  133. $this->assertEquals(100, $cache->load($id));
  134. }
  135. }