Beispiel #1
0
 public function testItBuildsCallback()
 {
     $stateMachine = $this->getMockBuilder('Finite\\StateMachine\\StateMachine')->disableOriginalConstructor()->getMock();
     $callableMock = $this->getMockBuilder('\\stdClass')->setMethods(array('call'))->getMock();
     $callback = CallbackBuilder::create($stateMachine, array($callableMock, 'call'))->setFrom(array('s1'))->addFrom('s2')->setTo(array('s2'))->addTo('s3')->setOn(array('t12'))->addOn('t23')->getCallback();
     $this->assertInstanceOf('Finite\\Event\\Callback\\Callback', $callback);
     $this->assertInstanceOf('Finite\\Event\\Callback\\CallbackSpecification', $callback->getSpecification());
 }
Beispiel #2
0
 public function testItAttachsPreTransitionWithExcludeToSpec()
 {
     $transitionOk = $this->getMock('Finite\\Transition\\TransitionInterface');
     $transitionNotOk = $this->getMock('Finite\\Transition\\TransitionInterface');
     $state = $this->getMock('Finite\\State\\StateInterface');
     $e1 = $this->getMockBuilder('Finite\\Event\\TransitionEvent')->disableOriginalConstructor()->getMock();
     $e2 = $this->getMockBuilder('Finite\\Event\\TransitionEvent')->disableOriginalConstructor()->getMock();
     $stateful = $this->getMock('Finite\\StatefulInterface');
     $this->statemachine->expects($this->any())->method('getObject')->will($this->returnValue($stateful));
     $e1->expects($this->any())->method('getStateMachine')->will($this->returnValue($this->statemachine));
     $e2->expects($this->any())->method('getStateMachine')->will($this->returnValue($this->statemachine));
     $e1->expects($this->any())->method('getInitialState')->will($this->returnValue($state));
     $e2->expects($this->any())->method('getInitialState')->will($this->returnValue($state));
     $transitionOk->expects($this->any())->method('getState')->will($this->returnValue('foobar'));
     $transitionNotOk->expects($this->any())->method('getState')->will($this->returnValue('bazqux'));
     $e1->expects($this->any())->method('getTransition')->will($this->returnValue($transitionOk));
     $e2->expects($this->any())->method('getTransition')->will($this->returnValue($transitionNotOk));
     $this->dispatcher->expects($this->at(0))->method('addListener')->with(FiniteEvents::PRE_TRANSITION, $this->logicalAnd($this->isInstanceOf('Finite\\Event\\Callback\\Callback'), $this->callback(function (Callback $c) use($e1, $e2) {
         $c($e1);
         $c($e2);
         return true;
     })));
     $that = $this;
     $this->object->addBefore(CallbackBuilder::create($this->statemachine)->addTo('-bazqux')->setCallable(function ($object, TransitionEvent $event) use($that, $stateful) {
         $that->assertSame($object, $stateful);
         $that->assertSame('foobar', $event->getTransition()->getState());
     })->getCallback());
 }
 /**
  * {@inheritdoc}
  */
 public function createBuilder(StateMachineInterface $stateMachine)
 {
     return CallbackBuilder::create($stateMachine);
 }
Beispiel #4
0
 /**
  * @param StateMachineInterface|Callback $smOrCallback
  * @param string                         $event
  * @param callable                       $callable
  * @param array                          $specs
  *
  * @return CallbackHandler
  */
 protected function add($smOrCallback, $event, $callable = null, array $specs = array())
 {
     if ($smOrCallback instanceof Callback) {
         $this->dispatcher->addListener($event, $smOrCallback);
         return $this;
     }
     trigger_error('Use of CallbackHandler::add without a Callback instance is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
     $specs = $this->specResolver->resolve($specs);
     $callback = CallbackBuilder::create($smOrCallback, $specs['from'], $specs['to'], $specs['on'], $callable)->getCallback();
     $this->dispatcher->addListener($event, $callback);
     return $this;
 }
Beispiel #5
0
    }
    public function setFiniteState($state)
    {
        $this->state = $state;
    }
    public function display()
    {
        echo 'Hello, I\'m a document and I\'m currently at the ', $this->state, ' state.', "\n";
    }
}
// Configure your graph
$document = new Document();
$stateMachine = new Finite\StateMachine\StateMachine($document);
$loader = new Finite\Loader\ArrayLoader(array('class' => 'Document', 'states' => array('draft' => array('type' => Finite\State\StateInterface::TYPE_INITIAL, 'properties' => array('deletable' => true, 'editable' => true)), 'proposed' => array('type' => Finite\State\StateInterface::TYPE_NORMAL, 'properties' => array()), 'accepted' => array('type' => Finite\State\StateInterface::TYPE_FINAL, 'properties' => array('printable' => true))), 'transitions' => array('propose' => array('from' => array('draft'), 'to' => 'proposed'), 'accept' => array('from' => array('proposed'), 'to' => 'accepted'), 'reject' => array('from' => array('proposed'), 'to' => 'draft')), 'callbacks' => array('before' => array(array('from' => '-proposed', 'do' => function (Finite\StatefulInterface $document, \Finite\Event\TransitionEvent $e) {
    echo 'Applying transition ' . $e->getTransition()->getName(), "\n";
}), array('from' => 'proposed', 'do' => function () {
    echo 'Applying transition from proposed state', "\n";
})), 'after' => array(array('to' => array('accepted'), 'do' => array($document, 'display'))))));
$loader->load($stateMachine);
$stateMachine->initialize();
$stateMachine->getDispatcher()->addListener(\Finite\Event\FiniteEvents::PRE_TRANSITION, function (\Finite\Event\TransitionEvent $e) {
    echo 'This is a pre transition', "\n";
});
$foobar = 42;
$stateMachine->getDispatcher()->addListener(\Finite\Event\FiniteEvents::POST_TRANSITION, \Finite\Event\Callback\CallbackBuilder::create($stateMachine)->setCallable(function () use($foobar) {
    echo "\$foobar is {$foobar} and this is a post transition\n";
})->getCallback());
$stateMachine->apply('propose');
$stateMachine->apply('reject');
$stateMachine->apply('propose');
$stateMachine->apply('accept');