summaryrefslogtreecommitdiff
path: root/tests/Unit/Logger/EngelsystemLoggerTest.php
blob: 3b67aa29335485a05f799d6e6ff3de15356ab5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

namespace Engelsystem\Test\Unit\Logger;

use Engelsystem\Logger\EngelsystemLogger;
use Engelsystem\Models\LogEntry;
use Engelsystem\Test\Unit\ServiceProviderTest;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LogLevel;

class EngelsystemLoggerTest extends ServiceProviderTest
{
    /**
     * @covers \Engelsystem\Logger\EngelsystemLogger::__construct
     * @covers \Engelsystem\Logger\EngelsystemLogger::log
     */
    public function testLog()
    {
        /** @var LogEntry|MockObject $logEntry */
        $logEntry = $this->getMockBuilder(LogEntry::class)
            ->addMethods(['create'])
            ->getMock();
        $logEntry->expects($this->once())
            ->method('create')
            ->with(['level' => LogLevel::INFO, 'message' => 'I\'m an information!']);

        $logger = new EngelsystemLogger($logEntry);

        $logger->log(LogLevel::INFO, 'I\'m an information!');
    }

    /**
     * @covers \Engelsystem\Logger\EngelsystemLogger::log
     * @covers \Engelsystem\Logger\EngelsystemLogger::checkLevel
     */
    public function testCheckLevel()
    {
        /** @var LogEntry|MockObject $logEntry */
        $logEntry = $this->createMock(LogEntry::class);
        $logger = new EngelsystemLogger($logEntry);

        $this->expectException(InvalidArgumentException::class);
        $logger->log('FooBar', 'Random Stuff');
    }

    /**
     * @covers \Engelsystem\Logger\EngelsystemLogger::interpolate
     */
    public function testInterpolate()
    {
        /** @var LogEntry|MockObject $logEntry */
        $logEntry = $this->getMockBuilder(LogEntry::class)
            ->addMethods(['create'])
            ->getMock();
        $logEntry->expects($this->exactly(3))
            ->method('create')
            ->withConsecutive(
                [['level' => LogLevel::DEBUG, 'message' => 'User: Foo']],
                [['level' => LogLevel::NOTICE, 'message' => 'User: {user}']],
                [['level' => LogLevel::NOTICE, 'message' => 'User: Bar']]
            );

        $logger = new EngelsystemLogger($logEntry);

        $logger->log(LogLevel::DEBUG, 'User: {user}', ['user' => 'Foo']);
        $logger->log(LogLevel::NOTICE, 'User: {user}', ['user' => ['name' => 'Lorem']]);
        $logger->log(LogLevel::NOTICE, 'User: {user}', [
            'user' =>
                new class
                {
                    public function __toString() { return 'Bar'; }
                }
        ]);
    }
}