/** * @test */ public function shouldBeAbleToStoreAndRetrieveData() { $adapter = new MongoDB("mongodb://localhost:27017"); //fixture $adapter->getClient()->izzum->states->drop(); $adapter->getClient()->izzum->configuration->drop(); $adapter->getClient()->izzum->history->drop(); $configuration = file_get_contents(__DIR__ . '/../loader/fixture-example.json'); //via the mongo shell, you could directly enter the json. //via php, we first need to decode the json to input it via the php mongo driver as an array $configuration = json_decode($configuration, true); //var_dump( $configuration); $adapter->getClient()->izzum->configuration->insert($configuration); //end fixture $machine = new StateMachine(new Context(new Identifier('mongo', 'test-machine'), null, $adapter)); $adapter->load($machine); $machine->add("adding for " . __FUNCTION__); $machine->runToCompletion("testing 213"); $this->assertEquals(array("mongo"), $adapter->getEntityIds('test-machine')); $this->assertEquals(array("mongo"), $adapter->getEntityIds('test-machine', 'done')); $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'a')); $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'b')); $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'c')); //another $identifier = new Identifier('another-mongo', 'test-machine'); $machine = new StateMachine(new Context($identifier, null, $adapter)); $adapter->load($machine); $this->assertFalse($adapter->isPersisted($identifier)); $this->assertTrue($machine->add("adding for " . __FUNCTION__)); $this->assertFalse($machine->add("adding for " . __FUNCTION__), 'already added'); $this->assertTrue($adapter->isPersisted($identifier)); $machine->runToCompletion("testing 213"); //some other stuff $machine = new StateMachine(new Context(new Identifier('foobar', 'non-used-machine'), null, $adapter)); $adapter->load($machine); $machine->add("adding for " . __FUNCTION__); $machine->runToCompletion("testing 213"); $index = array("entity_id" => 1, "machine" => 1); $options = array("background" => true); $adapter->getClient()->izzum->history->createIndex($index, $options); //getting the state for an entity_id/machine should be fast //db.states.createIndex({entity_id: 1, machine: 1}, {background: true}); $index = array("entity_id" => 1, "machine" => 1); $options = array("background" => true); $adapter->getClient()->izzum->states->createIndex($index, $options); //recreate the existing statemachine $identifier = new Identifier('another-mongo', 'test-machine'); $machine = new StateMachine(new Context($identifier, null, $adapter)); $adapter->load($machine); $this->assertTrue($adapter->isPersisted($identifier)); $this->assertFalse($machine->add(), 'already added'); $this->assertEquals('done', $machine->getCurrentState()->getName(), 'state persisted'); $this->assertEquals(0, $machine->runToCompletion(), 'alread in a final state, no transitions'); $ids = $adapter->getEntityIds('test-machine'); $this->assertEquals(2, count($ids)); $ids = $adapter->getEntityIds('non-used-machine'); $this->assertEquals(1, count($ids)); $this->assertNotNull($adapter->toString()); $this->assertNotNull($adapter . ''); $this->assertRegexp('|mongodb://localhost:27017|', $adapter->toString()); $this->assertRegexp('|mongodb://localhost:27017|', $adapter . ''); }