AccessControllerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_AccessControllerTest::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_Container_AccessControllerTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Memory manager, used for tests
  42. *
  43. * @var Zend_Memory_Manager
  44. */
  45. private $_memoryManager = null;
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. public function setUp()
  52. {
  53. $tmpDir = sys_get_temp_dir() . '/zend_memory';
  54. $this->_removeCacheDir($tmpDir);
  55. mkdir($tmpDir);
  56. $this->cacheDir = $tmpDir;
  57. }
  58. protected function _removeCacheDir($dir)
  59. {
  60. if (!file_exists($dir)) {
  61. return true;
  62. }
  63. if (!is_dir($dir) || is_link($dir)) {
  64. return unlink($dir);
  65. }
  66. foreach (scandir($dir) as $item) {
  67. if ($item == '.' || $item == '..') {
  68. continue;
  69. }
  70. $this->_removeCacheDir($dir . '/' . $item);
  71. }
  72. return rmdir($dir);
  73. }
  74. /**
  75. * Retrieve memory manager
  76. *
  77. */
  78. private function _getMemoryManager()
  79. {
  80. if ($this->_memoryManager === null) {
  81. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  82. $this->_memoryManager = Zend_Memory::factory('File', $backendOptions);
  83. }
  84. return $this->_memoryManager;
  85. }
  86. /**
  87. * tests the Movable memory container object creation
  88. */
  89. public function testCreation()
  90. {
  91. $memoryManager = $this->_getMemoryManager();
  92. $memObject = $memoryManager->create('012345678');
  93. $this->assertTrue($memObject instanceof Zend_Memory_AccessController);
  94. }
  95. /**
  96. * tests the value access methods
  97. */
  98. public function testValueAccess()
  99. {
  100. $memoryManager = $this->_getMemoryManager();
  101. $memObject = $memoryManager->create('0123456789');
  102. // getRef() method
  103. $this->assertEquals($memObject->getRef(), '0123456789');
  104. $valueRef = &$memObject->getRef();
  105. $valueRef[3] = '_';
  106. $this->assertEquals($memObject->getRef(), '012_456789');
  107. if (version_compare(PHP_VERSION, '5.2') < 0) {
  108. // Skip next tests for PHP versions before 5.2
  109. return;
  110. }
  111. // value property
  112. $this->assertEquals((string)$memObject->value, '012_456789');
  113. $memObject->value[7] = '_';
  114. $this->assertEquals((string)$memObject->value, '012_456_89');
  115. $memObject->value = 'another value';
  116. $this->assertTrue($memObject->value instanceof Zend_Memory_Value);
  117. $this->assertEquals((string)$memObject->value, 'another value');
  118. }
  119. /**
  120. * tests lock()/unlock()/isLocked() functions
  121. */
  122. public function testLock()
  123. {
  124. $memoryManager = $this->_getMemoryManager();
  125. $memObject = $memoryManager->create('012345678');
  126. $this->assertFalse((boolean)$memObject->isLocked());
  127. $memObject->lock();
  128. $this->assertTrue((boolean)$memObject->isLocked());
  129. $memObject->unlock();
  130. $this->assertFalse((boolean)$memObject->isLocked());
  131. }
  132. }
  133. if (PHPUnit_MAIN_METHOD == 'Zend_Memory_AccessControllerTest::main') {
  134. Zend_Memory_AccessControllerTest::main();
  135. }