/tests/Zend/Memory/MovableTest.php

https://github.com/devilsansclue/ZendFramework · PHP · 181 lines · 78 code · 32 blank · 71 comment · 2 complexity · 4c032fc5ab43bc65c450b5ce4de84ce2 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-2011 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. /** Zend_Memory */
  23. require_once 'Zend/Memory.php';
  24. /**
  25. * Memory value container
  26. *
  27. * (Should be presented for value object)
  28. *
  29. * @category Zend
  30. * @package Zend_Memory
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2011 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_Manager_Dummy extends Zend_Memory_Manager
  36. {
  37. /** @var boolean */
  38. public $processUpdatePassed = false;
  39. /** @var integer */
  40. public $processedId;
  41. /** @var Zend_Memory_Container_Movable */
  42. public $processedObject;
  43. /**
  44. * Dummy object constructor
  45. */
  46. public function __construct()
  47. {
  48. // Do nothing
  49. }
  50. /**
  51. * Dummy value update callback method
  52. */
  53. public function processUpdate(Zend_Memory_Container_Movable $container, $id)
  54. {
  55. $this->processUpdatePassed = true;
  56. $this->processedId = $id;
  57. $this->processedObject = $container;
  58. }
  59. }
  60. /**
  61. * @category Zend
  62. * @package Zend_Memory
  63. * @subpackage UnitTests
  64. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  65. * @license http://framework.zend.com/license/new-bsd New BSD License
  66. * @group Zend_Memory
  67. */
  68. class Zend_Memory_Container_MovableTest extends PHPUnit_Framework_TestCase
  69. {
  70. /**
  71. * tests the Movable memory container object creation
  72. */
  73. public function testCreation()
  74. {
  75. $memoryManager = new Zend_Memory_Manager_Dummy();
  76. $memObject = new Zend_Memory_Container_Movable($memoryManager, 10, '0123456789');
  77. $this->assertTrue($memObject instanceof Zend_Memory_Container_Movable);
  78. }
  79. /**
  80. * tests the value access methods
  81. */
  82. public function testValueAccess()
  83. {
  84. $memoryManager = new Zend_Memory_Manager_Dummy();
  85. $memObject = new Zend_Memory_Container_Movable($memoryManager, 10, '0123456789');
  86. // getRef() method
  87. $this->assertEquals($memObject->getRef(), '0123456789');
  88. $valueRef = &$memObject->getRef();
  89. $valueRef[3] = '_';
  90. $this->assertEquals($memObject->getRef(), '012_456789');
  91. if (version_compare(PHP_VERSION, '5.2') < 0) {
  92. // Skip next tests for PHP versions before 5.2
  93. return;
  94. }
  95. // value property
  96. $this->assertEquals((string)$memObject->value, '012_456789');
  97. $memObject->value[7] = '_';
  98. $this->assertEquals((string)$memObject->value, '012_456_89');
  99. $memObject->value = 'another value';
  100. $this->assertTrue($memObject->value instanceof Zend_Memory_Value);
  101. $this->assertEquals((string)$memObject->value, 'another value');
  102. }
  103. /**
  104. * tests lock()/unlock()/isLocked() functions
  105. */
  106. public function testLock()
  107. {
  108. $memoryManager = new Zend_Memory_Manager_Dummy();
  109. $memObject = new Zend_Memory_Container_Movable($memoryManager, 10, '0123456789');
  110. $this->assertFalse((boolean)$memObject->isLocked());
  111. $memObject->lock();
  112. $this->assertTrue((boolean)$memObject->isLocked());
  113. $memObject->unlock();
  114. $this->assertFalse((boolean)$memObject->isLocked());
  115. }
  116. /**
  117. * tests the touch() method
  118. */
  119. public function testTouch()
  120. {
  121. $memoryManager = new Zend_Memory_Manager_Dummy();
  122. $memObject = new Zend_Memory_Container_Movable($memoryManager, 10, '0123456789');
  123. $this->assertFalse($memoryManager->processUpdatePassed);
  124. $memObject->touch();
  125. $this->assertTrue($memoryManager->processUpdatePassed);
  126. $this->assertTrue($memoryManager->processedObject === $memObject);
  127. $this->assertEquals($memoryManager->processedId, 10);
  128. }
  129. /**
  130. * tests the value update tracing
  131. */
  132. public function testValueUpdateTracing()
  133. {
  134. if (version_compare(PHP_VERSION, '5.2') < 0) {
  135. // Skip next tests for PHP versions before 5.2
  136. return;
  137. }
  138. $memoryManager = new Zend_Memory_Manager_Dummy();
  139. $memObject = new Zend_Memory_Container_Movable($memoryManager, 10, '0123456789');
  140. // startTrace() method is usually invoked by memory manager, when it need to be notified
  141. // about value update
  142. $memObject->startTrace();
  143. $this->assertFalse($memoryManager->processUpdatePassed);
  144. $memObject->value[6] = '_';
  145. $this->assertTrue($memoryManager->processUpdatePassed);
  146. $this->assertTrue($memoryManager->processedObject === $memObject);
  147. $this->assertEquals($memoryManager->processedId, 10);
  148. }
  149. }