2
0

MemoryManagerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (file_exists($tmpDir)) {
  49. if (!rmdir($tmpDir)) {
  50. $dir = new DirectoryIterator($tmpDir);
  51. foreach ($dir as $file) {
  52. if (!$file->isDir()) {
  53. unlink($file->getPathname());
  54. }
  55. }
  56. if (!rmdir($tmpDir)) {
  57. throw new Exception('Unable to remove temporary directory ' . $tmpDir
  58. . '; perhaps it has a nested structure?');
  59. }
  60. }
  61. }
  62. mkdir($tmpDir);
  63. $this->cacheDir = $tmpDir;
  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 settings
  78. */
  79. public function testSettings()
  80. {
  81. /** 'File' backend */
  82. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  83. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  84. // MemoryLimit
  85. $memoryManager->setMemoryLimit(2*1024*1024 /* 2Mb */);
  86. $this->assertEquals($memoryManager->getMemoryLimit(), 2*1024*1024);
  87. // MinSize
  88. $this->assertEquals($memoryManager->getMinSize(), 16*1024); // check for default value (16K)
  89. $memoryManager->setMinSize(4*1024 /* 4Kb */);
  90. $this->assertEquals($memoryManager->getMinSize(), 4*1024);
  91. }
  92. /**
  93. * tests the memory Objects creation
  94. */
  95. public function testCreate()
  96. {
  97. /** 'File' backend */
  98. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  99. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  100. $memObject1 = $memoryManager->create('Value of object 1');
  101. $this->assertTrue($memObject1 instanceof Zend_Memory_AccessController);
  102. $this->assertEquals($memObject1->getRef(), 'Value of object 1');
  103. $memObject2 = $memoryManager->create();
  104. $this->assertTrue($memObject2 instanceof Zend_Memory_AccessController);
  105. $this->assertEquals($memObject2->getRef(), '');
  106. $memObject3 = $memoryManager->createLocked('Value of object 3');
  107. $this->assertTrue($memObject3 instanceof Zend_Memory_Container_Locked);
  108. $this->assertEquals($memObject3->getRef(), 'Value of object 3');
  109. $memObject4 = $memoryManager->createLocked();
  110. $this->assertTrue($memObject4 instanceof Zend_Memory_Container_Locked);
  111. $this->assertEquals($memObject4->getRef(), '');
  112. }
  113. /**
  114. * tests the processing of data
  115. */
  116. public function testProcessing()
  117. {
  118. /** 'File' backend */
  119. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  120. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  121. $memoryManager->setMinSize(256);
  122. $memoryManager->setMemoryLimit(1024*32);
  123. $memObjects = array();
  124. for ($count = 0; $count < 64; $count++) {
  125. $memObject = $memoryManager->create(str_repeat((string)($count % 10), 1024) /* 1K */);
  126. $memObjects[] = $memObject;
  127. }
  128. for ($count = 0; $count < 64; $count += 2) {
  129. if (version_compare(PHP_VERSION, '5.2') < 0) {
  130. $value = $memObjects[$count]->getRef();
  131. $this->assertEquals($value[16], (string)($count % 10));
  132. } else {
  133. $this->assertEquals($memObjects[$count]->value[16], (string)($count % 10));
  134. }
  135. }
  136. for ($count = 63; $count > 0; $count -= 2) {
  137. if (version_compare(PHP_VERSION, '5.2') < 0) {
  138. $value = &$memObjects[$count]->getRef();
  139. $value[16] = '_';
  140. } else {
  141. $memObjects[$count]->value[16] = '_';
  142. }
  143. }
  144. for ($count = 1; $count < 64; $count += 2) {
  145. if (version_compare(PHP_VERSION, '5.2') < 0) {
  146. $value = $memObjects[$count]->getRef();
  147. $this->assertEquals($value[16], '_');
  148. } else {
  149. $this->assertEquals($memObjects[$count]->value[16], '_');
  150. }
  151. }
  152. }
  153. }
  154. if (PHPUnit_MAIN_METHOD == 'Zend_Memory_MemoryManagerTest::main') {
  155. Zend_Memory_MemoryManagerTest::main();
  156. }