MemoryManagerTest.php 6.0 KB

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