/** * Checks a fully loaded statemachine for a valid configuration. * * This is useful in a debug situation so you can check for the validity of * rules, commands and callables in guard logic, transition logic and state entry/exit logic * before they hit a * * This does not instantiate any objects. It just checks if callables can be found * and if classes for rules and commands can be found * * @param StateMachine $machine * @return Exception[] an array of exceptions if anything is wrong with the configuration */ public static function checkConfiguration(StateMachine $machine) { //TODO: also check the rules and commands $exceptions = array(); $output = array(); //check state callables foreach ($machine->getStates() as $state) { $exceptions[] = self::getExceptionForCheckingCallable($state->getExitCallable(), State::CALLABLE_ENTRY, $state); $exceptions[] = self::getExceptionForCheckingCallable($state->getEntryCallable(), State::CALLABLE_ENTRY, $state); } //check transition callables foreach ($machine->getTransitions() as $transition) { $exceptions[] = self::getExceptionForCheckingCallable($transition->getGuardCallable(), Transition::CALLABLE_GUARD, $transition); $exceptions[] = self::getExceptionForCheckingCallable($transition->getTransitionCallable(), Transition::CALLABLE_TRANSITION, $transition); } //get the exceptions foreach ($exceptions as $e) { if (is_a($e, '\\Exception')) { $output[] = $e; } } return $output; }
/** * @test */ public function shouldAddRegexesLoaderOnlyWhenStatesAreSet() { $context = new Context(new Identifier(Identifier::NULL_ENTITY_ID, Identifier::NULL_STATEMACHINE)); $machine = new StateMachine($context); $transitions = array(); $s1 = new State("1"); $s2 = new State("2"); $s3 = new State("3"); //many to many $transitions[] = new Transition(new State('regex:/.*/'), new State('regex:/.*/')); $loader = new LoaderArray($transitions); $this->assertEquals(count($transitions), $loader->count()); $this->assertEquals(1, $loader->count()); $this->assertEquals(0, count($machine->getStates())); $this->assertEquals(0, count($machine->getTransitions())); $count = $loader->load($machine); $this->assertEquals(0, $count, 'nothing because there are no known states'); $this->assertTrue($machine->addState($s1)); $this->assertTrue($machine->addState($s2)); $this->assertTrue($machine->addState($s3)); $this->assertEquals(3, count($machine->getStates())); $this->assertFalse($machine->addState($s1)); $this->assertFalse($machine->addState($s2)); $this->assertFalse($machine->addState($s3)); $this->assertEquals(3, count($machine->getStates())); $count = $loader->load($machine); $this->assertEquals(6, count($machine->getTransitions())); $this->assertEquals(6, $count, 'regexes have matched all states and created a mesh'); $count = $loader->load($machine); $this->assertEquals(0, $count, 'transitions are not added since they have already been added'); $this->assertEquals(3, count($machine->getStates())); $this->assertEquals(6, count($machine->getTransitions())); }