Passed
Pull Request — master (#3)
by Alex
02:52 queued 01:07
created

testInvokeWillConfigureFormatter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 29
rs 9.7333
cc 2
nc 2
nop 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\Formatter\FormatterInterface;
11
use Monolog\Handler\HandlerInterface;
12
use Monolog\Handler\PsrHandler;
13
use Monolog\Logger;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
use Psr\Container\ContainerExceptionInterface;
17
use Psr\Container\ContainerInterface;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * @covers \Arp\LaminasMonolog\Factory\Handler\PsrHandlerFactory
22
 * @covers \Arp\LaminasMonolog\Factory\FactoryLoggerProviderTrait
23
 * @covers \Arp\LaminasMonolog\Factory\FactoryFormatterProviderTrait
24
 */
25
final class PsrHandlerFactoryTest extends TestCase
26
{
27
    /**
28
     * @var ContainerInterface&MockObject
29
     */
30
    private ContainerInterface $container;
31
32
    public function setUp(): void
33
    {
34
        $this->container = $this->createMock(ContainerInterface::class);
35
    }
36
37
    /**
38
     * Assert that the factory implements FactoryInterface
39
     */
40
    public function testImplementsFactoryInterface(): void
41
    {
42
        $factory = new PsrHandlerFactory();
43
44
        $this->assertInstanceOf(FactoryInterface::class, $factory);
45
    }
46
47
    /**
48
     * @throws ContainerExceptionInterface
49
     */
50
    public function testServiceNotCreatedExceptionIsThrownIfNotLoggerOptionIsProvided(): void
51
    {
52
        $factory = new PsrHandlerFactory();
53
54
        $requestedName = 'LoggerServiceName';
55
56
        $this->expectException(ServiceNotCreatedException::class);
57
        $this->expectExceptionMessage(
58
            sprintf('The required \'logger\' configuration option is missing for service \'%s\'', $requestedName)
59
        );
60
61
        $factory($this->container, $requestedName, []);
62
    }
63
64
    /**
65
     * @dataProvider getInvokeWillReturnConfiguredPsrHandlerInstanceData
66
     *
67
     * @param array<mixed> $options
68
     *
69
     * @throws ServiceNotCreatedException
70
     * @throws ContainerExceptionInterface
71
     */
72
    public function testInvokeWillReturnConfiguredPsrHandlerInstance(array $options): void
73
    {
74
        $factory = new PsrHandlerFactory();
75
76
        $handler = $factory($this->container, PsrHandler::class, $options);
77
78
        $this->assertInstanceOf(PsrHandler::class, $handler);
79
    }
80
81
    /**
82
     * @return array<mixed>
83
     */
84
    public function getInvokeWillReturnConfiguredPsrHandlerInstanceData(): array
85
    {
86
        return [
87
            [
88
                [
89
                    'logger' => $this->createMock(LoggerInterface::class),
90
                ]
91
            ],
92
            [
93
                [
94
                    'logger' => $this->createMock(LoggerInterface::class),
95
                    'level' => Logger::CRITICAL,
96
                ]
97
            ],
98
            [
99
                [
100
                    'logger' => $this->createMock(LoggerInterface::class),
101
                    'handler' => $this->createMock(HandlerInterface::class),
102
                ]
103
            ],
104
        ];
105
    }
106
107
    /**
108
     * @dataProvider getInvokeWillConfigureFormatterData
109
     *
110
     * @param array<string, FormatterInterface|string> $options
111
     *
112
     * @throws ContainerExceptionInterface
113
     * @throws ServiceNotCreatedException
114
     */
115
    public function testInvokeWillConfigureFormatter(array $options): void
116
    {
117
        $options = array_merge(
118
            [
119
                'logger' => $this->createMock(LoggerInterface::class),
120
            ],
121
            $options,
122
        );
123
124
        $factory = new PsrHandlerFactory();
125
126
        if (is_string($options['formatter'])) {
127
            $this->container->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Container\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
            $this->container->/** @scrutinizer ignore-call */ 
128
                              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
                ->method('has')
129
                ->with($options['formatter'])
130
                ->willReturn(true);
131
132
            /** @var FormatterInterface&MockObject $formatter */
133
            $formatter = $this->createMock(FormatterInterface::class);
134
135
            $this->container->expects($this->once())
136
                ->method('get')
137
                ->with($options['formatter'])
138
                ->willReturn($formatter);
139
        }
140
141
        $handler = $factory($this->container, PsrHandler::class, $options);
142
143
        $this->assertInstanceOf(PsrHandler::class, $handler);
144
    }
145
146
    /**
147
     * @return array<int, array<int, array<string, string|FormatterInterface>>>
148
     */
149
    public function getInvokeWillConfigureFormatterData(): array
150
    {
151
        return [
152
            [
153
                [
154
                    'formatter' => $this->createMock(FormatterInterface::class),
155
                ],
156
            ],
157
            [
158
                [
159
                    'formatter' => 'FooFormatter',
160
                ],
161
            ]
162
        ];
163
    }
164
}
165