AccessLogFactoryTest.php 4.6 KB

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