| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace GacelaTest\Unit\Framework\Bootstrap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Gacela\Framework\Bootstrap\GacelaConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Gacela\Framework\Bootstrap\SetupGacela; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Gacela\Framework\Event\Dispatcher\NullEventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Gacela\Framework\Event\GacelaEventInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use GacelaTest\Unit\Framework\Config\GacelaFileConfig\Factory\FakeEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | final class SetupGacelaTest extends TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     public function test_null_event_dispatcher(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         $config = new GacelaConfig(); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         $setup = SetupGacela::fromGacelaConfig($config); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         self::assertInstanceOf(NullEventDispatcher::class, $setup->getEventDispatcher()); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $setup->getEventDispatcher()->dispatch(new FakeEvent()); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function test_configurable_event_dispatcher(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $listenerDispatched = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $listener = static function (GacelaEventInterface $event) use (&$listenerDispatched): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             self::assertInstanceOf(FakeEvent::class, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $listenerDispatched = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $config = (new GacelaConfig())->registerGenericListener($listener); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $setup = SetupGacela::fromGacelaConfig($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         self::assertInstanceOf(ConfigurableEventDispatcher::class, $setup->getEventDispatcher()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         self::assertFalse($listenerDispatched); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $setup->getEventDispatcher()->dispatch(new FakeEvent()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |         self::assertTrue($listenerDispatched); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function test_combine_event_dispatcher(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $listenerDispatched1 = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $listenerDispatched2 = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $setup = SetupGacela::fromGacelaConfig( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             (new GacelaConfig())->registerSpecificListener( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 FakeEvent::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 static function (GacelaEventInterface $event) use (&$listenerDispatched1): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     self::assertInstanceOf(FakeEvent::class, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     $listenerDispatched1 = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         self::assertInstanceOf(ConfigurableEventDispatcher::class, $setup->getEventDispatcher()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $setup2 = SetupGacela::fromGacelaConfig( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             (new GacelaConfig())->registerSpecificListener( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 FakeEvent::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 static function (GacelaEventInterface $event) use (&$listenerDispatched2): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     self::assertInstanceOf(FakeEvent::class, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     $listenerDispatched2 = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $setup->combine($setup2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         self::assertFalse($listenerDispatched1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         self::assertFalse($listenerDispatched2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $setup->getEventDispatcher()->dispatch(new FakeEvent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         self::assertTrue($listenerDispatched1); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |         self::assertTrue($listenerDispatched2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |