| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace ShlinkioTest\Shlink\Common\Logger\Swoole; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Log\NullLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ReflectionObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Shlinkio\Shlink\Common\Logger\Swoole\AccessLogFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Zend\Expressive\Swoole\Log\AccessLogFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Zend\Expressive\Swoole\Log\AccessLogFormatterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Zend\Expressive\Swoole\Log\Psr3AccessLogDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Zend\Expressive\Swoole\Log\StdoutLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Zend\ServiceManager\ServiceManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function is_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class AccessLogFactoryTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var AccessLogFactory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->factory = new AccessLogFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @test | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     public function createsService() | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $service = ($this->factory)(new ServiceManager(), ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->assertInstanceOf(Psr3AccessLogDecorator::class, $service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @dataProvider provideLoggers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string|LoggerInterface $expectedLogger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function wrapsProperLogger(array $config, $expectedLogger) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $service = ($this->factory)(new ServiceManager(['services' => $config]), ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $ref = new ReflectionObject($service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $loggerProp = $ref->getProperty('logger'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $loggerProp->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $logger = $loggerProp->getValue($service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (is_string($expectedLogger)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->assertInstanceOf($expectedLogger, $logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $this->assertSame($expectedLogger, $logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function provideLoggers(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         yield 'without-any-logger' => [[], StdoutLogger::class]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         yield 'with-standard-logger' => (function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $logger = new NullLogger(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return [[LoggerInterface::class => $logger], $logger]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         })(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         yield 'with-custom-logger' => (function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $logger = new NullLogger(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 'config' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     'zend-expressive-swoole' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         'swoole-http-server' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                             'logger' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                                 'logger_name' => 'my-logger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 'my-logger' => $logger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ], $logger]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         })(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @dataProvider provideFormatters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string|AccessLogFormatterInterface $expectedFormatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function wrappsProperFormatter(array $config, $expectedFormatter, string $expectedFormat) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $service = ($this->factory)(new ServiceManager(['services' => $config]), ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $ref = new ReflectionObject($service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $formatterProp = $ref->getProperty('formatter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $formatterProp->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $formatter = $formatterProp->getValue($service); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $ref = new ReflectionObject($formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $formatProp = $ref->getProperty('format'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $formatProp->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $format = $formatProp->getValue($formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if (is_string($expectedFormatter)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->assertInstanceOf($expectedFormatter, $formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $this->assertSame($expectedFormatter, $formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertSame($expectedFormat, $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function provideFormatters(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         yield 'with-registered-formatter-and-default-format' => (function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $formatter = new AccessLogFormatter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             return [[AccessLogFormatterInterface::class => $formatter], $formatter, AccessLogFormatter::FORMAT_COMMON]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         })(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         yield 'with-registered-formatter-and-custom-format' => (function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $formatter = new AccessLogFormatter(AccessLogFormatter::FORMAT_AGENT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             return [[AccessLogFormatterInterface::class => $formatter], $formatter, AccessLogFormatter::FORMAT_AGENT]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         })(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         yield 'with-no-formatter-and-not-configured-format' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             AccessLogFormatter::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             AccessLogFormatter::FORMAT_COMMON, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         yield 'with-no-formatter-and-configured-format' => [[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'config' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 'zend-expressive-swoole' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     'swoole-http-server' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                         'logger' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                             'format' => AccessLogFormatter::FORMAT_COMBINED_DEBIAN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         ], AccessLogFormatter::class, AccessLogFormatter::FORMAT_COMBINED_DEBIAN]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |