MemoryManagerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_Memory
  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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Memory_MemoryManagerTest::main');
  24. }
  25. /** Zend_Memory */
  26. require_once 'Zend/Memory.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Memory
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Memory
  34. */
  35. class Zend_Memory_MemoryManagerTest extends PHPUnit_Framework_TestCase
  36. {
  37. public static function main()
  38. {
  39. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  40. $result = PHPUnit_TextUI_TestRunner::run($suite);
  41. }
  42. public function setUp()
  43. {
  44. $tmpDir = sys_get_temp_dir() . '/zend_memory';
  45. $this->_removeCacheDir($tmpDir);
  46. mkdir($tmpDir);
  47. $this->cacheDir = $tmpDir;
  48. }
  49. protected function _removeCacheDir($dir)
  50. {
  51. if (!file_exists($dir)) {
  52. return true;
  53. }
  54. if (!is_dir($dir) || is_link($dir)) {
  55. return unlink($dir);
  56. }
  57. foreach (scandir($dir) as $item) {
  58. if ($item == '.' || $item == '..') {
  59. continue;
  60. }
  61. $this->_removeCacheDir($dir . '/' . $item);
  62. }
  63. return rmdir($dir);
  64. }
  65. /**
  66. * tests the Memory Manager creation
  67. *
  68. */
  69. public function testCreation()
  70. {
  71. /** 'File' backend */
  72. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  73. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  74. $this->assertTrue($memoryManager instanceof Zend_Memory_Manager);
  75. }
  76. /**
  77. * tests the Memory Manager backends naming processing
  78. *
  79. * @group ZF-9023
  80. */
  81. public function testBackendNamingProcessing()
  82. {
  83. /** 'File' backend */
  84. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  85. $memoryManager = Zend_Memory::factory('fIlE', $backendOptions);
  86. }
  87. /**
  88. * tests the Memory Manager settings
  89. */
  90. public function testSettings()
  91. {
  92. /** 'File' backend */
  93. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  94. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  95. // MemoryLimit
  96. $memoryManager->setMemoryLimit(2*1024*1024 /* 2Mb */);
  97. $this->assertEquals($memoryManager->getMemoryLimit(), 2*1024*1024);
  98. // MinSize
  99. $this->assertEquals($memoryManager->getMinSize(), 16*1024); // check for default value (16K)
  100. $memoryManager->setMinSize(4*1024 /* 4Kb */);
  101. $this->assertEquals($memoryManager->getMinSize(), 4*1024);
  102. }
  103. /**
  104. * tests the memory Objects creation
  105. */
  106. public function testCreate()
  107. {
  108. /** 'File' backend */
  109. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  110. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  111. $memObject1 = $memoryManager->create('Value of object 1');
  112. $this->assertTrue($memObject1 instanceof Zend_Memory_AccessController);
  113. $this->assertEquals($memObject1->getRef(), 'Value of object 1');
  114. $memObject2 = $memoryManager->create();
  115. $this->assertTrue($memObject2 instanceof Zend_Memory_AccessController);
  116. $this->assertEquals($memObject2->getRef(), '');
  117. $memObject3 = $memoryManager->createLocked('Value of object 3');
  118. $this->assertTrue($memObject3 instanceof Zend_Memory_Container_Locked);
  119. $this->assertEquals($memObject3->getRef(), 'Value of object 3');
  120. $memObject4 = $memoryManager->createLocked();
  121. $this->assertTrue($memObject4 instanceof Zend_Memory_Container_Locked);
  122. $this->assertEquals($memObject4->getRef(), '');
  123. }
  124. /**
  125. * tests the processing of data
  126. */
  127. public function testProcessing()
  128. {
  129. /** 'File' backend */
  130. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  131. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  132. $memoryManager->setMinSize(256);
  133. $memoryManager->setMemoryLimit(1024*32);
  134. $memObjects = array();
  135. for ($count = 0; $count < 64; $count++) {
  136. $memObject = $memoryManager->create(str_repeat((string)($count % 10), 1024) /* 1K */);
  137. $memObjects[] = $memObject;
  138. }
  139. for ($count = 0; $count < 64; $count += 2) {
  140. if (version_compare(PHP_VERSION, '5.2') < 0) {
  141. $value = $memObjects[$count]->getRef();
  142. $this->assertEquals($value[16], (string)($count % 10));
  143. } else {
  144. $this->assertEquals($memObjects[$count]->value[16], (string)($count % 10));
  145. }
  146. }
  147. for ($count = 63; $count > 0; $count -= 2) {
  148. if (version_compare(PHP_VERSION, '5.2') < 0) {
  149. $value = &$memObjects[$count]->getRef();
  150. $value[16] = '_';
  151. } else {
  152. $memObjects[$count]->value[16] = '_';
  153. }
  154. }
  155. for ($count = 1; $count < 64; $count += 2) {
  156. if (version_compare(PHP_VERSION, '5.2') < 0) {
  157. $value = $memObjects[$count]->getRef();
  158. $this->assertEquals($value[16], '_');
  159. } else {
  160. $this->assertEquals($memObjects[$count]->value[16], '_');
  161. }
  162. }
  163. }
  164. }
  165. if (PHPUnit_MAIN_METHOD == 'Zend_Memory_MemoryManagerTest::main') {
  166. Zend_Memory_MemoryManagerTest::main();
  167. }