MemoryManagerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /** Zend_Memory */
  27. require_once 'Zend/Memory.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Memory
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Memory_MemoryManagerTest extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * tests the Memory Manager creation
  39. *
  40. */
  41. public function testCreation()
  42. {
  43. /** 'File' backend */
  44. $backendOptions = array('cache_dir' => dirname(__FILE__) . '/_files/'); // Directory where to put the cache files
  45. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  46. $this->assertTrue($memoryManager instanceof Zend_Memory_Manager);
  47. }
  48. /**
  49. * tests the Memory Manager settings
  50. */
  51. public function testSettings()
  52. {
  53. /** 'File' backend */
  54. $backendOptions = array('cache_dir' => dirname(__FILE__) . '/_files/'); // Directory where to put the cache files
  55. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  56. // MemoryLimit
  57. $memoryManager->setMemoryLimit(2*1024*1024 /* 2Mb */);
  58. $this->assertEquals($memoryManager->getMemoryLimit(), 2*1024*1024);
  59. // MinSize
  60. $this->assertEquals($memoryManager->getMinSize(), 16*1024); // check for default value (16K)
  61. $memoryManager->setMinSize(4*1024 /* 4Kb */);
  62. $this->assertEquals($memoryManager->getMinSize(), 4*1024);
  63. }
  64. /**
  65. * tests the memory Objects creation
  66. */
  67. public function testCreate()
  68. {
  69. /** 'File' backend */
  70. $backendOptions = array('cache_dir' => dirname(__FILE__) . '/_files/'); // Directory where to put the cache files
  71. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  72. $memObject1 = $memoryManager->create('Value of object 1');
  73. $this->assertTrue($memObject1 instanceof Zend_Memory_AccessController);
  74. $this->assertEquals($memObject1->getRef(), 'Value of object 1');
  75. $memObject2 = $memoryManager->create();
  76. $this->assertTrue($memObject2 instanceof Zend_Memory_AccessController);
  77. $this->assertEquals($memObject2->getRef(), '');
  78. $memObject3 = $memoryManager->createLocked('Value of object 3');
  79. $this->assertTrue($memObject3 instanceof Zend_Memory_Container_Locked);
  80. $this->assertEquals($memObject3->getRef(), 'Value of object 3');
  81. $memObject4 = $memoryManager->createLocked();
  82. $this->assertTrue($memObject4 instanceof Zend_Memory_Container_Locked);
  83. $this->assertEquals($memObject4->getRef(), '');
  84. }
  85. /**
  86. * tests the processing of data
  87. */
  88. public function testProcessing()
  89. {
  90. /** 'File' backend */
  91. $backendOptions = array('cache_dir' => dirname(__FILE__) . '/_files/'); // Directory where to put the cache files
  92. $memoryManager = Zend_Memory::factory('File', $backendOptions);
  93. $memoryManager->setMinSize(256);
  94. $memoryManager->setMemoryLimit(1024*32);
  95. $memObjects = array();
  96. for ($count = 0; $count < 64; $count++) {
  97. $memObject = $memoryManager->create(str_repeat((string)($count % 10), 1024) /* 1K */);
  98. $memObjects[] = $memObject;
  99. }
  100. for ($count = 0; $count < 64; $count += 2) {
  101. if (version_compare(PHP_VERSION, '5.2') < 0) {
  102. $value = $memObjects[$count]->getRef();
  103. $this->assertEquals($value[16], (string)($count % 10));
  104. } else {
  105. $this->assertEquals($memObjects[$count]->value[16], (string)($count % 10));
  106. }
  107. }
  108. for ($count = 63; $count > 0; $count -= 2) {
  109. if (version_compare(PHP_VERSION, '5.2') < 0) {
  110. $value = &$memObjects[$count]->getRef();
  111. $value[16] = '_';
  112. } else {
  113. $memObjects[$count]->value[16] = '_';
  114. }
  115. }
  116. for ($count = 1; $count < 64; $count += 2) {
  117. if (version_compare(PHP_VERSION, '5.2') < 0) {
  118. $value = $memObjects[$count]->getRef();
  119. $this->assertEquals($value[16], '_');
  120. } else {
  121. $this->assertEquals($memObjects[$count]->value[16], '_');
  122. }
  123. }
  124. }
  125. }