|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AutoExcludedChannelsPass; |
| 16 | +use Symfony\Bundle\MonologBundle\MonologBundle; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 18 | + |
| 19 | +class AutoExcludedChannelsPassTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @group legacy |
| 23 | + * |
| 24 | + * @dataProvider handlerChannels |
| 25 | + */ |
| 26 | + public function testProcess(?array $autoExcludedChannels, array $handlers, ?array $expectedChannels, array $expectedLog): void |
| 27 | + { |
| 28 | + $container = new ContainerBuilder(); |
| 29 | + |
| 30 | + $bundle = new MonologBundle(); |
| 31 | + $container->registerExtension($bundle->getContainerExtension()); |
| 32 | + $bundle->build($container); |
| 33 | + |
| 34 | + $container->loadFromExtension('monolog', [ |
| 35 | + 'channels' => ['channel1', 'channel2', 'channel3', 'channel4'], |
| 36 | + 'auto_excluded_channels' => $autoExcludedChannels, |
| 37 | + 'handlers' => $handlers, |
| 38 | + ]); |
| 39 | + |
| 40 | + $container->compile(); |
| 41 | + |
| 42 | + $this->assertSame($expectedChannels, $container->getParameter('monolog.handlers_to_channels')); |
| 43 | + $this->assertFalse($container->hasParameter('monolog.auto_excluded_channels')); |
| 44 | + |
| 45 | + $this->assertSame($expectedLog, array_values(array_filter( |
| 46 | + $container->getCompiler()->getLog(), |
| 47 | + function (string $log) { |
| 48 | + return 0 === strpos($log, AutoExcludedChannelsPass::class); |
| 49 | + } |
| 50 | + ))); |
| 51 | + } |
| 52 | + |
| 53 | + public static function handlerChannels(): iterable |
| 54 | + { |
| 55 | + yield 'No auto-excluded channels' => [ |
| 56 | + null, |
| 57 | + [ |
| 58 | + 'foo' => [ |
| 59 | + 'type' => 'console', |
| 60 | + 'channels' => ['!channel1'], |
| 61 | + ], |
| 62 | + ], |
| 63 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]], |
| 64 | + [], |
| 65 | + ]; |
| 66 | + yield 'Empty auto-excluded channels array' => [ |
| 67 | + [], |
| 68 | + [ |
| 69 | + 'foo' => [ |
| 70 | + 'type' => 'console', |
| 71 | + 'channels' => ['!channel1'], |
| 72 | + ], |
| 73 | + ], |
| 74 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]], |
| 75 | + [], |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'No channels' => [ |
| 79 | + ['channel1'], |
| 80 | + [ |
| 81 | + 'foo' => [ |
| 82 | + 'type' => 'console', |
| 83 | + 'channels' => null, |
| 84 | + ], |
| 85 | + ], |
| 86 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]], |
| 87 | + [self::getLog('foo', ['channel1'])], |
| 88 | + ]; |
| 89 | + yield 'Empty channels array' => [ |
| 90 | + ['channel1'], |
| 91 | + [ |
| 92 | + 'foo' => [ |
| 93 | + 'type' => 'console', |
| 94 | + 'channels' => [], |
| 95 | + ], |
| 96 | + ], |
| 97 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]], |
| 98 | + [self::getLog('foo', ['channel1'])], |
| 99 | + ]; |
| 100 | + |
| 101 | + yield 'Inclusive' => [ |
| 102 | + ['channel2'], |
| 103 | + [ |
| 104 | + 'foo' => [ |
| 105 | + 'type' => 'console', |
| 106 | + 'channels' => ['channel1'], |
| 107 | + ], |
| 108 | + ], |
| 109 | + ['monolog.handler.foo' => ['type' => 'inclusive', 'elements' => ['channel1']]], |
| 110 | + [], |
| 111 | + ]; |
| 112 | + |
| 113 | + yield 'Exclusive without exception' => [ |
| 114 | + ['channel2'], |
| 115 | + [ |
| 116 | + 'foo' => [ |
| 117 | + 'type' => 'console', |
| 118 | + 'channels' => ['!channel1'], |
| 119 | + ], |
| 120 | + ], |
| 121 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1', 'channel2']]], |
| 122 | + [self::getLog('foo', ['channel2'])], |
| 123 | + ]; |
| 124 | + yield 'Exclusive with exception' => [ |
| 125 | + ['channel2'], |
| 126 | + [ |
| 127 | + 'foo' => [ |
| 128 | + 'type' => 'console', |
| 129 | + 'channels' => ['!channel1', '!!channel2'], |
| 130 | + ], |
| 131 | + ], |
| 132 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]], |
| 133 | + [], |
| 134 | + ]; |
| 135 | + yield 'Exclusive with only an exception' => [ |
| 136 | + ['channel1'], |
| 137 | + [ |
| 138 | + 'foo' => [ |
| 139 | + 'type' => 'console', |
| 140 | + 'channels' => ['!!channel1'], |
| 141 | + ], |
| 142 | + ], |
| 143 | + ['monolog.handler.foo' => null], |
| 144 | + [], |
| 145 | + ]; |
| 146 | + |
| 147 | + yield 'Explicitly excluded' => [ |
| 148 | + ['channel1'], |
| 149 | + [ |
| 150 | + 'foo' => [ |
| 151 | + 'type' => 'console', |
| 152 | + 'channels' => ['!channel1'], |
| 153 | + ], |
| 154 | + ], |
| 155 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel1']]], |
| 156 | + [], |
| 157 | + ]; |
| 158 | + |
| 159 | + yield 'Multiple auto-excluded channels' => [ |
| 160 | + ['channel1', 'channel3'], |
| 161 | + [ |
| 162 | + 'foo' => [ |
| 163 | + 'type' => 'console', |
| 164 | + 'channels' => ['!channel2'], |
| 165 | + ], |
| 166 | + ], |
| 167 | + ['monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel2', 'channel1', 'channel3']]], |
| 168 | + [self::getLog('foo', ['channel1', 'channel3'])], |
| 169 | + ]; |
| 170 | + |
| 171 | + yield 'Multiple handlers' => [ |
| 172 | + ['channel1', 'channel3'], |
| 173 | + [ |
| 174 | + 'foo' => [ |
| 175 | + 'type' => 'console', |
| 176 | + 'channels' => ['!channel2'], |
| 177 | + ], |
| 178 | + 'bar' => [ |
| 179 | + 'type' => 'console', |
| 180 | + 'channels' => ['channel1', 'channel4'], |
| 181 | + ], |
| 182 | + 'baz' => [ |
| 183 | + 'type' => 'console', |
| 184 | + 'channels' => ['!!channel1', '!channel2'], |
| 185 | + ], |
| 186 | + ], |
| 187 | + [ |
| 188 | + 'monolog.handler.baz' => ['type' => 'exclusive', 'elements' => ['channel2', 'channel3']], |
| 189 | + 'monolog.handler.bar' => ['type' => 'inclusive', 'elements' => ['channel1', 'channel4']], |
| 190 | + 'monolog.handler.foo' => ['type' => 'exclusive', 'elements' => ['channel2', 'channel1', 'channel3']], |
| 191 | + ], |
| 192 | + [ |
| 193 | + self::getLog('baz', ['channel3']), |
| 194 | + self::getLog('foo', ['channel1', 'channel3']), |
| 195 | + ], |
| 196 | + ]; |
| 197 | + } |
| 198 | + |
| 199 | + private static function getLog(string $handler, array $channels): string |
| 200 | + { |
| 201 | + return sprintf('%s: Auto-excluded the following channels from the "%s" handler: "%s".', AutoExcludedChannelsPass::class, $handler, implode('", "', $channels)); |
| 202 | + } |
| 203 | +} |
0 commit comments