MemoryManagerTest.php 6.0 KB

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