Exemple #1
0
 public function testEvaluateConditions()
 {
     // Base Case
     $state = new State('dummy');
     $conditionOne = new Condition(function ($context) {
         return $context->getLabel() === 'fresh';
     }, 'one');
     $conditionTwo = new Condition(function ($context) {
         return $context->getNum() <= 1;
     }, 'two', 'three');
     $one = new State('one');
     $two = new State('two');
     $three = new State('three');
     $conditionOne->setTrueState($one);
     $conditionTwo->setTrueState($two);
     $conditionTwo->setFalseState($three);
     $state->addCondition($conditionOne);
     $state->addCondition($conditionTwo);
     $context = new DummyContext();
     $context->setLabel('fresh');
     // One
     $this->assertEquals('one', $state->evaluateConditions($context)->getName());
     // Two
     $context->setLabel('soggy');
     $this->assertEquals('two', $state->evaluateConditions($context)->getName());
     // Three
     $context->increment();
     $this->assertEquals('three', $state->evaluateConditions($context)->getName());
 }
 public function testBuild()
 {
     // Test build with actions
     $workflowBuilder = new WorkflowBuilder('testflow', '\\TyHand\\WorkflowBundle\\Tests\\DummyContext');
     $stateBuilder = new StateBuilder($workflowBuilder, 'round1');
     $stateBuilder->addAction(function ($context) {
         $context->increment();
     });
     $state = $stateBuilder->build(array());
     $this->assertNotNull($state);
     $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\State', $state);
     $this->assertEquals('round1', $state->getName());
     $this->assertCount(1, $state->getActions());
     $testContext = new DummyContext();
     $this->assertEquals(1, $testContext->getNum());
     call_user_func($state->getActions()[0], $testContext, $state);
     $this->assertEquals(2, $testContext->getNum());
     $this->assertCount(0, $state->getEvents());
     $this->assertFalse($state->hasTimeLimit());
     // Test build with events
     $stateBuilder = new StateBuilder($workflowBuilder, 'round2');
     $stateBuilder->addEvent('knockout', 'finish');
     $state = $stateBuilder->build(array('finish' => new State('finish')));
     $this->assertNotNull($state);
     $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\State', $state);
     $this->assertEquals('round2', $state->getName());
     $this->assertCount(1, $state->getEvents());
     $this->assertArrayHasKey('knockout', $state->getEvents());
     $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\EventTrigger', $state->getEvents()['knockout']);
     $this->assertNotNull($state->getEvents()['knockout']->getState());
     $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\State', $state->getEvents()['knockout']->getState());
     $this->assertEquals('finish', $state->getEvents()['knockout']->getState()->getName());
     $this->assertCount(0, $state->getActions());
     $this->assertFalse($state->hasTimeLimit());
     // Test build with time limit
     $stateBuilder = new StateBuilder($workflowBuilder, 'round3');
     $stateBuilder->setTimeLimit(60, 'gameover');
     $state = $stateBuilder->build(array('gameover' => new State('gameover')));
     $this->assertNotNull($state);
     $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\State', $state);
     $this->assertEquals('round3', $state->getName());
     $this->assertTrue($state->hasTimeLimit());
     $this->assertNotNull($state->getTimeLimit());
     $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\TimeLimit', $state->getTimeLimit());
     $this->assertTrue($state->getTimeLimit()->isComplete());
     $now = new \DateTime();
     $now->modify('-60 seconds');
     $nextState = $state->hasTimeLimitPassed($now);
     $this->assertNotNull($nextState);
     $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\State', $nextState);
     $this->assertEquals('gameover', $nextState->getName());
     $this->assertCount(0, $state->getEvents());
     $this->assertCount(0, $state->getActions());
 }