I am testing a simple event as follows …
I have an event class to pass data
class EventMakeCurrent extends Event {
public bool $result=false;
}
A sender model to populate the event data raise the event
class SenderModel extends Model {
public function triggerEvent() : EventMakeCurrent{
$e = new EventMakeCurrent();
Event::trigger(SenderModel::class, 'event', $e);
return $e;
}
}
And a receiver class with a class level event handler to process the event
Event::on(SenderModel::class, 'event', function($e){
$e->result = true;
$e->note = date('h:i:s', time());
return;
});
class ReceiverModel extends Model
{
}
My unit test is as follows…
class EventTestTest extends Unit
{
public function testEventTrigger()
{
$sender = new SenderModel();
$receiver = new ReceiverModel();
$resultEvent = $sender->triggerEvent();
$this->assertTrue($resultEvent->result);
}
public function testSecondEventTrigger()
{
$sender = new SenderModel();
$receiver = new ReceiverModel();
$resultEvent = $sender->triggerEvent();
$this->assertTrue($resultEvent->result);
}
}
Edit:
The following test passes.
public function testEventTrigger()
{
$sender = new SenderModel();
$receiver = new ReceiverModel();
$resultEvent = $sender->triggerEvent();
$this->assertTrue($resultEvent->result);
$sender = new SenderModel();
$receiver = new ReceiverModel();
$resultEvent = $sender->triggerEvent();
$this->assertTrue($resultEvent->result);
}
The first test passes, then the next identical test fails.
The events is not raised or received in the second test ?
Edit… It seems in the second test that the Event object does not have my event registered.
Returns at.
if (empty(self::$_events[$name]) && empty($wildcardEventHandlers)) {
return;
}
Can anyone reproduce?
Thanks