Skip to content

Commit e8a31f7

Browse files
committed
Phpunit tests
1 parent 455a738 commit e8a31f7

File tree

12 files changed

+351
-46
lines changed

12 files changed

+351
-46
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"scripts": {
4343
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text",
44+
"coverage-html": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html ./test-coverage-report",
4445
"php-cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --using-cache=no",
4546
"php-cs-try": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
4647
"phpstan": "./vendor/bin/phpstan analyze --no-progress",

src/Business/Provider/LoggerProvider.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Micro\Framework\Kernel\KernelInterface;
1515
use Micro\Plugin\Logger\Configuration\LoggerPluginConfigurationInterface;
1616
use Micro\Plugin\Logger\Configuration\LoggerProviderTypeConfigurationInterface;
17+
use Micro\Plugin\Logger\Exception\LoggerAdapterAlreadyExistsException;
18+
use Micro\Plugin\Logger\Exception\LoggerAdapterNameInvalidException;
1719
use Micro\Plugin\Logger\Exception\LoggerAdapterNotRegisteredException;
1820
use Micro\Plugin\Logger\Plugin\LoggerProviderPluginInterface;
1921
use Psr\Log\LoggerInterface;
@@ -47,7 +49,6 @@ public function getLogger(string $loggerName): LoggerInterface
4749
}
4850

4951
$loggerProviderTypeConfig = $this->loggerPluginConfiguration->getLoggerProviderTypeConfig($loggerName);
50-
5152
$this->lookupLoggerAdapters();
5253
$logger = $this->createLogger($loggerProviderTypeConfig);
5354

@@ -95,7 +96,16 @@ protected function lookupLoggerAdapters(): void
9596
$installed = false;
9697
/** @var LoggerProviderPluginInterface $adapter */
9798
foreach ($iterator as $adapter) {
98-
$this->loggerAdapters[$adapter->getLoggerAdapterName()] = $adapter;
99+
$adapterName = trim($adapter->getLoggerAdapterName());
100+
if (!$adapterName) {
101+
throw new LoggerAdapterNameInvalidException(sprintf('Logger adapter `%s::getLoggerAdapterName()` is empty.', \get_class($adapter)));
102+
}
103+
104+
if (\array_key_exists($adapterName, $this->loggerAdapters)) {
105+
throw new LoggerAdapterAlreadyExistsException(sprintf('Logger adapter with alias `%s` already exists.', $adapterName));
106+
}
107+
108+
$this->loggerAdapters[$adapterName] = $adapter;
99109
$installed = true;
100110
}
101111

src/Configuration/LogLevel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function getLevelFromString(string $logLevel): LogLevel
6161
case PsrLogLevel::NOTICE:
6262
return LogLevel::NOTICE;
6363
default:
64-
throw new \RuntimeException('Invalid log level value `%s`.');
64+
throw new \RuntimeException(sprintf('Invalid log level value `%s`.', $logLevel));
6565
}
6666
}
6767
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Exception;
15+
16+
/**
17+
* @author Stanislau Komar <head.trackingsoft@gmail.com>
18+
*/
19+
class LoggerAdapterAlreadyExistsException extends LoggerAdapterException
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Exception;
15+
16+
/**
17+
* @author Stanislau Komar <head.trackingsoft@gmail.com>
18+
*/
19+
class LoggerAdapterException extends \RuntimeException
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Exception;
15+
16+
/**
17+
* @author Stanislau Komar <head.trackingsoft@gmail.com>
18+
*/
19+
class LoggerAdapterNameInvalidException extends LoggerAdapterException
20+
{
21+
}

src/Exception/LoggerAdapterNotRegisteredException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
/**
1717
* @author Stanislau Komar <head.trackingsoft@gmail.com>
1818
*/
19-
class LoggerAdapterNotRegisteredException extends \RuntimeException
19+
class LoggerAdapterNotRegisteredException extends LoggerAdapterException
2020
{
2121
}

tests/Unit/Business/Provider/LoggerProviderTest.php

Lines changed: 178 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,193 @@
1414
namespace Micro\Plugin\Logger\Test\Unit\Business\Provider;
1515

1616
use Micro\Framework\Kernel\KernelInterface;
17+
use Micro\Plugin\Logger\Business\Factory\LoggerFactoryInterface;
1718
use Micro\Plugin\Logger\Business\Provider\LoggerProvider;
1819
use Micro\Plugin\Logger\Configuration\LoggerPluginConfigurationInterface;
20+
use Micro\Plugin\Logger\Configuration\LoggerProviderTypeConfigurationInterface;
21+
use Micro\Plugin\Logger\Exception\LoggerAdapterAlreadyExistsException;
22+
use Micro\Plugin\Logger\Exception\LoggerAdapterNameInvalidException;
23+
use Micro\Plugin\Logger\Exception\LoggerAdapterNotRegisteredException;
24+
use Micro\Plugin\Logger\Plugin\LoggerProviderPluginInterface;
1925
use PHPUnit\Framework\TestCase;
2026
use Psr\Log\LoggerInterface;
2127

2228
class LoggerProviderTest extends TestCase
2329
{
24-
public function testGetLogger()
30+
private LoggerProvider $loggerProvider;
31+
32+
private KernelInterface $kernel;
33+
34+
private LoggerPluginConfigurationInterface $loggerPluginConfiguration;
35+
36+
private LoggerFactoryInterface $loggerFactory;
37+
38+
private LoggerProviderPluginInterface $loggerProviderPlugin;
39+
40+
private LoggerInterface $logger;
41+
42+
protected function setUp(): void
43+
{
44+
$this->kernel = $this->createMock(KernelInterface::class);
45+
$this->loggerPluginConfiguration = $this->createMock(LoggerPluginConfigurationInterface::class);
46+
$this->loggerFactory = $this->createMock(LoggerFactoryInterface::class);
47+
$this->loggerProviderPlugin = $this->createMock(LoggerProviderPluginInterface::class);
48+
$this->logger = $this->createMock(LoggerInterface::class);
49+
50+
$this->loggerProvider = new LoggerProvider($this->kernel, $this->loggerPluginConfiguration);
51+
}
52+
53+
public function testGetLoggerWhenOnlyOneproviderRegistered(): void
54+
{
55+
$loggerProviderTypeConfig = $this->createMock(LoggerProviderTypeConfigurationInterface::class);
56+
57+
$this->loggerPluginConfiguration
58+
->method('getLoggerProviderTypeConfig')
59+
->with('loggerName')
60+
->willReturn($loggerProviderTypeConfig);
61+
62+
$this->kernel
63+
->expects($this->once())
64+
->method('plugins')
65+
->with(LoggerProviderPluginInterface::class)
66+
->willReturn([$this->loggerProviderPlugin]);
67+
68+
$this->loggerFactory
69+
->expects($this->once())
70+
->method('create')
71+
->with($loggerProviderTypeConfig)
72+
->willReturn($this->logger);
73+
74+
$this->loggerProviderPlugin
75+
->expects($this->once())
76+
->method('getLoggerFactory')
77+
->willReturn($this->loggerFactory);
78+
79+
$this->loggerProviderPlugin
80+
->expects($this->once())
81+
->method('getLoggerAdapterName')
82+
->willReturn('test_adapter');
83+
84+
$this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName'));
85+
// Cached
86+
$this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName'));
87+
}
88+
89+
/**
90+
* @dataProvider dataProviderGetLoggerWhenManyOneProviderRegistered
91+
*/
92+
public function testGetLoggerWhenManyOneProviderRegistered(bool $shouldAdaptedNotFoundException): void
93+
{
94+
$loggerProviderTypeConfig = $this->createMock(LoggerProviderTypeConfigurationInterface::class);
95+
$loggerProviderTypeConfig
96+
->expects($this->once())
97+
->method('getType')
98+
->willReturn($shouldAdaptedNotFoundException ? 'not_registered_adapter' : 'test_adapter');
99+
100+
if ($shouldAdaptedNotFoundException) {
101+
$this->expectException(LoggerAdapterNotRegisteredException::class);
102+
}
103+
104+
$providerNotUsed = $this->createMock(LoggerProviderPluginInterface::class);
105+
$providerNotUsed
106+
->expects($this->once())
107+
->method('getLoggerAdapterName')
108+
->willReturn('not_used_provider');
109+
110+
$this->loggerPluginConfiguration
111+
->method('getLoggerProviderTypeConfig')
112+
->with('loggerName')
113+
->willReturn($loggerProviderTypeConfig);
114+
115+
$this->loggerProviderPlugin
116+
->expects($this->once())
117+
->method('getLoggerAdapterName')
118+
->willReturn('test_adapter');
119+
120+
$this->kernel
121+
->expects($this->once())
122+
->method('plugins')
123+
->with(LoggerProviderPluginInterface::class)
124+
->willReturn([
125+
$providerNotUsed,
126+
$this->loggerProviderPlugin,
127+
]);
128+
129+
if (!$shouldAdaptedNotFoundException) {
130+
$this->loggerFactory
131+
->expects($this->once())
132+
->method('create')
133+
->with($loggerProviderTypeConfig)
134+
->willReturn($this->logger);
135+
136+
$this->loggerProviderPlugin
137+
->expects($this->once())
138+
->method('getLoggerFactory')
139+
->willReturn($this->loggerFactory);
140+
}
141+
142+
$this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName'));
143+
// Cached
144+
$this->assertSame($this->logger, $this->loggerProvider->getLogger('loggerName'));
145+
}
146+
147+
public function dataProviderGetLoggerWhenManyOneProviderRegistered()
148+
{
149+
return [
150+
[true],
151+
[false],
152+
];
153+
}
154+
155+
public function testGetLoggerNotRegisteredAdapterThrowsException(): void
25156
{
26-
$kernel = $this->createMock(KernelInterface::class);
27-
$loggerPluginConfiguration = $this->createMock(LoggerPluginConfigurationInterface::class);
157+
$this->expectException(LoggerAdapterAlreadyExistsException::class);
28158

29-
$loggerProvider = new LoggerProvider(
30-
$kernel,
31-
$loggerPluginConfiguration
32-
);
159+
$this->loggerProviderPlugin
160+
->method('getLoggerAdapterName')
161+
->willReturn('duplicated_provider');
162+
163+
$this->kernel
164+
->expects($this->once())
165+
->method('plugins')
166+
->willReturn([
167+
$this->loggerProviderPlugin,
168+
$this->loggerProviderPlugin,
169+
])
170+
;
171+
172+
$this->loggerProvider->getLogger('loggerName');
173+
}
174+
175+
public function testAdapterNameInvalidThrowsException(): void
176+
{
177+
$this->expectException(LoggerAdapterNameInvalidException::class);
178+
179+
$this->loggerProviderPlugin
180+
->method('getLoggerAdapterName')
181+
->willReturn(' ');
182+
183+
$this->kernel
184+
->expects($this->once())
185+
->method('plugins')
186+
->willReturn([
187+
$this->loggerProviderPlugin,
188+
])
189+
;
190+
191+
$this->loggerProvider->getLogger('loggerName');
192+
}
193+
194+
public function testAdapterAlreadyRegisteredThrowsException(): void
195+
{
196+
$this->expectException(LoggerAdapterNotRegisteredException::class);
197+
$this->kernel
198+
->expects($this->once())
199+
->method('plugins')
200+
->willReturn([
201+
])
202+
;
33203

34-
$this->assertInstanceOf(LoggerInterface::class, $loggerProvider->getLogger('default'));
204+
$this->loggerProvider->getLogger('loggerName');
35205
}
36206
}

tests/Unit/Configuration/LogLevelTest.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,33 @@
1919

2020
class LogLevelTest extends TestCase
2121
{
22-
/**
23-
* @dataProvider dataProvider
24-
*/
25-
public function testLevel(string $level)
22+
public function testLevelMethodReturnsCorrectValue()
2623
{
27-
if ('not_registered_level' === $level) {
28-
$this->expectException(\RuntimeException::class);
29-
}
30-
31-
$levelObj = LogLevel::getLevelFromString($level);
24+
$this->assertEquals(PsrLogLevel::DEBUG, LogLevel::DEBUG->level());
25+
$this->assertEquals(PsrLogLevel::CRITICAL, LogLevel::CRITICAL->level());
26+
$this->assertEquals(PsrLogLevel::EMERGENCY, LogLevel::EMERGENCY->level());
27+
$this->assertEquals(PsrLogLevel::ALERT, LogLevel::ALERT->level());
28+
$this->assertEquals(PsrLogLevel::ERROR, LogLevel::ERROR->level());
29+
$this->assertEquals(PsrLogLevel::INFO, LogLevel::INFO->level());
30+
$this->assertEquals(PsrLogLevel::WARNING, LogLevel::WARNING->level());
31+
$this->assertEquals(PsrLogLevel::NOTICE, LogLevel::NOTICE->level());
32+
}
3233

33-
$this->assertEquals($level, $levelObj->level());
34+
public function testGetLevelFromStringReturnsCorrectValue()
35+
{
36+
$this->assertEquals(LogLevel::DEBUG, LogLevel::getLevelFromString(PsrLogLevel::DEBUG));
37+
$this->assertEquals(LogLevel::CRITICAL, LogLevel::getLevelFromString(PsrLogLevel::CRITICAL));
38+
$this->assertEquals(LogLevel::EMERGENCY, LogLevel::getLevelFromString(PsrLogLevel::EMERGENCY));
39+
$this->assertEquals(LogLevel::ALERT, LogLevel::getLevelFromString(PsrLogLevel::ALERT));
40+
$this->assertEquals(LogLevel::ERROR, LogLevel::getLevelFromString(PsrLogLevel::ERROR));
41+
$this->assertEquals(LogLevel::INFO, LogLevel::getLevelFromString(PsrLogLevel::INFO));
42+
$this->assertEquals(LogLevel::WARNING, LogLevel::getLevelFromString(PsrLogLevel::WARNING));
43+
$this->assertEquals(LogLevel::NOTICE, LogLevel::getLevelFromString(PsrLogLevel::NOTICE));
3444
}
3545

36-
public function dataProvider()
46+
public function testGetLevelFromStringThrowsExceptionForInvalidValue()
3747
{
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-
];
48+
$this->expectException(\RuntimeException::class);
49+
LogLevel::getLevelFromString('invalid_value');
4950
}
5051
}

0 commit comments

Comments
 (0)