aimeos /
ai-monolog
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
| 5 | * @copyright Aimeos (aimeos.org), 2014-2025 |
||
| 6 | */ |
||
| 7 | |||
| 8 | |||
| 9 | namespace Aimeos\Base\Logger; |
||
| 10 | |||
| 11 | |||
| 12 | class MonologTest extends \PHPUnit\Framework\TestCase |
||
| 13 | { |
||
| 14 | private $object; |
||
| 15 | |||
| 16 | |||
| 17 | protected function setUp() : void |
||
| 18 | { |
||
| 19 | if( class_exists( '\\Monolog\\Logger' ) === false ) { |
||
| 20 | $this->markTestSkipped( 'Class \\Monolog\\Logger not found' ); |
||
| 21 | } |
||
| 22 | |||
| 23 | $log = new \Monolog\Logger( 'test' ); |
||
| 24 | $log->pushHandler( new \Monolog\Handler\StreamHandler( 'monolog.log', \Monolog\Logger::INFO ) ); |
||
| 25 | |||
| 26 | $this->object = new \Aimeos\Base\Logger\Monolog( $log ); |
||
| 27 | } |
||
| 28 | |||
| 29 | |||
| 30 | protected function tearDown() : void |
||
| 31 | { |
||
| 32 | @unlink( 'monolog.log' ); |
||
|
0 ignored issues
–
show
|
|||
| 33 | } |
||
| 34 | |||
| 35 | |||
| 36 | public function testLog() |
||
| 37 | { |
||
| 38 | $this->assertInstanceOf( \Aimeos\Base\Logger\Iface::class, $this->object->log( 'error' ) ); |
||
| 39 | $this->assertMatchesRegularExpression( '/^\[[^\]]+\] test.ERROR: error/', file_get_contents( 'monolog.log' ) ); |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | public function testLoglevels() |
||
| 44 | { |
||
| 45 | $this->object->log( 'EMERGENCY', \Aimeos\Base\Logger\Iface::EMERG ); |
||
| 46 | $this->object->log( 'ALERT', \Aimeos\Base\Logger\Iface::ALERT ); |
||
| 47 | $this->object->log( 'CRITICAL', \Aimeos\Base\Logger\Iface::CRIT ); |
||
| 48 | $this->object->log( 'ERROR', \Aimeos\Base\Logger\Iface::ERR ); |
||
| 49 | $this->object->log( 'WARNING', \Aimeos\Base\Logger\Iface::WARN ); |
||
| 50 | $this->object->log( 'NOTICE', \Aimeos\Base\Logger\Iface::NOTICE ); |
||
| 51 | $this->object->log( 'INFO', \Aimeos\Base\Logger\Iface::INFO ); |
||
| 52 | $this->object->log( 'DEBUG', \Aimeos\Base\Logger\Iface::DEBUG ); |
||
| 53 | |||
| 54 | $content = file_get_contents( 'monolog.log' ); |
||
| 55 | |||
| 56 | $this->assertStringContainsString( 'test.EMERGENCY: EMERGENCY', $content ); |
||
| 57 | $this->assertStringContainsString( 'test.ALERT: ALERT', $content ); |
||
| 58 | $this->assertStringContainsString( 'test.CRITICAL: CRITICAL', $content ); |
||
| 59 | $this->assertStringContainsString( 'test.ERROR: ERROR', $content ); |
||
| 60 | $this->assertStringContainsString( 'test.WARNING: WARNING', $content ); |
||
| 61 | $this->assertStringContainsString( 'test.NOTICE: NOTICE', $content ); |
||
| 62 | $this->assertStringContainsString( 'test.INFO: INFO', $content ); |
||
| 63 | $this->assertStringNotContainsString( 'test.DEBUG: DEBUG', $content ); |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | public function testNonScalarLog() |
||
| 68 | { |
||
| 69 | $this->assertInstanceOf( \Aimeos\Base\Logger\Iface::class, $this->object->log( array( 'error', 'error2', 2 ) ) ); |
||
| 70 | $this->assertMatchesRegularExpression( '/^\[[^\]]+\] test.ERROR: \["error","error2",2\]/', file_get_contents( 'monolog.log' ) ); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | public function testLogDebug() |
||
| 75 | { |
||
| 76 | $this->object->log( 'debug', \Aimeos\Base\Logger\Iface::DEBUG ); |
||
| 77 | $this->assertFalse( file_exists( 'monolog.log' ) ); |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | public function testBadPriority() |
||
| 82 | { |
||
| 83 | $this->expectException( '\\Aimeos\\Base\\Logger\\Exception' ); |
||
| 84 | $this->object->log( 'error', -1 ); |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | public function testFacility() |
||
| 89 | { |
||
| 90 | $log = new \Monolog\Logger( 'test' ); |
||
| 91 | $log->pushHandler( new \Monolog\Handler\StreamHandler( 'monolog.log', \Monolog\Logger::INFO ) ); |
||
| 92 | |||
| 93 | $this->object = new \Aimeos\Base\Logger\Monolog( $log, array( 'test' ) ); |
||
| 94 | |||
| 95 | $this->assertInstanceOf( \Aimeos\Base\Logger\Iface::class, $this->object->log( 'error', \Aimeos\Base\Logger\Iface::ERR ) ); |
||
| 96 | |||
| 97 | $this->assertFalse( file_exists( 'monolog.log' ) ); |
||
| 98 | } |
||
| 99 | } |
||
| 100 |
If you suppress an error, we recommend checking for the error condition explicitly: