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

testServiceNotCreatedExceptionIsThrownIfNotLoggerOptionIsProvided()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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