Skip to content

Commit aa6a567

Browse files
committed
Tests implements continue
1 parent e9f8dbe commit aa6a567

File tree

9 files changed

+330
-25
lines changed

9 files changed

+330
-25
lines changed

composer.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
{
22
"name": "micro/plugin-logger-core",
33
"description": "Micro Framework: Logger plugin",
4-
"type": "library",
54
"license": "MIT",
6-
"autoload": {
7-
"psr-4": {
8-
"Micro\\Plugin\\Logger\\": "src/"
9-
}
10-
},
11-
"autoload-dev": {
12-
"psr-4": {
13-
"Micro\\Plugin\\Logger\\Test\\": "tests/"
14-
}
15-
},
5+
"type": "library",
166
"authors": [
177
{
188
"name": "Stanislau.Komar",
199
"email": "stanislau_komar@epam.com"
2010
}
2111
],
2212
"require": {
13+
"micro/kernel": "^1",
2314
"micro/kernel-boot-configuration": "^1",
2415
"micro/kernel-boot-dependency": "^1",
25-
"micro/kernel": "^1",
26-
"psr/log": "^1|^2|^3"
16+
"psr/log": "^1 || ^2 || ^3"
2717
},
2818
"require-dev": {
2919
"ergebnis/composer-normalize": "^2.29",
@@ -33,6 +23,16 @@
3323
"phpunit/phpunit": "^9.5",
3424
"vimeo/psalm": "^5.2"
3525
},
26+
"autoload": {
27+
"psr-4": {
28+
"Micro\\Plugin\\Logger\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Micro\\Plugin\\Logger\\Test\\Unit\\": "tests/Unit"
34+
}
35+
},
3636
"config": {
3737
"allow-plugins": {
3838
"ergebnis/composer-normalize": true
@@ -42,7 +42,7 @@
4242
"scripts": {
4343
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text",
4444
"php-cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --using-cache=no",
45-
"php-cs-try": "./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
45+
"php-cs-try": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
4646
"phpstan": "./vendor/bin/phpstan analyze --no-progress",
4747
"phpunit": "./vendor/bin/phpunit",
4848
"psalm": "./vendor/bin/psalm --no-progress --show-info=true",

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<directory name="src" />
1111
<ignoreFiles>
1212
<directory name="vendor" />
13-
<file name="src/Configuration/LogLevel.php"/>
1413
</ignoreFiles>
1514

1615
</projectFiles>

src/Configuration/LogLevel.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,53 @@
1515

1616
use Psr\Log\LogLevel as PsrLogLevel;
1717

18-
enum LogLevel: string
18+
enum LogLevel
1919
{
20-
case DEBUG = PsrLogLevel::DEBUG;
21-
case CRITICAL = PsrLogLevel::CRITICAL;
22-
case EMERGENCY = PsrLogLevel::EMERGENCY;
23-
case ALERT = PsrLogLevel::ALERT;
24-
case ERROR = PsrLogLevel::ERROR;
25-
case INFO = PsrLogLevel::INFO;
26-
case WARNING = PsrLogLevel::WARNING;
27-
case NOTICE = PsrLogLevel::NOTICE;
20+
case DEBUG;
21+
case CRITICAL;
22+
case EMERGENCY;
23+
case ALERT;
24+
case ERROR;
25+
case INFO;
26+
case WARNING;
27+
case NOTICE;
28+
29+
public function level(): string
30+
{
31+
return match ($this) {
32+
LogLevel::DEBUG => PsrLogLevel::DEBUG,
33+
LogLevel::CRITICAL => PsrLogLevel::CRITICAL,
34+
LogLevel::EMERGENCY => PsrLogLevel::EMERGENCY,
35+
LogLevel::ALERT => PsrLogLevel::ALERT,
36+
LogLevel::ERROR => PsrLogLevel::ERROR,
37+
LogLevel::INFO => PsrLogLevel::INFO,
38+
LogLevel::WARNING => PsrLogLevel::WARNING,
39+
LogLevel::NOTICE => PsrLogLevel::NOTICE,
40+
};
41+
}
42+
43+
public static function getLevelFromString(string $logLevel): LogLevel
44+
{
45+
$toLower = mb_strtolower($logLevel);
46+
switch ($toLower) {
47+
case PsrLogLevel::DEBUG:
48+
return LogLevel::DEBUG;
49+
case PsrLogLevel::CRITICAL:
50+
return LogLevel::CRITICAL;
51+
case PsrLogLevel::EMERGENCY:
52+
return LogLevel::EMERGENCY;
53+
case PsrLogLevel::ALERT:
54+
return LogLevel::ALERT;
55+
case PsrLogLevel::ERROR:
56+
return LogLevel::ERROR;
57+
case PsrLogLevel::INFO:
58+
return LogLevel::INFO;
59+
case PsrLogLevel::WARNING:
60+
return LogLevel::WARNING;
61+
case PsrLogLevel::NOTICE:
62+
return LogLevel::NOTICE;
63+
default:
64+
throw new \RuntimeException('Invalid log level value `%s`.');
65+
}
66+
}
2867
}

src/Configuration/LoggerProviderTypeConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getLogLevel(): LogLevel
3030
return LogLevel::DEBUG;
3131
}
3232

33-
return LogLevel::from(mb_strtoupper($levelString));
33+
return LogLevel::getLevelFromString($levelString);
3434
}
3535

3636
public function getType(): string
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Logger\Test\Unit\Business\Provider;
15+
16+
use Micro\Framework\Kernel\KernelInterface;
17+
use Micro\Plugin\Logger\Business\Provider\LoggerProvider;
18+
use Micro\Plugin\Logger\Configuration\LoggerPluginConfigurationInterface;
19+
use PHPUnit\Framework\TestCase;
20+
use Psr\Log\LoggerInterface;
21+
22+
class LoggerProviderTest extends TestCase
23+
{
24+
public function testGetLogger()
25+
{
26+
$kernel = $this->createMock(KernelInterface::class);
27+
$loggerPluginConfiguration = $this->createMock(LoggerPluginConfigurationInterface::class);
28+
29+
$loggerProvider = new LoggerProvider(
30+
$kernel,
31+
$loggerPluginConfiguration
32+
);
33+
34+
$this->assertInstanceOf(LoggerInterface::class, $loggerProvider->getLogger('default'));
35+
}
36+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Logger\Test\Unit\Configuration;
15+
16+
use Micro\Plugin\Logger\Configuration\LogLevel;
17+
use PHPUnit\Framework\TestCase;
18+
use Psr\Log\LogLevel as PsrLogLevel;
19+
20+
class LogLevelTest extends TestCase
21+
{
22+
/**
23+
* @dataProvider dataProvider
24+
*/
25+
public function testLevel(string $level)
26+
{
27+
if ('not_registered_level' === $level) {
28+
$this->expectException(\RuntimeException::class);
29+
}
30+
31+
$levelObj = LogLevel::getLevelFromString($level);
32+
33+
$this->assertEquals($level, $levelObj->level());
34+
}
35+
36+
public function dataProvider()
37+
{
38+
return [
39+
[PsrLogLevel::DEBUG],
40+
[PsrLogLevel::CRITICAL],
41+
[PsrLogLevel::EMERGENCY],
42+
[PsrLogLevel::ALERT],
43+
[PsrLogLevel::ERROR],
44+
[PsrLogLevel::INFO],
45+
[PsrLogLevel::WARNING],
46+
[PsrLogLevel::NOTICE],
47+
['not_registered_level'],
48+
];
49+
}
50+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Logger\Test\Unit\Configuration;
15+
16+
use Micro\Framework\Kernel\Configuration\ApplicationConfigurationInterface;
17+
use Micro\Plugin\Logger\Configuration\LoggerProviderTypeConfiguration;
18+
use Micro\Plugin\Logger\Configuration\LoggerProviderTypeConfigurationInterface;
19+
use Micro\Plugin\Logger\Configuration\LogLevel;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class LoggerProviderTypeConfigurationTest extends TestCase
23+
{
24+
private ApplicationConfigurationInterface $applicationConfiguration;
25+
26+
private LoggerProviderTypeConfigurationInterface $loggerProviderTypeConfiguration;
27+
28+
protected function setUp(): void
29+
{
30+
$this->applicationConfiguration = $this->createMock(ApplicationConfigurationInterface::class);
31+
32+
$this->loggerProviderTypeConfiguration = new LoggerProviderTypeConfiguration(
33+
$this->applicationConfiguration,
34+
'default'
35+
);
36+
}
37+
38+
/**
39+
* @dataProvider dataProvider
40+
*/
41+
public function testGetLogLevel($loggerLevel)
42+
{
43+
$this->applicationConfiguration
44+
->expects($this->once())
45+
->method('get')
46+
->with('LOGGER_DEFAULT_LOG_LEVEL')
47+
->willReturn($loggerLevel);
48+
49+
if ($loggerLevel) {
50+
$this->assertEquals(LogLevel::getLevelFromString($loggerLevel), $this->loggerProviderTypeConfiguration->getLogLevel());
51+
52+
return;
53+
}
54+
55+
$this->assertEquals(LogLevel::DEBUG->level(), $this->loggerProviderTypeConfiguration->getLogLevel());
56+
}
57+
58+
public function dataProvider()
59+
{
60+
return [
61+
'critical',
62+
null,
63+
];
64+
}
65+
66+
public function testGetType()
67+
{
68+
$this->applicationConfiguration
69+
->expects($this->once())
70+
->method('get')
71+
->with('LOGGER_DEFAULT_PROVIDER_TYPE')
72+
->willReturn('default_type');
73+
74+
$this->assertEquals('default_type', $this->loggerProviderTypeConfiguration->getType());
75+
}
76+
77+
public function testGetLoggerName()
78+
{
79+
$this->assertEquals('default', $this->loggerProviderTypeConfiguration->getLoggerName());
80+
}
81+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Logger\Test\Unit\Facade;
15+
16+
use Micro\Plugin\Logger\Business\Provider\LoggerProviderInterface;
17+
use Micro\Plugin\Logger\Facade\LoggerFacade;
18+
use PHPUnit\Framework\TestCase;
19+
use Psr\Log\LoggerInterface;
20+
21+
class LoggerFacadeTest extends TestCase
22+
{
23+
/**
24+
* @dataProvider dataProvider
25+
*/
26+
public function testGetLogger(string|null $loggerName)
27+
{
28+
$provider = $this->createMock(LoggerProviderInterface::class);
29+
$logger = $this->createMock(LoggerInterface::class);
30+
31+
$provider
32+
->expects($this->once())
33+
->method('getLogger')
34+
->with($loggerName)
35+
->willReturn($logger);
36+
37+
$facade = new LoggerFacade($provider);
38+
39+
$this->assertEquals($logger, $facade->getLogger($loggerName));
40+
}
41+
42+
public function dataProvider()
43+
{
44+
return [
45+
['test'],
46+
[],
47+
];
48+
}
49+
}

0 commit comments

Comments
 (0)