FSMTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_Search_Lucene
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. * Zend_Search_Lucene_FSM
  24. */
  25. require_once 'Zend/Search/Lucene/FSM.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Search_Lucene
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Search_Lucene_FSM_testClass
  34. {
  35. public $action1Passed = false;
  36. public $action2Passed = false;
  37. public $action3Passed = false;
  38. public $action4Passed = false;
  39. public $action5Passed = false;
  40. public $action6Passed = false;
  41. public $action7Passed = false;
  42. public $action8Passed = false;
  43. public function action1() { $this->action1Passed = true; }
  44. public function action2() { $this->action2Passed = true; }
  45. public function action3() { $this->action3Passed = true; }
  46. public function action4() { $this->action4Passed = true; }
  47. public function action5() { $this->action5Passed = true; }
  48. public function action6() { $this->action6Passed = true; }
  49. public function action7() { $this->action7Passed = true; }
  50. public function action8() { $this->action8Passed = true; }
  51. }
  52. /**
  53. * @category Zend
  54. * @package Zend_Search_Lucene
  55. * @subpackage UnitTests
  56. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  57. * @license http://framework.zend.com/license/new-bsd New BSD License
  58. */
  59. class Zend_Search_Lucene_FSM_testFSMClass extends Zend_Search_Lucene_FSM
  60. {
  61. const OPENED = 0;
  62. const CLOSED = 1;
  63. const CLOSED_AND_LOCKED = 2;
  64. const OPENED_AND_LOCKED = 3; // Wrong state, should not be used
  65. const OPEN = 0;
  66. const CLOSE = 1;
  67. const LOCK = 3;
  68. const UNLOCK = 4;
  69. /**
  70. * Object to trace FSM actions
  71. *
  72. * @var Zend_Search_Lucene_FSM_testClass
  73. */
  74. public $actionTracer;
  75. public function __construct()
  76. {
  77. $this->actionTracer = new Zend_Search_Lucene_FSM_testClass();
  78. $this->addStates(array(self::OPENED, self::CLOSED, self::CLOSED_AND_LOCKED));
  79. $this->addInputSymbols(array(self::OPEN, self::CLOSE, self::LOCK, self::UNLOCK));
  80. $unlockAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action4');
  81. $openAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action6');
  82. $closeEntryAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action2');
  83. $closeExitAction = new Zend_Search_Lucene_FSMAction($this->actionTracer, 'action8');
  84. $this->addRules(array( array(self::OPENED, self::CLOSE, self::CLOSED),
  85. array(self::CLOSED, self::OPEN, self::OPEN),
  86. array(self::CLOSED, self::LOCK, self::CLOSED_AND_LOCKED),
  87. array(self::CLOSED_AND_LOCKED, self::UNLOCK, self::CLOSED, $unlockAction),
  88. ));
  89. $this->addInputAction(self::CLOSED_AND_LOCKED, self::UNLOCK, $unlockAction);
  90. $this->addTransitionAction(self::CLOSED, self::OPENED, $openAction);
  91. $this->addEntryAction(self::CLOSED, $closeEntryAction);
  92. $this->addExitAction(self::CLOSED, $closeExitAction);
  93. }
  94. }
  95. /**
  96. * @category Zend
  97. * @package Zend_Search_Lucene
  98. * @subpackage UnitTests
  99. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  100. * @license http://framework.zend.com/license/new-bsd New BSD License
  101. * @group Zend_Search_Lucene
  102. */
  103. class Zend_Search_Lucene_FSMTest extends PHPUnit_Framework_TestCase
  104. {
  105. public function testCreate()
  106. {
  107. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  108. $this->assertTrue($doorFSM instanceof Zend_Search_Lucene_FSM);
  109. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  110. }
  111. public function testSetState()
  112. {
  113. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  114. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  115. $doorFSM->setState(Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  116. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED );
  117. $wrongStateExceptionCatched = false;
  118. try {
  119. $doorFSM->setState(Zend_Search_Lucene_FSM_testFSMClass::OPENED_AND_LOCKED);
  120. } catch(Zend_Search_Exception $e) {
  121. $wrongStateExceptionCatched = true;
  122. }
  123. $this->assertTrue($wrongStateExceptionCatched);
  124. }
  125. public function testReset()
  126. {
  127. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  128. $doorFSM->setState(Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  129. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  130. $doorFSM->reset();
  131. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  132. }
  133. public function testProcess()
  134. {
  135. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  136. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::CLOSE);
  137. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED);
  138. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::LOCK);
  139. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED_AND_LOCKED);
  140. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::UNLOCK);
  141. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::CLOSED);
  142. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::OPEN);
  143. $this->assertEquals($doorFSM->getState(), Zend_Search_Lucene_FSM_testFSMClass::OPENED);
  144. $wrongInputExceptionCatched = false;
  145. try {
  146. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::LOCK);
  147. } catch(Zend_Search_Exception $e) {
  148. $wrongInputExceptionCatched = true;
  149. }
  150. $this->assertTrue($wrongInputExceptionCatched);
  151. }
  152. public function testActions()
  153. {
  154. $doorFSM = new Zend_Search_Lucene_FSM_testFSMClass();
  155. $this->assertFalse($doorFSM->actionTracer->action2Passed /* 'closed' state entry action*/);
  156. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::CLOSE);
  157. $this->assertTrue($doorFSM->actionTracer->action2Passed);
  158. $this->assertFalse($doorFSM->actionTracer->action8Passed /* 'closed' state exit action*/);
  159. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::LOCK);
  160. $this->assertTrue($doorFSM->actionTracer->action8Passed);
  161. $this->assertFalse($doorFSM->actionTracer->action4Passed /* 'closed&locked' state +'unlock' input action */);
  162. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::UNLOCK);
  163. $this->assertTrue($doorFSM->actionTracer->action4Passed);
  164. $this->assertFalse($doorFSM->actionTracer->action6Passed /* 'locked' -> 'opened' transition action action */);
  165. $doorFSM->process(Zend_Search_Lucene_FSM_testFSMClass::OPEN);
  166. $this->assertTrue($doorFSM->actionTracer->action6Passed);
  167. }
  168. }