|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Blitz PHP framework. |
| 5 | + * |
| 6 | + * (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use BlitzPHP\Container\Services; |
| 13 | +use BlitzPHP\Contracts\Event\EventInterface; |
| 14 | + |
| 15 | +use function Kahlan\expect; |
| 16 | + |
| 17 | +describe('Events / Event', function (): void { |
| 18 | + beforeAll(function (): void { |
| 19 | + $this->eventManager = Services::event(); |
| 20 | + $this->eventManager->clearListeners(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(function (): void { |
| 24 | + $this->eventManager->clearListeners(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('Listeners', function (): void { |
| 28 | + it('Les callbacks sont bien enregistrés', function (): void { |
| 29 | + $callback1 = static function (): void { |
| 30 | + }; |
| 31 | + $callback2 = static function (): void { |
| 32 | + }; |
| 33 | + |
| 34 | + $this->eventManager->on('foo', $callback1); |
| 35 | + $this->eventManager->on('foo', $callback2); |
| 36 | + |
| 37 | + expect($this->eventManager->getListeners('foo')[0])->toBe([$callback1, $callback2]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('clearListeners', function (): void { |
| 41 | + $callback1 = static function (): void { |
| 42 | + }; |
| 43 | + $callback2 = static function (): void { |
| 44 | + }; |
| 45 | + $callback3 = static function (): void { |
| 46 | + }; |
| 47 | + |
| 48 | + $this->eventManager->on('foo', $callback1); |
| 49 | + $this->eventManager->on('foo', $callback3); |
| 50 | + $this->eventManager->on('bar', $callback2); |
| 51 | + $this->eventManager->on('baz', $callback2); |
| 52 | + |
| 53 | + expect($this->eventManager->getListeners())->toBe([ |
| 54 | + 'foo' => [[$callback1, $callback3]], |
| 55 | + 'bar' => [[$callback2]], |
| 56 | + 'baz' => [[$callback2]], |
| 57 | + ]); |
| 58 | + |
| 59 | + $this->eventManager->clearListeners('foo'); |
| 60 | + |
| 61 | + expect($this->eventManager->getListeners())->toBe([ |
| 62 | + 'bar' => [[$callback2]], |
| 63 | + 'baz' => [[$callback2]], |
| 64 | + ]); |
| 65 | + |
| 66 | + $this->eventManager->clearListeners(); |
| 67 | + |
| 68 | + expect($this->eventManager->getListeners())->toBe([]); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + define('Execution', function(): void { |
| 73 | + it('Execute un event', function (): void { |
| 74 | + $result = null; |
| 75 | + $this->eventManager->on('foo', static function (EventInterface $event) use (&$result): void { |
| 76 | + $result = $event->getTarget(); |
| 77 | + }); |
| 78 | + |
| 79 | + $this->eventManager->emit('foo', 'bar'); |
| 80 | + |
| 81 | + expect($result)->toBe('bar'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Execute un event avec une classe callable', function (): void { |
| 85 | + $box = new class () { |
| 86 | + public string $logged; |
| 87 | + |
| 88 | + public function hold($event): void |
| 89 | + { |
| 90 | + $this->logged = $event->getTarget(); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + $this->eventManager->on('foo', $box->hold(...)); |
| 95 | + |
| 96 | + $this->eventManager->emit('foo', 'bar'); |
| 97 | + |
| 98 | + expect($box->logged)->toBe('bar'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('Arret de l\'execution', function (): void { |
| 103 | + it('Arrete l\'execution des autres listeners lorsque FALSE est renvoyé', function (): void { |
| 104 | + $result = null; |
| 105 | + |
| 106 | + $this->eventManager->on('foo', static function () use (&$result) { |
| 107 | + $result = 1; |
| 108 | + |
| 109 | + return false; |
| 110 | + }); |
| 111 | + $this->eventManager->on('foo', static function () use (&$result): void { |
| 112 | + $result = 2; |
| 113 | + }); |
| 114 | + |
| 115 | + $this->eventManager->emit('foo'); |
| 116 | + |
| 117 | + expect($result)->toBe(1); |
| 118 | + }); |
| 119 | + |
| 120 | + it('Arrete l\'execution des autres listeners lorsque stopPropagation est utilisé', function (): void { |
| 121 | + $result = null; |
| 122 | + |
| 123 | + $this->eventManager->on('foo', static function (EventInterface $event) use (&$result): void { |
| 124 | + $result = 1; |
| 125 | + |
| 126 | + $event->stopPropagation(); |
| 127 | + }); |
| 128 | + $this->eventManager->on('foo', static function () use (&$result): void { |
| 129 | + $result = 2; |
| 130 | + }); |
| 131 | + |
| 132 | + $this->eventManager->emit('foo'); |
| 133 | + |
| 134 | + expect($result)->toBe(1); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('Priorite', function (): void { |
| 139 | + it('Priorite', function (): void { |
| 140 | + $result = 0; |
| 141 | + |
| 142 | + $this->eventManager->on('foo', static function () use (&$result) { |
| 143 | + $result = 1; |
| 144 | + |
| 145 | + return false; |
| 146 | + }, EventInterface::PRIORITY_NORMAL); |
| 147 | + |
| 148 | + // Ceci doit etre lancer en premier car elle a une priorite elevee |
| 149 | + $this->eventManager->on('foo', static function () use (&$result) { |
| 150 | + $result = 2; |
| 151 | + |
| 152 | + return false; |
| 153 | + }, EventInterface::PRIORITY_HIGH); |
| 154 | + |
| 155 | + $this->eventManager->emit('foo'); |
| 156 | + |
| 157 | + expect($result)->toBe(2); |
| 158 | + }); |
| 159 | + |
| 160 | + it('Priorite multiple', function (): void { |
| 161 | + $result = []; |
| 162 | + |
| 163 | + $this->eventManager->on('foo', static function () use (&$result): void { |
| 164 | + $result[] = 'a'; |
| 165 | + }, EventInterface::PRIORITY_NORMAL); |
| 166 | + |
| 167 | + $this->eventManager->on('foo', static function () use (&$result): void { |
| 168 | + $result[] = 'b'; |
| 169 | + }, EventInterface::PRIORITY_LOW); |
| 170 | + |
| 171 | + $this->eventManager->on('foo', static function () use (&$result): void { |
| 172 | + $result[] = 'c'; |
| 173 | + }, EventInterface::PRIORITY_HIGH); |
| 174 | + |
| 175 | + $this->eventManager->on('foo', static function () use (&$result): void { |
| 176 | + $result[] = 'd'; |
| 177 | + }, 75); |
| 178 | + |
| 179 | + $this->eventManager->emit('foo'); |
| 180 | + |
| 181 | + expect($result)->toBe(['c', 'd', 'a', 'b']); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe('Retrait de listener ', function (): void { |
| 186 | + it('Le retrait de listener fonctionne', function (): void { |
| 187 | + $result = false; |
| 188 | + |
| 189 | + $callback = static function () use (&$result): void { |
| 190 | + $result = true; |
| 191 | + }; |
| 192 | + |
| 193 | + $this->eventManager->on('foo', $callback); |
| 194 | + |
| 195 | + $this->eventManager->emit('foo'); |
| 196 | + expect($result)->toBeTruthy(); |
| 197 | + |
| 198 | + $result = false; |
| 199 | + expect($this->eventManager->off('foo', $callback))->toBeTruthy(); |
| 200 | + |
| 201 | + $this->eventManager->emit('foo'); |
| 202 | + expect($result)->toBeFalsy(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('Retire le listener une seule fois', function (): void { |
| 206 | + $result = false; |
| 207 | + |
| 208 | + $callback = static function () use (&$result): void { |
| 209 | + $result = true; |
| 210 | + }; |
| 211 | + |
| 212 | + $this->eventManager->on('foo', $callback); |
| 213 | + |
| 214 | + $this->eventManager->emit('foo'); |
| 215 | + expect($result)->toBeTruthy(); |
| 216 | + |
| 217 | + $result = false; |
| 218 | + expect($this->eventManager->off('foo', $callback))->toBeTruthy(); |
| 219 | + expect($this->eventManager->off('foo', $callback))->toBeFalsy(); |
| 220 | + |
| 221 | + $this->eventManager->emit('foo'); |
| 222 | + expect($result)->toBeFalsy(); |
| 223 | + }); |
| 224 | + |
| 225 | + it('Retrait d\'un listener inconnue', function (): void { |
| 226 | + $result = false; |
| 227 | + |
| 228 | + $callback = static function () use (&$result): void { |
| 229 | + $result = true; |
| 230 | + }; |
| 231 | + |
| 232 | + $this->eventManager->on('foo', $callback); |
| 233 | + |
| 234 | + $this->eventManager->emit('foo'); |
| 235 | + expect($result)->toBeTruthy(); |
| 236 | + |
| 237 | + $result = false; |
| 238 | + expect($this->eventManager->off('bar', $callback))->toBeFalsy(); |
| 239 | + |
| 240 | + $this->eventManager->emit('foo'); |
| 241 | + expect($result)->toBeTruthy(); |
| 242 | + }); |
| 243 | + }); |
| 244 | +}); |
0 commit comments