TwoLevelsBackendTest.php 4.8 KB

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