FSMTest.php 7.8 KB

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