AccessControllerTest.php 4.5 KB

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