require 'vendor/autoload.php';
use FutoIn\RI\AsyncSteps;
use FutoIn\RI\AsyncToolTest;
// Note, we have no default event engine in PHP
AsyncToolTest::init();
$model_as = new AsyncSteps();
$model_as->state()->variable = 'Vanilla';
$model_as->add(function ($as) {
    echo '-----' . PHP_EOL;
    echo 'Hi! I am from model_as' . PHP_EOL;
    echo 'State.var: ' . $as->variable . PHP_EOL;
    $as->variable = 'Dirty';
    $as->success();
});
for ($i = 0; $i < 3; ++$i) {
    $root_as = new AsyncSteps();
    $root_as->copyFrom($model_as);
    $root_as->add(function ($as) use($model_as) {
        $as->add(function ($as) {
            echo '>> The first inner step' . PHP_EOL;
            $as->success();
        });
        $as->copyFrom($model_as);
        $as->successStep();
    });
    $root_as->execute();
}
// Process all pending events
AsyncToolTest::run();
 function testForEachObject()
 {
     $as = $this->as;
     $as->reserr = null;
     $as->add(function ($as) {
         $as->i = 0;
         $o = new \StdClass();
         $o->a = 1;
         $o->b = 2;
         $o->c = 3;
         $as->loopForEach($o, function ($as, $k, $v) {
             switch ($k) {
                 case 'a':
                     $rv = 1;
                     break;
                 case 'b':
                     $rv = 2;
                     break;
                 case 'c':
                     $rv = 3;
                     break;
                 default:
                     $this->assertTrue(false);
             }
             $this->assertEquals($rv, $v);
             $as->i += $v;
         });
     }, function ($as, $err) {
         $as->reserr = $err;
     })->execute();
     AsyncToolTest::run();
     $this->assertNoEvents();
     $this->assertNull($as->reserr);
     $this->assertEquals(6, $as->i);
 }
 public function testCallParallel()
 {
     $this->as->add(function ($as) {
         $this->ccm->register($as, 'srv', 'srv.test:1.1', 'http://localhost:12345/ftn');
     });
     $this->as->run();
     $model_as = new \FutoIn\RI\AsyncSteps();
     $model_as->add(function ($as) {
         $iface = $this->ccm->iface('srv');
         $iface->call($as, 'test', array('ping' => 'PINGPING'));
     }, function ($as, $err) {
         $as->executed = false;
     })->add(function ($as, $rsp) {
         $this->assertEquals('PONGPONG', $rsp->pong);
         $this->assertEquals('PINGPING', $rsp->ping);
         $as->executed = true;
     });
     $asl = array();
     for ($i = 0; $i < 10; ++$i) {
         $asl[] = clone $model_as;
     }
     foreach ($asl as $as) {
         $as->execute();
     }
     \FutoIn\RI\AsyncToolTest::run();
     foreach ($asl as $i => $as) {
         $this->assertTrue($as->executed, "Failed on {$i}");
     }
 }
 public function testSimpleCCM()
 {
     \FutoIn\RI\AsyncToolTest::init();
     new \FutoIn\RI\Invoker\SimpleCCM();
     $this->assertTrue(true);
 }
 public function testModel()
 {
     $model_as = new \FutoIn\RI\AsyncSteps();
     $model_as->model_run = false;
     $model_as->add(function ($as) {
         $as->model_run = true;
         $as->success();
     });
     $as = $this->as;
     $as->copyFrom($model_as);
     $as->add(function ($as) use($model_as) {
         $this->assertTrue($as->model_run);
         $as->model_run = false;
         $as->copyFrom($model_as);
         $as->successStep();
     });
     $as->execute();
     AsyncToolTest::run();
     $this->assertTrue($as->model_run);
 }
 public function testAsyncSteps()
 {
     \FutoIn\RI\AsyncToolTest::init();
     new \FutoIn\RI\AsyncSteps();
     $this->assertTrue(true);
 }