MonologTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testLogDebug() 0 4 1
A testLog() 0 4 1
A testNonScalarLog() 0 4 1
A testBadPriority() 0 4 1
A testLoglevels() 0 21 1
A tearDown() 0 3 1
A setUp() 0 10 2
A testFacility() 0 10 1
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
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

32
		/** @scrutinizer ignore-unhandled */ @unlink( 'monolog.log' );

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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