/** * @covers ::check */ public function testCanCallStatically() { // ---------------------------------------------------------------- // setup your test $stream = new EventStream(); $eventName = HasEventHandlerTest_DummyEvent::class; $handler = function ($event) { }; $this->assertFalse(HasEventHandler::check($stream, $eventName)); // ---------------------------------------------------------------- // perform the change RegisterEventHandler::on($stream, $eventName, $handler); // ---------------------------------------------------------------- // test the results $this->assertTrue(HasEventHandler::check($stream, $eventName)); }
/** * @covers ::on */ public function testCanHaveMultipleHandlersForOneEvent() { // ---------------------------------------------------------------- // setup your test $stream = new EventStream(); $eventName = RegisterEventHandlerTest_DummyEvent::class; $handler1 = function ($event) { }; $handler2 = function ($event) { }; $this->assertFalse(HasEventHandler::check($stream, $eventName)); // ---------------------------------------------------------------- // perform the change RegisterEventHandler::on($stream, $eventName, $handler1); RegisterEventHandler::on($stream, $eventName, $handler2); // ---------------------------------------------------------------- // test the results $handlers = GetEventHandlerList::from($stream, $eventName); $this->assertSame($handlers[0], $handler1); $this->assertSame($handlers[1], $handler2); }
/** * @covers ::to */ public function testDoesNothingIfNoRegisteredEventHandlersForEvent() { // ---------------------------------------------------------------- // setup your test $stream = new EventStream(); $event1 = new DispatchEventTest_DummyEvent1(); $eventName1 = get_class($event1); $actualPayload1 = null; $handler1 = function ($event) use(&$actualPayload1) { $actualPayload1 = $event->payload; }; $expectedPayload1 = "hello, world"; $event1->payload = $expectedPayload1; $event2 = new DispatchEventTest_DummyEvent2(); $eventName2 = get_class($event2); RegisterEventHandler::on($stream, $eventName1, $handler1); // ---------------------------------------------------------------- // perform the change DispatchEvent::to($stream, $event2); // ---------------------------------------------------------------- // test the results $this->assertNull($actualPayload1); }
/** * @covers ::run * @covers ::runCommand */ public function testSendsEventDuringOutput() { // ---------------------------------------------------------------- // setup your test $obj = new PopenProcessRunner(); $stream = new EventStream(); $handlerData = []; $handler = function (ProcessPartialOutput $event) use(&$handlerData) { $handlerData[] = [time(), $event->output]; }; RegisterEventHandler::on($stream, ProcessPartialOutput::class, $handler); // ---------------------------------------------------------------- // perform the change $actualResult = $obj(['php', __DIR__ . '/PartialOutputHelper.php'], null, null, $stream); // ---------------------------------------------------------------- // test the results $this->assertEquals(2, count($handlerData)); $this->assertTrue($handlerData[0][0] < $handlerData[1][0]); }
/** * @covers ::from */ public function testCanCallStatically() { // ---------------------------------------------------------------- // setup your test $stream = new EventStream(); $eventName = GetEventHandlerListTest_DummyEvent::class; $handler = function ($event) { }; $this->assertFalse(HasEventHandler::check($stream, $eventName)); RegisterEventHandler::on($stream, $eventName, $handler); $this->assertTrue(HasEventHandler::check($stream, $eventName)); $expectedResult = [$handler]; // ---------------------------------------------------------------- // perform the change $actualResult = GetEventHandlerList::from($stream, $eventName); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }