ErrorlogTest::tearDown()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 */
7
8
9
namespace Aimeos\Base\Logger;
10
11
12
class ErrorlogTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
16
17
	protected function setUp() : void
18
	{
19
		$this->object = new \Aimeos\Base\Logger\Errorlog( \Aimeos\Base\Logger\Iface::DEBUG );
20
	}
21
22
23
	protected function tearDown() : void
24
	{
25
		if( file_exists( 'error.log' ) ) {
26
			unlink( 'error.log' );
27
		}
28
	}
29
30
31
	public function testLog()
32
	{
33
		ini_set( "error_log", "error.log" );
34
35
		$this->object->log( 'error test' );
36
		$this->object->log( 'warning test', \Aimeos\Base\Logger\Iface::WARN );
37
		$this->object->log( 'notice test', \Aimeos\Base\Logger\Iface::NOTICE );
38
		$this->object->log( 'info test', \Aimeos\Base\Logger\Iface::INFO );
39
		$this->object->log( 'debug test', \Aimeos\Base\Logger\Iface::DEBUG );
40
		$this->object->log( array( 'scalar', 'test' ) );
41
42
		ini_restore( "error_log" );
43
44
		$this->assertFileExists( 'error.log', 'Unable to open file "error.log"' );
45
46
		foreach( file( 'error.log' ) as $line ) {
47
			$this->assertMatchesRegularExpression( '/\[[^\]]+\] <message> \[[^\]]+\] \[[^\]]+\] .+test/', $line, $line );
48
		}
49
	}
50
51
52
	public function testLogFacility()
53
	{
54
		ini_set( "error_log", "error.log" );
55
56
		$this->object = new \Aimeos\Base\Logger\Errorlog( \Aimeos\Base\Logger\Iface::DEBUG, array( 'test' ) );
57
		$this->object->log( 'info test', \Aimeos\Base\Logger\Iface::INFO, 'info' );
58
59
		ini_restore( "error_log" );
60
61
		$this->assertFileDoesNotExist( 'error.log', 'File "error.log" should not be created' );
62
	}
63
64
65
	public function testLogLevel()
66
	{
67
		$this->expectException( \Aimeos\Base\Logger\Exception::class );
68
		$this->object->log( 'wrong loglevel test', -1 );
69
	}
70
}
71