FSMTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Search_Lucene
  5. * @subpackage UnitTests
  6. */
  7. /**
  8. * Zend_Search_Lucene_FSM
  9. */
  10. require_once 'Zend/Search/Lucene/FSM.php';
  11. /**
  12. * PHPUnit test case
  13. */
  14. require_once 'PHPUnit/Framework/TestCase.php';
  15. class Zend_Search_Lucene_FSM_testClass
  16. {
  17. public $action1Passed = false;
  18. public $action2Passed = false;
  19. public $action3Passed = false;
  20. public $action4Passed = false;
  21. public $action5Passed = false;
  22. public $action6Passed = false;
  23. public $action7Passed = false;
  24. public $action8Passed = false;
  25. public function action1() { $this->action1Passed = true; }
  26. public function action2() { $this->action2Passed = true; }
  27. public function action3() { $this->action3Passed = true; }
  28. public function action4() { $this->action4Passed = true; }
  29. public function action5() { $this->action5Passed = true; }
  30. public function action6() { $this->action6Passed = true; }
  31. public function action7() { $this->action7Passed = true; }
  32. public function action8() { $this->action8Passed = true; }
  33. }
  34. class Zend_Search_Lucene_FSM_testFSMClass extends Zend_Search_Lucene_FSM
  35. {
  36. const OPENED = 0;
  37. const CLOSED = 1;
  38. const CLOSED_AND_LOCKED = 2;
  39. const OPENED_AND_LOCKED = 3; // Wrong state, should not be used
  40. const OPEN = 0;
  41. const CLOSE = 1;
  42. const LOCK = 3;
  43. const UNLOCK = 4;
  44. /**
  45. * Object to trace FSM actions
  46. *
  47. * @var Zend_Search_Lucene_FSM_testClass
  48. */
  49. public $actionTracer;
  50. public function __construct()
  51. {
  52. $this->actionTracer = new Zend_Search_Lucene_FSM_testClass();
  53. $this->addStates(array(self::OPENED, self::CLOSED, self::CLOSED_AND_LOCKED));
  54. $this->addInputSymbols(array(self::OPEN, self::CLOSE, self::LOCK, self::UNLOCK));
  55. $unlockAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action4');
  56. $openAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action6');
  57. $closeEntryAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action2');
  58. $closeExitAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action8');
  59. $this->addRules(array( array(self::OPENED, self::CLOSE, self::CLOSED),
  60. array(self::CLOSED, self::OPEN, self::OPEN),
  61. array(self::CLOSED, self::LOCK, self::CLOSED_AND_LOCKED),
  62. array(self::CLOSED_AND_LOCKED, self::UNLOCK, self::CLOSED, $unlockAction),
  63. ));
  64. $this->addInputAction(self::CLOSED_AND_LOCKED, self::UNLOCK, $unlockAction);
  65. $this->addTransitionAction(self::CLOSED, self::OPENED, $openAction);
  66. $this->addEntryAction(self::CLOSED, $closeEntryAction);
  67. $this->addExitAction(self::CLOSED, $closeExitAction);
  68. }
  69. }
  70. /**
  71. * @category Zend
  72. * @package Zend_Search_Lucene
  73. * @subpackage UnitTests
  74. */
  75. class Zend_Search_Lucene_FSMTest extends PHPUnit_Framework_TestCase
  76. {
  77. public function testCreate()
  78. {
  79. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  80. $this->assertTrue($doorFSM instanceof Zend_Search_Lucene_FSM);
  81. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  82. }
  83. public function testSetState()
  84. {
  85. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  86. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  87. $doorFSM->setState(Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  88. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED );
  89. $wrongStateExceptionCatched = false;
  90. try {
  91. $doorFSM->setState(Zend_Search_Lucene_FSM_testFSMClass::OPENED_AND_LOCKED);
  92. } catch(Zend_Search_Exception $e) {
  93. $wrongStateExceptionCatched = true;
  94. }
  95. $this->assertTrue($wrongStateExceptionCatched);
  96. }
  97. public function testReset()
  98. {
  99. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  100. $doorFSM->setState(Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  101. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  102. $doorFSM->reset();
  103. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  104. }
  105. public function testProcess()
  106. {
  107. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  108. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::CLOSE);
  109. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED);
  110. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::LOCK);
  111. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  112. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::UNLOCK);
  113. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED);
  114. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::OPEN);
  115. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  116. $wrongInputExceptionCatched = false;
  117. try {
  118. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::LOCK);
  119. } catch(Zend_Search_Exception $e) {
  120. $wrongInputExceptionCatched = true;
  121. }
  122. $this->assertTrue($wrongInputExceptionCatched);
  123. }
  124. public function testActions()
  125. {
  126. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  127. $this->assertFalse($doorFSM->actionTracer->action2Passed /* 'closed' state entry action*/);
  128. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::CLOSE);
  129. $this->assertTrue($doorFSM->actionTracer->action2Passed);
  130. $this->assertFalse($doorFSM->actionTracer->action8Passed /* 'closed' state exit action*/);
  131. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::LOCK);
  132. $this->assertTrue($doorFSM->actionTracer->action8Passed);
  133. $this->assertFalse($doorFSM->actionTracer->action4Passed /* 'closed&locked' state +'unlock' input action */);
  134. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::UNLOCK);
  135. $this->assertTrue($doorFSM->actionTracer->action4Passed);
  136. $this->assertFalse($doorFSM->actionTracer->action6Passed /* 'locked' -> 'opened' transition action action */);
  137. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::OPEN);
  138. $this->assertTrue($doorFSM->actionTracer->action6Passed);
  139. }
  140. }