|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace flight\tests; |
| 6 | + |
| 7 | +use Flight; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use flight\Engine; |
| 10 | +use TypeError; |
| 11 | + |
| 12 | +class EventSystemTest extends TestCase |
| 13 | +{ |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + // Reset the Flight engine before each test to ensure a clean state |
| 17 | + Flight::setEngine(new Engine()); |
| 18 | + Flight::app()->init(); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Test registering and triggering a single listener. |
| 23 | + */ |
| 24 | + public function testRegisterAndTriggerSingleListener() |
| 25 | + { |
| 26 | + $called = false; |
| 27 | + Flight::onEvent('test.event', function () use (&$called) { |
| 28 | + $called = true; |
| 29 | + }); |
| 30 | + Flight::triggerEvent('test.event'); |
| 31 | + $this->assertTrue($called, 'Single listener should be called when event is triggered.'); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Test registering multiple listeners for the same event. |
| 36 | + */ |
| 37 | + public function testRegisterMultipleListeners() |
| 38 | + { |
| 39 | + $counter = 0; |
| 40 | + Flight::onEvent('test.event', function () use (&$counter) { |
| 41 | + $counter++; |
| 42 | + }); |
| 43 | + Flight::onEvent('test.event', function () use (&$counter) { |
| 44 | + $counter++; |
| 45 | + }); |
| 46 | + Flight::triggerEvent('test.event'); |
| 47 | + $this->assertEquals(2, $counter, 'All registered listeners should be called.'); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test triggering an event with no listeners registered. |
| 52 | + */ |
| 53 | + public function testTriggerWithNoListeners() |
| 54 | + { |
| 55 | + // Should not throw any errors |
| 56 | + Flight::triggerEvent('non.existent.event'); |
| 57 | + $this->assertTrue(true, 'Triggering an event with no listeners should not throw an error.'); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test that a listener receives a single argument correctly. |
| 62 | + */ |
| 63 | + public function testListenerReceivesSingleArgument() |
| 64 | + { |
| 65 | + $received = null; |
| 66 | + Flight::onEvent('test.event', function ($arg) use (&$received) { |
| 67 | + $received = $arg; |
| 68 | + }); |
| 69 | + Flight::triggerEvent('test.event', 'hello'); |
| 70 | + $this->assertEquals('hello', $received, 'Listener should receive the passed argument.'); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Test that a listener receives multiple arguments correctly. |
| 75 | + */ |
| 76 | + public function testListenerReceivesMultipleArguments() |
| 77 | + { |
| 78 | + $received = []; |
| 79 | + Flight::onEvent('test.event', function ($arg1, $arg2) use (&$received) { |
| 80 | + $received = [$arg1, $arg2]; |
| 81 | + }); |
| 82 | + Flight::triggerEvent('test.event', 'first', 'second'); |
| 83 | + $this->assertEquals(['first', 'second'], $received, 'Listener should receive all passed arguments.'); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Test that listeners are called in the order they were registered. |
| 88 | + */ |
| 89 | + public function testListenersCalledInOrder() |
| 90 | + { |
| 91 | + $order = []; |
| 92 | + Flight::onEvent('test.event', function () use (&$order) { |
| 93 | + $order[] = 1; |
| 94 | + }); |
| 95 | + Flight::onEvent('test.event', function () use (&$order) { |
| 96 | + $order[] = 2; |
| 97 | + }); |
| 98 | + Flight::triggerEvent('test.event'); |
| 99 | + $this->assertEquals([1, 2], $order, 'Listeners should be called in registration order.'); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test that listeners are not called for unrelated events. |
| 104 | + */ |
| 105 | + public function testListenerNotCalledForOtherEvents() |
| 106 | + { |
| 107 | + $called = false; |
| 108 | + Flight::onEvent('test.event1', function () use (&$called) { |
| 109 | + $called = true; |
| 110 | + }); |
| 111 | + Flight::triggerEvent('test.event2'); |
| 112 | + $this->assertFalse($called, 'Listeners should not be called for different events.'); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Test overriding the onEvent method. |
| 117 | + */ |
| 118 | + public function testOverrideOnEvent() |
| 119 | + { |
| 120 | + $called = false; |
| 121 | + Flight::map('onEvent', function ($event, $callback) use (&$called) { |
| 122 | + $called = true; |
| 123 | + }); |
| 124 | + Flight::onEvent('test.event', function () { |
| 125 | + }); |
| 126 | + $this->assertTrue($called, 'Overridden onEvent method should be called.'); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Test overriding the triggerEvent method. |
| 131 | + */ |
| 132 | + public function testOverrideTriggerEvent() |
| 133 | + { |
| 134 | + $called = false; |
| 135 | + Flight::map('triggerEvent', function ($event, ...$args) use (&$called) { |
| 136 | + $called = true; |
| 137 | + }); |
| 138 | + Flight::triggerEvent('test.event'); |
| 139 | + $this->assertTrue($called, 'Overridden triggerEvent method should be called.'); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Test that an overridden onEvent can still register listeners by calling the original method. |
| 144 | + */ |
| 145 | + public function testOverrideOnEventStillRegistersListener() |
| 146 | + { |
| 147 | + $overrideCalled = false; |
| 148 | + Flight::map('onEvent', function ($event, $callback) use (&$overrideCalled) { |
| 149 | + $overrideCalled = true; |
| 150 | + // Call the original method |
| 151 | + Flight::app()->_onEvent($event, $callback); |
| 152 | + }); |
| 153 | + |
| 154 | + $listenerCalled = false; |
| 155 | + Flight::onEvent('test.event', function () use (&$listenerCalled) { |
| 156 | + $listenerCalled = true; |
| 157 | + }); |
| 158 | + |
| 159 | + Flight::triggerEvent('test.event'); |
| 160 | + |
| 161 | + $this->assertTrue($overrideCalled, 'Overridden onEvent should be called.'); |
| 162 | + $this->assertTrue($listenerCalled, 'Listener should still be triggered after override.'); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Test that an overridden triggerEvent can still trigger listeners by calling the original method. |
| 167 | + */ |
| 168 | + public function testOverrideTriggerEventStillTriggersListeners() |
| 169 | + { |
| 170 | + $overrideCalled = false; |
| 171 | + Flight::map('triggerEvent', function ($event, ...$args) use (&$overrideCalled) { |
| 172 | + $overrideCalled = true; |
| 173 | + // Call the original method |
| 174 | + Flight::app()->_triggerEvent($event, ...$args); |
| 175 | + }); |
| 176 | + |
| 177 | + $listenerCalled = false; |
| 178 | + Flight::onEvent('test.event', function () use (&$listenerCalled) { |
| 179 | + $listenerCalled = true; |
| 180 | + }); |
| 181 | + |
| 182 | + Flight::triggerEvent('test.event'); |
| 183 | + |
| 184 | + $this->assertTrue($overrideCalled, 'Overridden triggerEvent should be called.'); |
| 185 | + $this->assertTrue($listenerCalled, 'Listeners should still be triggered after override.'); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Test that an invalid callable throws an exception (if applicable). |
| 190 | + */ |
| 191 | + public function testInvalidCallableThrowsException() |
| 192 | + { |
| 193 | + $this->expectException(TypeError::class); |
| 194 | + // Assuming the event system validates callables |
| 195 | + Flight::onEvent('test.event', 'not_a_callable'); |
| 196 | + } |
| 197 | +} |
0 commit comments