MultiExecStateTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Transaction;
  11. use PredisTestCase;
  12. /**
  13. * @group realm-transaction
  14. */
  15. class MultiExecStateTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testFlagsValues()
  21. {
  22. $this->assertSame(1, MultiExecState::INITIALIZED);
  23. $this->assertSame(2, MultiExecState::INSIDEBLOCK);
  24. $this->assertSame(4, MultiExecState::DISCARDED);
  25. $this->assertSame(8, MultiExecState::CAS);
  26. $this->assertSame(16, MultiExecState::WATCH);
  27. }
  28. /**
  29. * @group disconnected
  30. */
  31. public function testStateConstructorStartsWithResetState()
  32. {
  33. $state = new MultiExecState();
  34. $this->assertSame(0, $state->get());
  35. $this->assertTrue($state->isReset());
  36. }
  37. /**
  38. * @group disconnected
  39. */
  40. public function testCanCheckOneOrMoreStateFlags()
  41. {
  42. $flags = MultiExecState::INITIALIZED | MultiExecState::CAS;
  43. $state = new MultiExecState();
  44. $state->set($flags);
  45. $this->assertSame($flags, $state->get());
  46. $this->assertFalse($state->check(MultiExecState::INSIDEBLOCK));
  47. $this->assertTrue($state->check(MultiExecState::INITIALIZED));
  48. $this->assertTrue($state->check(MultiExecState::CAS));
  49. $this->assertTrue($state->check($flags));
  50. $this->assertFalse($state->check($flags | MultiExecState::INSIDEBLOCK));
  51. }
  52. /**
  53. * @group disconnected
  54. */
  55. public function testSettingAndGettingWholeFlags()
  56. {
  57. $flags = MultiExecState::INITIALIZED | MultiExecState::CAS;
  58. $state = new MultiExecState();
  59. $state->set($flags);
  60. $this->assertFalse($state->check(MultiExecState::INSIDEBLOCK));
  61. $this->assertTrue($state->check(MultiExecState::INITIALIZED));
  62. $this->assertTrue($state->check(MultiExecState::CAS));
  63. $this->assertSame($flags, $state->get());
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testCanFlagSingleStates()
  69. {
  70. $flags = MultiExecState::INITIALIZED | MultiExecState::CAS;
  71. $state = new MultiExecState();
  72. $state->flag(MultiExecState::INITIALIZED);
  73. $this->assertTrue($state->check(MultiExecState::INITIALIZED));
  74. $this->assertFalse($state->check(MultiExecState::CAS));
  75. $state->flag(MultiExecState::CAS);
  76. $this->assertTrue($state->check(MultiExecState::INITIALIZED));
  77. $this->assertTrue($state->check(MultiExecState::CAS));
  78. $this->assertSame($flags, $state->get());
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testCanUnflagSingleStates()
  84. {
  85. $state = new MultiExecState();
  86. $state->set(MultiExecState::INITIALIZED | MultiExecState::CAS);
  87. $this->assertTrue($state->check(MultiExecState::INITIALIZED));
  88. $this->assertTrue($state->check(MultiExecState::CAS));
  89. $state->unflag(MultiExecState::CAS);
  90. $this->assertTrue($state->check(MultiExecState::INITIALIZED));
  91. $this->assertFalse($state->check(MultiExecState::CAS));
  92. $state->unflag(MultiExecState::INITIALIZED);
  93. $this->assertFalse($state->check(MultiExecState::INITIALIZED));
  94. $this->assertFalse($state->check(MultiExecState::CAS));
  95. $this->assertTrue($state->isReset());
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testIsInitializedMethod()
  101. {
  102. $state = new MultiExecState();
  103. $this->assertFalse($state->isInitialized());
  104. $state->set(MultiExecState::INITIALIZED);
  105. $this->assertTrue($state->isInitialized());
  106. }
  107. /**
  108. * @group disconnected
  109. */
  110. public function testIsExecuting()
  111. {
  112. $state = new MultiExecState();
  113. $this->assertFalse($state->isExecuting());
  114. $state->set(MultiExecState::INSIDEBLOCK);
  115. $this->assertTrue($state->isExecuting());
  116. }
  117. /**
  118. * @group disconnected
  119. */
  120. public function testIsCAS()
  121. {
  122. $state = new MultiExecState();
  123. $this->assertFalse($state->isCAS());
  124. $state->set(MultiExecState::CAS);
  125. $this->assertTrue($state->isCAS());
  126. }
  127. /**
  128. * @group disconnected
  129. */
  130. public function testIsWatchAllowed()
  131. {
  132. $state = new MultiExecState();
  133. $this->assertFalse($state->isWatchAllowed());
  134. $state->flag(MultiExecState::INITIALIZED);
  135. $this->assertTrue($state->isWatchAllowed());
  136. $state->flag(MultiExecState::CAS);
  137. $this->assertFalse($state->isWatchAllowed());
  138. $state->unflag(MultiExecState::CAS);
  139. $this->assertTrue($state->isWatchAllowed());
  140. }
  141. /**
  142. * @group disconnected
  143. */
  144. public function testIsWatching()
  145. {
  146. $state = new MultiExecState();
  147. $this->assertFalse($state->isWatching());
  148. $state->set(MultiExecState::WATCH);
  149. $this->assertTrue($state->isWatching());
  150. }
  151. /**
  152. * @group disconnected
  153. */
  154. public function testIsDiscarded()
  155. {
  156. $state = new MultiExecState();
  157. $this->assertFalse($state->isDiscarded());
  158. $state->set(MultiExecState::DISCARDED);
  159. $this->assertTrue($state->isDiscarded());
  160. }
  161. }