MovableTest.php 5.4 KB

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