Used to mark the initial construction of a statemachine at a certain
point in time. subsequent calls to 'add' will not have any effect if it
has already been persisted.
public add ( string $state, string $message = null ) : boolean | ||
$state | string | |
$message | string | optional message. this can be used by the persistence adapter to be part of the transition history to provide extra information about the transition. |
Результат | boolean | true if it was added, false if it was already there |
/** * @test */ public function shouldLoadAndWriteViaDelegator() { $loader = XML::createFromFile(__DIR__ . '/../loader/fixture-example.xml'); $writer = new Memory(); $identifier = new Identifier('readerwriter-test', 'test-machine'); $delegator = new ReaderWriterDelegator($loader, $writer); $context = new Context($identifier, null, $delegator); $machine = new StateMachine($context); $this->assertCount(0, $machine->getTransitions()); $count = $delegator->load($machine); //add to the backend $this->assertTrue($context->add('a')); $this->assertCount(4, $machine->getTransitions(), 'there is a regex transition that adds 2 transitions (a-c and b-c)'); $this->assertEquals(4, $count); $this->assertTrue($machine->ab()); //get the data from the memory storage facility $data = $writer->getStorageFromRegistry($machine->getContext()->getIdentifier()); $this->assertEquals('b', $data->state); $this->assertEquals('b', $machine->getCurrentState()->getName()); $this->assertTrue($machine->bdone()); $data = $writer->getStorageFromRegistry($machine->getContext()->getIdentifier()); $this->assertEquals('done', $data->state); }
public function testFull() { $entity_id = "id"; $machine = "test machine"; $identifier = new Identifier($entity_id, $machine); $builder = new EntityBuilder(); $io = new Memory(); // all parameters $o = new Context($identifier, $builder, $io); $this->assertEquals($entity_id, $o->getEntityId()); $this->assertEquals($machine, $o->getMachine()); $this->assertNull($o->getStateMachine()); $this->assertTrue(is_a($o->getPersistenceAdapter(), 'izzum\\statemachine\\persistence\\Memory')); $this->assertTrue(is_a($o->getBuilder(), 'izzum\\statemachine\\EntityBuilder')); $this->assertEquals($o->getIdentifier(), $o->getEntity()); $this->assertTrue(is_string($o->getEntityId())); $this->assertTrue(is_string($o->toString())); $this->assertContains($entity_id, $o->toString()); $this->assertContains($machine, $o->toString()); $this->assertContains('izzum\\statemachine\\Context', $o->toString()); // even though we have a valid reader, the state machine does not exist. $this->assertEquals(State::STATE_UNKNOWN, $o->getState()); $this->assertTrue($o->setState('lala')); $this->assertEquals('lala', $o->getState()); // for coverage. $this->assertNotNull($o->getId()); $this->assertNotNull($o->getId(true)); $this->assertNotNull($o->getId(false)); $this->assertNotNull($o->getId(true, true)); $this->assertNotNull($o->getId(false, true)); // adding $machine = 'add-experiment-machine'; $context = new Context(new Identifier('add-experiment-id', $machine), $builder, $io); $sm = new StateMachine($context); $sm->addTransition(new Transition(new State('c', State::TYPE_FINAL), new State('d'), State::TYPE_NORMAL)); $sm->addTransition(new Transition(new State('a', State::TYPE_INITIAL), new State('b'))); $this->assertCount(0, $context->getPersistenceAdapter()->getEntityIds($machine)); $state = $sm->getInitialState()->getName(); $this->assertEquals('a', $state); $this->assertTrue($context->add($state)); // var_dump( Memory::get()); $this->assertCount(1, $context->getPersistenceAdapter()->getEntityIds($machine)); }