Skip to content

Commit ed15be3

Browse files
committed
Create Unit tests
1 parent 4aa86a5 commit ed15be3

23 files changed

+816
-12
lines changed

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
"Micro\\Plugin\\Http\\": "src/"
2727
}
2828
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Micro\\Plugin\\Http\\Test\\": "tests/"
32+
}
33+
},
2934
"config": {
3035
"allow-plugins": {
3136
"ergebnis/composer-normalize": true
@@ -34,8 +39,8 @@
3439
},
3540
"scripts": {
3641
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text",
37-
"php-cs-fix": "./vendor/bin/php-cs-fixer fix --verbose --using-cache=no",
38-
"php-cs-try": "./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
42+
"php-cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --using-cache=no",
43+
"php-cs-try": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
3944
"phpstan": "./vendor/bin/phpstan analyze --no-progress",
4045
"phpunit": "./vendor/bin/phpunit",
4146
"psalm": "./vendor/bin/psalm --no-progress --show-info=true",

psalm.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818

1919
<UnnecessaryVarAnnotation>
2020
<errorLevel type="suppress">
21-
<file name="src/PluginHttpLogger.php"/>
21+
<file name="src/HttpLoggerPlugin.php"/>
2222
</errorLevel>
2323
</UnnecessaryVarAnnotation>
2424

2525
<MissingConstructor>
2626
<errorLevel type="suppress">
27-
<file name="src/PluginHttpLogger.php"/>
27+
<file name="src/HttpLoggerPlugin.php"/>
2828
</errorLevel>
2929
</MissingConstructor>
3030

3131
<ImplementedReturnTypeMismatch>
3232
<errorLevel type="suppress">
33-
<file name="src/PluginHttpLogger.php"/>
33+
<file name="src/HttpLoggerPlugin.php"/>
3434
</errorLevel>
3535
</ImplementedReturnTypeMismatch>
3636
</issueHandlers>

src/Business/Executor/HttpExecutorLoggerAwareDecoratorFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Micro\Plugin\Http\Business\Executor;
1515

1616
use Micro\Plugin\Http\Business\Logger\Formatter\LogFormatterFactoryInterface;
17-
use Micro\Plugin\Http\PluginHttpLoggerConfigurationInterface;
17+
use Micro\Plugin\Http\HttpLoggerPluginConfigurationInterface;
1818
use Micro\Plugin\Logger\LoggerFacadeInterface;
1919

2020
/**
@@ -26,7 +26,7 @@ public function __construct(
2626
private RouteExecutorInterface $decorated,
2727
private LoggerFacadeInterface $loggerFacade,
2828
private LogFormatterFactoryInterface $logFormatterFactory,
29-
private PluginHttpLoggerConfigurationInterface $configuration
29+
private HttpLoggerPluginConfigurationInterface $configuration
3030
) {
3131
}
3232

src/Decorator/HttpFacadeLoggerDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ public function generateUrlByRouteName(string $routeName, ?array $parameters = [
5555

5656
public function match(Request $request): RouteInterface
5757
{
58-
return $this->match($request);
58+
return $this->decorated->match($request);
5959
}
6060
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
/**
3131
* @author Stanislau Komar <kost@micro-php.net>
3232
*
33-
* @method PluginHttpLoggerConfigurationInterface configuration()
33+
* @method HttpLoggerPluginConfigurationInterface configuration()
3434
*/
35-
class PluginHttpLogger implements DependencyProviderInterface, PluginDependedInterface, ConfigurableInterface
35+
class HttpLoggerPlugin implements DependencyProviderInterface, PluginDependedInterface, ConfigurableInterface
3636
{
3737
use PluginConfigurationTrait;
3838
private HttpFacadeInterface $httpFacade;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @author Stanislau Komar <kost@micro-php.net>
2020
*/
21-
class PluginHttpLoggerConfiguration extends PluginConfiguration implements PluginHttpLoggerConfigurationInterface
21+
class HttpLoggerPluginConfiguration extends PluginConfiguration implements HttpLoggerPluginConfigurationInterface
2222
{
2323
public const CFG_LOGGER_ACCESS = 'MICRO_HTTP_LOGGER_ACCESS';
2424
public const CFG_LOGGER_ERROR = 'MICRO_HTTP_LOGGER_ERROR';

src/PluginHttpLoggerConfigurationInterface.php renamed to src/HttpLoggerPluginConfigurationInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Stanislau Komar <kost@micro-php.net>
1818
*/
19-
interface PluginHttpLoggerConfigurationInterface
19+
interface HttpLoggerPluginConfigurationInterface
2020
{
2121
public function getAccessLoggerName(): string|null;
2222

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Micro framework package.
5+
*
6+
* (c) Stanislau Komar <kost@micro-php.net>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Micro\Plugin\Http\Business\Executor;
13+
14+
use Micro\Plugin\Http\Business\Logger\Formatter\LogFormatterFactoryInterface;
15+
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
16+
use Micro\Plugin\Http\HttpLoggerPluginConfigurationInterface;
17+
use Micro\Plugin\Logger\LoggerFacadeInterface;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class HttpExecutorLoggerAwareDecoratorFactoryTest extends TestCase
21+
{
22+
public function testCreate()
23+
{
24+
$factory = new HttpExecutorLoggerAwareDecoratorFactory(
25+
$this->createMock(HttpFacadeInterface::class),
26+
$this->createMock(LoggerFacadeInterface::class),
27+
$this->createMock(LogFormatterFactoryInterface::class),
28+
$this->createMock(HttpLoggerPluginConfigurationInterface::class),
29+
);
30+
31+
$this->assertInstanceOf(RouteExecutorInterface::class, $factory->create());
32+
}
33+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Micro framework package.
5+
*
6+
* (c) Stanislau Komar <kost@micro-php.net>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Micro\Plugin\Http\Business\Executor;
13+
14+
use Micro\Plugin\Http\Business\Logger\Formatter\LogFormatterInterface;
15+
use Micro\Plugin\Http\Exception\HttpException;
16+
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
17+
use PHPUnit\Framework\TestCase;
18+
use Psr\Log\LoggerInterface;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\Response;
21+
22+
class HttpExecutorLoggerAwareDecoratorTest extends TestCase
23+
{
24+
/**
25+
* @dataProvider dataProvider
26+
*/
27+
public function testExecute(bool $isFlush, bool $exceptException)
28+
{
29+
$decorator = new HttpExecutorLoggerAwareDecorator(
30+
$this->createDecorated($exceptException),
31+
$this->createLogger(),
32+
$this->createLogger(),
33+
$this->createFormatter(),
34+
$this->createFormatter()
35+
);
36+
37+
if ($exceptException) {
38+
$this->expectException(HttpException::class);
39+
$this->expectExceptionCode(404);
40+
}
41+
42+
$this->assertInstanceOf(
43+
Response::class,
44+
$decorator->execute(
45+
$this->createMock(Request::class),
46+
$isFlush
47+
)
48+
);
49+
}
50+
51+
public function dataProvider()
52+
{
53+
return [
54+
[
55+
true, true,
56+
],
57+
[
58+
false, true,
59+
],
60+
[
61+
true, false,
62+
],
63+
[
64+
false, false,
65+
],
66+
];
67+
}
68+
69+
protected function createDecorated(bool $throwException)
70+
{
71+
$decorated = $this->createMock(HttpFacadeInterface::class);
72+
$methodMock = $decorated
73+
->expects($this->once())
74+
->method('execute')
75+
;
76+
if ($throwException) {
77+
$methodMock->willThrowException(new HttpException('test', 404));
78+
} else {
79+
$methodMock->willReturn(
80+
$this->createMock(Response::class)
81+
);
82+
}
83+
84+
return $decorated;
85+
}
86+
87+
protected function createLogger()
88+
{
89+
return $this->createMock(LoggerInterface::class);
90+
}
91+
92+
protected function createFormatter()
93+
{
94+
return $this->createMock(LogFormatterInterface::class);
95+
}
96+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\Http\Business\Logger\Formatter\Format;
15+
16+
use Micro\Plugin\Http\Business\Logger\Formatter\LogFormatterInterface;
17+
use Micro\Plugin\Http\Exception\HttpException;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\Response;
21+
22+
/**
23+
* @author Stanislau Komar <kost@micro-php.net>
24+
*/
25+
abstract class AbstractFormatTest extends TestCase
26+
{
27+
/**
28+
* @dataProvider dataProvider
29+
*/
30+
public function testFormat(bool $hasResponse, \Throwable|null $throwable)
31+
{
32+
$object = $this->createTestObject();
33+
34+
$this->assertIsString(
35+
$object->format(
36+
$this->createRequest(),
37+
$this->createResponse($hasResponse),
38+
$this->createThrowable($throwable)
39+
)
40+
);
41+
}
42+
43+
public function dataProvider()
44+
{
45+
return [
46+
[false, null],
47+
[true, null],
48+
[true, new HttpException()],
49+
[false, new HttpException()],
50+
[true, new \Exception()],
51+
[false, new \Exception()],
52+
];
53+
}
54+
55+
public function createThrowable(\Throwable|null $throwable): \Throwable|null
56+
{
57+
return $throwable;
58+
}
59+
60+
protected function createResponse(bool $hasResponse): Response|null
61+
{
62+
if (!$hasResponse) {
63+
return null;
64+
}
65+
66+
return $this->createMock(Response::class);
67+
}
68+
69+
protected function createRequest(): Request
70+
{
71+
return Request::create('/test');
72+
}
73+
74+
abstract protected function getTestClass(): string;
75+
76+
protected function createTestObject(): LogFormatterInterface
77+
{
78+
$testClass = $this->getTestClass();
79+
80+
return new $testClass($this->getFormattedVariable());
81+
}
82+
83+
protected function getFormattedVariable()
84+
{
85+
return '{{'.$this->getVariable().'}}';
86+
}
87+
88+
abstract public function getVariable(): string;
89+
}

0 commit comments

Comments
 (0)