Passed
Pull Request — master (#1)
by Alex
02:48
created

PsrHandlerFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 74
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testServiceNotCreatedExceptionIsThrownIfNotLoggerOptionIsProvided() 0 12 1
A getInvokeWillReturnConfiguredPsrHandlerInstanceData() 0 12 1
A testImplementsFactoryInterface() 0 5 1
A testInvokeWillReturnConfiguredPsrHandlerInstance() 0 7 1
A setUp() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasMonolog\Factory\Handler;
6
7
use Arp\LaminasFactory\FactoryInterface;
8
use Arp\LaminasMonolog\Factory\Handler\PsrHandlerFactory;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Monolog\Handler\PsrHandler;
11
use Monolog\Logger;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\ContainerInterface;
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * @covers \Arp\LaminasMonolog\Factory\Handler\PsrHandlerFactory
20
 * @covers \Arp\LaminasMonolog\Factory\FactoryLoggerProviderTrait
21
 * @covers \Arp\LaminasMonolog\Factory\FactoryFormatterProviderTrait
22
 *
23
 * @author  Alex Patterson <[email protected]>
24
 * @package ArpTest\LaminasMonolog\Factory\Handler
25
 */
26
final class PsrHandlerFactoryTest extends TestCase
27
{
28
    /**
29
     * @var ContainerInterface&MockObject
30
     */
31
    private ContainerInterface $container;
32
33
    /**
34
     * Prepare the test case dependencies
35
     */
36
    public function setUp(): void
37
    {
38
        $this->container = $this->createMock(ContainerInterface::class);
39
    }
40
41
    /**
42
     * Assert that the factory implements FactoryInterface
43
     */
44
    public function testImplementsFactoryInterface(): void
45
    {
46
        $factory = new PsrHandlerFactory();
47
48
        $this->assertInstanceOf(FactoryInterface::class, $factory);
49
    }
50
51
    /**
52
     * @throws ContainerExceptionInterface
53
     */
54
    public function testServiceNotCreatedExceptionIsThrownIfNotLoggerOptionIsProvided(): void
55
    {
56
        $factory = new PsrHandlerFactory();
57
58
        $requestedName = 'LoggerServiceName';
59
60
        $this->expectException(ServiceNotCreatedException::class);
61
        $this->expectExceptionMessage(
62
            sprintf('The required \'logger\' configuration option is missing for service \'%s\'', $requestedName)
63
        );
64
65
        $factory($this->container, $requestedName, []);
66
    }
67
68
    /**
69
     * @dataProvider getInvokeWillReturnConfiguredPsrHandlerInstanceData
70
     *
71
     * @param array<mixed> $options
72
     *
73
     * @throws ServiceNotCreatedException
74
     * @throws ContainerExceptionInterface
75
     */
76
    public function testInvokeWillReturnConfiguredPsrHandlerInstance(array $options): void
77
    {
78
        $factory = new PsrHandlerFactory();
79
80
        $handler = $factory($this->container, PsrHandler::class, $options);
81
82
        $this->assertInstanceOf(PsrHandler::class, $handler);
83
    }
84
85
    /**
86
     * @return array<mixed>
87
     */
88
    public function getInvokeWillReturnConfiguredPsrHandlerInstanceData(): array
89
    {
90
        return [
91
            [
92
                [
93
                    'logger' => $this->createMock(LoggerInterface::class),
94
                ]
95
            ],
96
            [
97
                [
98
                    'logger' => $this->createMock(LoggerInterface::class),
99
                    'level' => Logger::CRITICAL,
100
                ]
101
            ]
102
        ];
103
    }
104
}
105