JsonFormatterFactoryTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasMonolog\Factory\Formatter;
6
7
use Arp\LaminasFactory\FactoryInterface;
8
use Arp\LaminasMonolog\Factory\Formatter\JsonFormatterFactory;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Monolog\Formatter\JsonFormatter;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * @covers \Arp\LaminasMonolog\Factory\Formatter\JsonFormatterFactory
18
 * @covers \Arp\LaminasMonolog\Factory\Formatter\AbstractNormalizerFormatterFactory
19
 */
20
final class JsonFormatterFactoryTest extends TestCase
21
{
22
    /**
23
     * @var ContainerInterface&MockObject
24
     */
25
    private ContainerInterface $container;
26
27
    public function setUp(): void
28
    {
29
        $this->container = $this->createMock(ContainerInterface::class);
30
    }
31
32
    public function testImplementsFactoryInterface(): void
33
    {
34
        $factory = new JsonFormatterFactory();
35
36
        $this->assertInstanceOf(FactoryInterface::class, $factory);
37
    }
38
39
    /**
40
     * @dataProvider getInvokeReturnsConfiguredJsonFormatterInstanceData
41
     *
42
     * @param array<mixed> $options
43
     *
44
     * @throws ServiceNotCreatedException
45
     * @throws ContainerExceptionInterface
46
     */
47
    public function testInvokeReturnsConfiguredJsonFormatterInstance(array $options): void
48
    {
49
        $factory = new JsonFormatterFactory();
50
51
        $formatter = $factory($this->container, JsonFormatter::class, $options);
52
53
        $this->assertSame(
54
            $options['batch_mode'] ?? JsonFormatter::BATCH_MODE_JSON,
55
            $formatter->getBatchMode()
56
        );
57
58
        $this->assertSame(
59
            $options['append_new_line'] ?? true,
60
            $formatter->isAppendingNewlines()
61
        );
62
63
        if (isset($options['date_format'])) {
64
            $this->assertSame($options['date_format'], $formatter->getDateFormat());
65
        }
66
67
        if (isset($options['max_normalize_depth'])) {
68
            $this->assertSame($options['max_normalize_depth'], $formatter->getMaxNormalizeDepth());
69
        }
70
71
        $this->assertInstanceOf(JsonFormatter::class, $formatter);
72
    }
73
74
    /**
75
     * @return array<mixed>
76
     */
77
    public function getInvokeReturnsConfiguredJsonFormatterInstanceData(): array
78
    {
79
        return [
80
            [
81
                [],
82
            ],
83
            [
84
                [
85
                    'batch_mode' => JsonFormatter::BATCH_MODE_NEWLINES,
86
                ]
87
            ],
88
            [
89
                [
90
                    'append_new_line' => false,
91
                    'ignore_empty_context_and_extra' => true,
92
                    'include_stack_traces' => true,
93
                ]
94
            ],
95
            [
96
                [
97
                    'date_format' => 'Y-m-d H:i:s',
98
                ]
99
            ],
100
            [
101
                [
102
                    'max_normalize_depth' => 1234,
103
                ]
104
            ]
105
        ];
106
    }
107
}
108