From d42cbc949ccecb0e27f90ecf5c0b5fe916d33f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 2 Oct 2025 21:14:10 +0200 Subject: [PATCH] Add test with php config files --- .gitignore | 1 + .../Fixtures/php/handlers.php | 16 ++++ .../Fixtures/php/handlers_with_channels.php | 73 +++++++++++++++++++ .../php/multiple_email_recipients.php | 20 +++++ .../Fixtures/php/multiple_handlers.php | 41 +++++++++++ .../Fixtures/php/native_mailer.php | 20 +++++ .../Fixtures/php/new_and_priority.php | 41 +++++++++++ .../Fixtures/php/new_and_priority_import.php | 29 ++++++++ .../Fixtures/php/new_at_end.php | 33 +++++++++ .../Fixtures/php/new_at_end_import.php | 29 ++++++++ .../Fixtures/php/overwriting.php | 27 +++++++ .../Fixtures/php/overwriting_import.php | 29 ++++++++ .../Fixtures/php/parameterized_handlers.php | 33 +++++++++ .../php/process_psr_3_messages_disabled.php | 18 +++++ .../php/process_psr_3_messages_null.php | 17 +++++ .../process_psr_3_messages_with_arguments.php | 24 ++++++ ...ocess_psr_3_messages_without_arguments.php | 18 +++++ .../Fixtures/php/server_log.php | 17 +++++ .../Fixtures/php/single_email_recipient.php | 20 +++++ .../PhpMonologExtensionTest.php | 31 ++++++++ 20 files changed, 537 insertions(+) create mode 100644 tests/DependencyInjection/Fixtures/php/handlers.php create mode 100644 tests/DependencyInjection/Fixtures/php/handlers_with_channels.php create mode 100644 tests/DependencyInjection/Fixtures/php/multiple_email_recipients.php create mode 100644 tests/DependencyInjection/Fixtures/php/multiple_handlers.php create mode 100644 tests/DependencyInjection/Fixtures/php/native_mailer.php create mode 100644 tests/DependencyInjection/Fixtures/php/new_and_priority.php create mode 100644 tests/DependencyInjection/Fixtures/php/new_and_priority_import.php create mode 100644 tests/DependencyInjection/Fixtures/php/new_at_end.php create mode 100644 tests/DependencyInjection/Fixtures/php/new_at_end_import.php create mode 100644 tests/DependencyInjection/Fixtures/php/overwriting.php create mode 100644 tests/DependencyInjection/Fixtures/php/overwriting_import.php create mode 100644 tests/DependencyInjection/Fixtures/php/parameterized_handlers.php create mode 100644 tests/DependencyInjection/Fixtures/php/process_psr_3_messages_disabled.php create mode 100644 tests/DependencyInjection/Fixtures/php/process_psr_3_messages_null.php create mode 100644 tests/DependencyInjection/Fixtures/php/process_psr_3_messages_with_arguments.php create mode 100644 tests/DependencyInjection/Fixtures/php/process_psr_3_messages_without_arguments.php create mode 100644 tests/DependencyInjection/Fixtures/php/server_log.php create mode 100644 tests/DependencyInjection/Fixtures/php/single_email_recipient.php create mode 100644 tests/DependencyInjection/PhpMonologExtensionTest.php diff --git a/.gitignore b/.gitignore index c5158513..df69a525 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +tests/var vendor .php-cs-fixer.cache composer.phar diff --git a/tests/DependencyInjection/Fixtures/php/handlers.php b/tests/DependencyInjection/Fixtures/php/handlers.php new file mode 100644 index 00000000..ccda8cc7 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/handlers.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('noop') + ->type('noop'); +}; diff --git a/tests/DependencyInjection/Fixtures/php/handlers_with_channels.php b/tests/DependencyInjection/Fixtures/php/handlers_with_channels.php new file mode 100644 index 00000000..952cfde9 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/handlers_with_channels.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + +return static function (ContainerConfigurator $containerConfigurator): void { + $services = $containerConfigurator->services(); + + $services->set('security_logger', 'Foo') + ->tag('monolog.logger', ['channel' => 'security']); + + $services->set('doctrine_logger', 'Foo') + ->tag('monolog.logger', ['channel' => 'doctrine']); + + $services->set('foo_logger', 'Foo') + ->tag('monolog.logger', ['channel' => 'foo']); + + $services->set('bar_logger', 'Foo') + ->tag('monolog.logger', ['channel' => 'bar']); + + $containerConfigurator->extension('monolog', [ + 'handlers' => [ + 'custom' => [ + 'type' => 'stream', + 'path' => '/tmp/symfony.log', + 'bubble' => false, + 'level' => 'ERROR', + 'channels' => 'foo', + ], + 'main' => [ + 'type' => 'group', + 'members' => [ + 'nested', + ], + 'channels' => [ + '!foo', + '!bar', + ], + ], + 'nested' => [ + 'type' => 'stream', + ], + 'extra' => [ + 'type' => 'syslog', + 'ident' => 'monolog', + 'facility' => 'user', + 'level' => 'ALERT', + ], + 'more' => [ + 'type' => 'native_mailer', + 'to_email' => 'monitoring@example.org', + 'from_email' => 'webmaster@example.org', + 'subject' => 'Monolog report', + 'level' => 'CRITICAL', + 'channels' => [ + 'type' => 'inclusive', + 'elements' => [ + 'security', + 'doctrine', + ], + ], + ], + ], + ]); +}; diff --git a/tests/DependencyInjection/Fixtures/php/multiple_email_recipients.php b/tests/DependencyInjection/Fixtures/php/multiple_email_recipients.php new file mode 100644 index 00000000..1b95a3f0 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/multiple_email_recipients.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('swift') + ->type('swift_mailer') + ->fromEmail('error@example.com') + ->toEmail(['dev1@example.com', 'dev2@example.com']) + ->subject('An Error Occurred!') + ->level('debug'); +}; diff --git a/tests/DependencyInjection/Fixtures/php/multiple_handlers.php b/tests/DependencyInjection/Fixtures/php/multiple_handlers.php new file mode 100644 index 00000000..28585b8e --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/multiple_handlers.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('custom') + ->type('stream') + ->path('/tmp/symfony.log') + ->bubble(false) + ->level('ERROR') + ->filePermission(438); + + $monologConfig + ->handler('main') + ->type('fingers_crossed') + ->actionLevel('ERROR') + ->passthruLevel('NOTICE') + ->handler('nested'); + + $monologConfig + ->handler('nested') + ->type('stream'); + + $monologConfig + ->handler('filtered') + ->type('filter') + ->acceptedLevels(['WARNING', 'ERROR']) + ->handler('nested2'); + + $monologConfig + ->handler('nested2') + ->type('stream'); +}; diff --git a/tests/DependencyInjection/Fixtures/php/native_mailer.php b/tests/DependencyInjection/Fixtures/php/native_mailer.php new file mode 100644 index 00000000..00000ab5 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/native_mailer.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('mailer') + ->type('native_mailer') + ->fromEmail('foo@example.com') + ->toEmail('bar@exemple.com') + ->subject('a subject') + ->headers(['Foo: bar', 'Baz: inga']); +}; diff --git a/tests/DependencyInjection/Fixtures/php/new_and_priority.php b/tests/DependencyInjection/Fixtures/php/new_and_priority.php new file mode 100644 index 00000000..e82139e4 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/new_and_priority.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + +return static function (ContainerConfigurator $containerConfigurator): void { + $containerConfigurator->import(__DIR__.'/new_and_priority_import.php'); + + $containerConfigurator->extension('monolog', [ + 'handlers' => [ + 'custom' => [ + 'type' => 'stream', + 'path' => '/tmp/symfony.log', + 'bubble' => true, + 'level' => 'WARNING', + ], + 'first' => [ + 'type' => 'rotating_file', + 'path' => '/tmp/monolog.log', + 'bubble' => true, + 'level' => 'ERROR', + 'priority' => 3, + ], + 'last' => [ + 'type' => 'stream', + 'path' => '/tmp/last.log', + 'bubble' => true, + 'level' => 'ERROR', + 'priority' => -3, + ], + ], + ]); +}; diff --git a/tests/DependencyInjection/Fixtures/php/new_and_priority_import.php b/tests/DependencyInjection/Fixtures/php/new_and_priority_import.php new file mode 100644 index 00000000..3bec4f23 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/new_and_priority_import.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('custom') + ->type('stream') + ->path('/tmp/symfony.log') + ->bubble(true) + ->level('ERROR'); + + $monologConfig + ->handler('main') + ->type('buffer') + ->level('INFO') + ->handler('nested'); + + $monologConfig + ->handler('nested') + ->type('stream'); +}; diff --git a/tests/DependencyInjection/Fixtures/php/new_at_end.php b/tests/DependencyInjection/Fixtures/php/new_at_end.php new file mode 100644 index 00000000..4c3bf1cb --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/new_at_end.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + +return static function (ContainerConfigurator $containerConfigurator): void { + $containerConfigurator->import(__DIR__.'/new_at_end_import.php'); + + $containerConfigurator->extension('monolog', [ + 'handlers' => [ + 'custom' => [ + 'type' => 'stream', + 'path' => '/tmp/symfony.log', + 'bubble' => false, + 'level' => 'WARNING', + ], + 'new' => [ + 'type' => 'stream', + 'path' => '/tmp/monolog.log', + 'bubble' => true, + 'level' => 'ERROR', + ], + ], + ]); +}; diff --git a/tests/DependencyInjection/Fixtures/php/new_at_end_import.php b/tests/DependencyInjection/Fixtures/php/new_at_end_import.php new file mode 100644 index 00000000..0bb2c2ec --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/new_at_end_import.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('custom') + ->type('stream') + ->path('/tmp/symfony.log') + ->bubble(true) + ->level('ERROR'); + + $monologConfig + ->handler('main') + ->type('fingers_crossed') + ->actionLevel('ERROR') + ->handler('nested'); + + $monologConfig + ->handler('nested') + ->type('stream'); +}; diff --git a/tests/DependencyInjection/Fixtures/php/overwriting.php b/tests/DependencyInjection/Fixtures/php/overwriting.php new file mode 100644 index 00000000..a4270e2e --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/overwriting.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + +return static function (ContainerConfigurator $containerConfigurator): void { + $containerConfigurator->import(__DIR__.'/overwriting_import.php'); + + $containerConfigurator->extension('monolog', [ + 'handlers' => [ + 'custom' => [ + 'type' => 'stream', + 'path' => '/tmp/symfony.log', + 'bubble' => true, + 'level' => 'WARNING', + ], + ], + ]); +}; diff --git a/tests/DependencyInjection/Fixtures/php/overwriting_import.php b/tests/DependencyInjection/Fixtures/php/overwriting_import.php new file mode 100644 index 00000000..c3770804 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/overwriting_import.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('custom') + ->type('stream') + ->path('/tmp/symfony.log') + ->bubble(false) + ->level('ERROR'); + + $monologConfig + ->handler('main') + ->type('fingers_crossed') + ->actionLevel('ERROR') + ->handler('nested'); + + $monologConfig + ->handler('nested') + ->type('stream'); +}; diff --git a/tests/DependencyInjection/Fixtures/php/parameterized_handlers.php b/tests/DependencyInjection/Fixtures/php/parameterized_handlers.php new file mode 100644 index 00000000..a8e6aace --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/parameterized_handlers.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + +return static function (ContainerConfigurator $containerConfigurator): void { + $parameters = $containerConfigurator->parameters(); + + $parameters->set('channel_parameter', 'some_channel'); + + $services = $containerConfigurator->services(); + + $services->set('foo_logger', 'Foo') + ->tag('monolog.logger', ['channel' => '%channel_parameter%']); + + $containerConfigurator->extension('monolog', [ + 'handlers' => [ + 'custom' => [ + 'type' => 'stream', + 'path' => '/tmp/symfony.log', + 'channels' => '%channel_parameter%', + ], + ], + ]); +}; diff --git a/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_disabled.php b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_disabled.php new file mode 100644 index 00000000..8b058819 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_disabled.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('custom') + ->type('stream') + ->path('/tmp/symfony.log') + ->processPsr3Messages(false); +}; diff --git a/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_null.php b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_null.php new file mode 100644 index 00000000..8988cd6f --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_null.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('custom') + ->type('noop') + ->processPsr3Messages(true); +}; diff --git a/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_with_arguments.php b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_with_arguments.php new file mode 100644 index 00000000..4035a888 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_with_arguments.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('with_arguments') + ->type('stream') + ->processPsr3Messages() + ->dateFormat('Y'); + + $monologConfig + ->handler('without_arguments') + ->type('stream') + ->processPsr3Messages() + ->enabled(true); +}; diff --git a/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_without_arguments.php b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_without_arguments.php new file mode 100644 index 00000000..96e37f4f --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/process_psr_3_messages_without_arguments.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('without_arguments') + ->type('stream') + ->processPsr3Messages() + ->enabled(true); +}; diff --git a/tests/DependencyInjection/Fixtures/php/server_log.php b/tests/DependencyInjection/Fixtures/php/server_log.php new file mode 100644 index 00000000..0bbd1506 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/server_log.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('server_log') + ->type('server_log') + ->host('0:9911'); +}; diff --git a/tests/DependencyInjection/Fixtures/php/single_email_recipient.php b/tests/DependencyInjection/Fixtures/php/single_email_recipient.php new file mode 100644 index 00000000..09ee8564 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/php/single_email_recipient.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return static function (Symfony\Config\MonologConfig $monologConfig): void { + $monologConfig + ->handler('swift') + ->type('swift_mailer') + ->fromEmail('error@example.com') + ->toEmail('error@example.com') + ->subject('An Error Occurred!') + ->level('debug'); +}; diff --git a/tests/DependencyInjection/PhpMonologExtensionTest.php b/tests/DependencyInjection/PhpMonologExtensionTest.php new file mode 100644 index 00000000..2103831f --- /dev/null +++ b/tests/DependencyInjection/PhpMonologExtensionTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection; + +use Symfony\Bundle\MonologBundle\DependencyInjection\Configuration; +use Symfony\Component\Config\Builder\ConfigBuilderGenerator; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; + +class PhpMonologExtensionTest extends FixtureMonologExtensionTestCase +{ + protected function loadFixture(ContainerBuilder $container, $fixture) + { + $container->setDefinition('mailer', new Definition('Swiftmailer')); + + $generator = new ConfigBuilderGenerator(__DIR__.'/../../var/cache'); + $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/Fixtures/php'), null, $generator); + $loader->load($fixture.'.php'); + } +}