2
0

AccessControllerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if (file_exists($tmpDir)) {
  55. if (!rmdir($tmpDir)) {
  56. $dir = new DirectoryIterator($tmpDir);
  57. foreach ($dir as $file) {
  58. if (!$file->isDir()) {
  59. unlink($file->getPathname());
  60. }
  61. }
  62. if (!rmdir($tmpDir)) {
  63. throw new Exception('Unable to remove temporary directory ' . $tmpDir
  64. . '; perhaps it has a nested structure?');
  65. }
  66. }
  67. }
  68. mkdir($tmpDir);
  69. $this->cacheDir = $tmpDir;
  70. }
  71. /**
  72. * Retrieve memory manager
  73. *
  74. */
  75. private function _getMemoryManager()
  76. {
  77. if ($this->_memoryManager === null) {
  78. $backendOptions = array('cache_dir' => $this->cacheDir); // Directory where to put the cache files
  79. $this->_memoryManager = Zend_Memory::factory('File', $backendOptions);
  80. }
  81. return $this->_memoryManager;
  82. }
  83. /**
  84. * tests the Movable memory container object creation
  85. */
  86. public function testCreation()
  87. {
  88. $memoryManager = $this->_getMemoryManager();
  89. $memObject = $memoryManager->create('012345678');
  90. $this->assertTrue($memObject instanceof Zend_Memory_AccessController);
  91. }
  92. /**
  93. * tests the value access methods
  94. */
  95. public function testValueAccess()
  96. {
  97. $memoryManager = $this->_getMemoryManager();
  98. $memObject = $memoryManager->create('0123456789');
  99. // getRef() method
  100. $this->assertEquals($memObject->getRef(), '0123456789');
  101. $valueRef = &$memObject->getRef();
  102. $valueRef[3] = '_';
  103. $this->assertEquals($memObject->getRef(), '012_456789');
  104. if (version_compare(PHP_VERSION, '5.2') < 0) {
  105. // Skip next tests for PHP versions before 5.2
  106. return;
  107. }
  108. // value property
  109. $this->assertEquals((string)$memObject->value, '012_456789');
  110. $memObject->value[7] = '_';
  111. $this->assertEquals((string)$memObject->value, '012_456_89');
  112. $memObject->value = 'another value';
  113. $this->assertTrue($memObject->value instanceof Zend_Memory_Value);
  114. $this->assertEquals((string)$memObject->value, 'another value');
  115. }
  116. /**
  117. * tests lock()/unlock()/isLocked() functions
  118. */
  119. public function testLock()
  120. {
  121. $memoryManager = $this->_getMemoryManager();
  122. $memObject = $memoryManager->create('012345678');
  123. $this->assertFalse((boolean)$memObject->isLocked());
  124. $memObject->lock();
  125. $this->assertTrue((boolean)$memObject->isLocked());
  126. $memObject->unlock();
  127. $this->assertFalse((boolean)$memObject->isLocked());
  128. }
  129. }
  130. if (PHPUnit_MAIN_METHOD == 'Zend_Memory_AccessControllerTest::main') {
  131. Zend_Memory_AccessControllerTest::main();
  132. }