Exemple #1
0
 public function testNotifyUntil()
 {
     $d = new EventDispatcher();
     $o = new \stdClass();
     $n = new Notifier($d, $o);
     $n->notifyUntil('foo');
     $l = new TestListener();
     $d->addListener($l);
     $e = $l->getLastReceivedEvent();
     $this->assertEquals($o, $e->getSender());
     $this->assertEquals('foo', $e->getName());
 }
Exemple #2
0
 public function testProxy()
 {
     $o = new TestClass();
     $d = new EventDispatcher();
     $l = new TestListener();
     $d->addListener($l);
     $p = new EventProxy($o, $d);
     $p->foo = 'bar';
     $p->foo;
     isset($p->foo);
     unset($p->foo);
     $p->foobar();
     $e = $l->getReceivedEvents();
     $this->assertCount(5, $e);
     $this->assertEquals('proxy.set', $e[0]->getName());
     $this->assertEquals($o, $e[0]->getSender());
     $this->assertEquals('proxy.get', $e[1]->getName());
     $this->assertEquals('proxy.isset', $e[2]->getName());
     $this->assertEquals('proxy.unset', $e[3]->getName());
     $this->assertEquals('proxy.call', $e[4]->getName());
 }
Exemple #3
0
        $this->notifier = new Notifier($dispatcher, $this, 'car.');
    }
    public function forward($time = 1)
    {
        $this->notifier->notify('forward', array('time' => $time));
    }
    public function turnLeft($degree = 90)
    {
        $this->notifier->notify('turn.left', array('degree' => $degree));
    }
    public function turnRight($degree = 90)
    {
        $this->notifier->notify('turn.right', array('degree' => $degree));
    }
}
$dispatcher = new EventDispatcher();
$dispatcher->on('car.forward', function ($e) {
    echo "The car went forward for {$e->time} sec\n";
});
$dispatcher->on('car.turn.left', function ($e) {
    echo "The car turned left of {$e->degree} degrees\n";
});
$dispatcher->on('car.turn.right', function ($e) {
    echo "The car turned right of {$e->degree} degrees\n";
});
$dispatcher->on('/^car\\.turn\\.(left|right)$/', function ($e) {
    echo "The car turned\n";
});
$dispatcher->on('car.*', function ($e) {
    echo "Something happened to the car!\n";
});
 public function testEventDispatcherAsListener()
 {
     $dispatcherListener = new EventDispatcher();
     $listener = new TestListener();
     $dispatcherListener->addListener($listener);
     $dispatcher = new EventDispatcher();
     $dispatcher->addListener($dispatcherListener);
     $event = new Event(null, 'test');
     $dispatcher->notify($event);
     $this->assertEquals($event, $listener->getLastReceivedEvent());
 }