/tests/Zend/Memory/MemoryManagerTest.php

https://github.com/WebTricks/WebTricks-CMS · PHP · 200 lines · 114 code · 29 blank · 57 comment · 20 complexity · cb4e6bbe2175d19b428d5fa6ea6c922c MD5 · raw file

  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: MemoryManagerTest.php 20804 2010-02-01 15:49:16Z alexander $
  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 backends naming processing
  82. *
  83. * @group ZF-9023
  84. */
  85. public function testBackendNamingProcessing()
  86. {
  87. /** 'File' backend */
  88. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  89. $memoryManager = Zend_Memory::factory('fIlE', $backendOptions);
  90. }
  91. /**
  92. * tests the Memory Manager settings
  93. */
  94. public function testSettings()
  95. {
  96. /** 'File' backend */
  97. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  98. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  99. // MemoryLimit
  100. $memoryManager->setMemoryLimit(2*1024*1024 /* 2Mb */);
  101. $this->assertEquals($memoryManager->getMemoryLimit(), 2*1024*1024);
  102. // MinSize
  103. $this->assertEquals($memoryManager->getMinSize(), 16*1024); // check for default value (16K)
  104. $memoryManager->setMinSize(4*1024 /* 4Kb */);
  105. $this->assertEquals($memoryManager->getMinSize(), 4*1024);
  106. }
  107. /**
  108. * tests the memory Objects creation
  109. */
  110. public function testCreate()
  111. {
  112. /** 'File' backend */
  113. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  114. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  115. $memObject1 = $memoryManager->create('Value of object 1');
  116. $this->assertTrue($memObject1 instanceof Zend_Memory_AccessController);
  117. $this->assertEquals($memObject1->getRef(), 'Value of object 1');
  118. $memObject2 = $memoryManager->create();
  119. $this->assertTrue($memObject2 instanceof Zend_Memory_AccessController);
  120. $this->assertEquals($memObject2->getRef(), '');
  121. $memObject3 = $memoryManager->createLocked('Value of object 3');
  122. $this->assertTrue($memObject3 instanceof Zend_Memory_Container_Locked);
  123. $this->assertEquals($memObject3->getRef(), 'Value of object 3');
  124. $memObject4 = $memoryManager->createLocked();
  125. $this->assertTrue($memObject4 instanceof Zend_Memory_Container_Locked);
  126. $this->assertEquals($memObject4->getRef(), '');
  127. }
  128. /**
  129. * tests the processing of data
  130. */
  131. public function testProcessing()
  132. {
  133. /** 'File' backend */
  134. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  135. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  136. $memoryManager->setMinSize(256);
  137. $memoryManager->setMemoryLimit(1024*32);
  138. $memObjects = array();
  139. for ($count = 0; $count < 64; $count++) {
  140. $memObject = $memoryManager->create(str_repeat((string)($count % 10), 1024) /* 1K */);
  141. $memObjects[] = $memObject;
  142. }
  143. for ($count = 0; $count < 64; $count += 2) {
  144. if (version_compare(PHP_VERSION, '5.2') < 0) {
  145. $value = $memObjects[$count]->getRef();
  146. $this->assertEquals($value[16], (string)($count % 10));
  147. } else {
  148. $this->assertEquals($memObjects[$count]->value[16], (string)($count % 10));
  149. }
  150. }
  151. for ($count = 63; $count > 0; $count -= 2) {
  152. if (version_compare(PHP_VERSION, '5.2') < 0) {
  153. $value = &$memObjects[$count]->getRef();
  154. $value[16] = '_';
  155. } else {
  156. $memObjects[$count]->value[16] = '_';
  157. }
  158. }
  159. for ($count = 1; $count < 64; $count += 2) {
  160. if (version_compare(PHP_VERSION, '5.2') < 0) {
  161. $value = $memObjects[$count]->getRef();
  162. $this->assertEquals($value[16], '_');
  163. } else {
  164. $this->assertEquals($memObjects[$count]->value[16], '_');
  165. }
  166. }
  167. }
  168. }
  169. if (PHPUnit_MAIN_METHOD == 'Zend_Memory_MemoryManagerTest::main') {
  170. Zend_Memory_MemoryManagerTest::main();
  171. }