DefaultMessageTest::getArrayMessageContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SimpleLog\Test;
4
5
use PHPUnit\Framework\TestCase;
6
use SimpleLog\Message\DefaultMessage;
7
8
class DefaultMessageTest extends TestCase
9
{
10
    public function testSimpleMessage(): void
11
    {
12
        $message = (new DefaultMessage())->createMessage('Some log message', [])->getMessage();
13
14
        $this->assertEquals($this->getSampleContent(), \substr($message, strpos($message, "\n") + 1));
15
    }
16
17
    protected function getSampleContent(): string
18
    {
19
        return <<<EOT
20
Some log message
21
-----------------------------------------------------------
22
23
EOT;
24
    }
25
26
    public function testSimpleMessageWithArray(): void
27
    {
28
        $content = [
29
            'message key' => 'some message',
30
            'another key' => 'some another message',
31
            'no key message',
32
        ];
33
        $message = (new DefaultMessage())->createMessage($content, [])->getMessage();
34
35
        //because of different time and date of creating log file, we remove first line with date
36
        $this->assertEquals($this->getArrayMessageContent(), \substr($message, \strpos($message, "\n") + 1));
37
    }
38
39
    protected function getArrayMessageContent(): string
40
    {
41
        return <<<EOT
42
- message key: some message
43
- another key: some another message
44
- no key message
45
-----------------------------------------------------------
46
47
EOT;
48
    }
49
50
    /**
51
     * simple create log object and create log message from array with sub arrays data in given directory
52
     */
53
    public function testCreateLogWithSubArrayMessage(): void
54
    {
55
        $content = [
56
            'sub array' => [
57
                'key' => 'val',
58
                'key 2' => 'val 2',
59
            ],
60
        ];
61
        $message = (new DefaultMessage())->createMessage($content, [])->getMessage();
62
63
        //because of different time and date of creating log file, we remove first line with date
64
        //hack with remove new lines because of differences between output and stored expectation
65
        $this->assertEquals(
66
            \str_replace("\n", '', $this->getSubArrayMessageContent()),
67
            \str_replace("\n", '', \substr($message, \strpos($message, "\n") + 1))
68
        );
69
    }
70
71
    protected function getSubArrayMessageContent(): string
72
    {
73
        return <<<EOT
74
- sub array:
75
    - key: val
76
    - key 2: val 2
77
-----------------------------------------------------------
78
79
EOT;
80
    }
81
82
    public function testMessageWithContext(): void
83
    {
84
        $context = ['context' => 'some value'];
85
        $message = (new DefaultMessage())->createMessage('Some log message with {context}', $context)->getMessage();
86
87
        //because of different time and date of creating log file, we remove first line with date
88
        $this->assertEquals(
89
            $this->getSampleContentWithContext(),
90
            \substr($message, \strpos($message, "\n") + 1)
91
        );
92
    }
93
94
    protected function getSampleContentWithContext(): string
95
    {
96
        return <<<EOT
97
Some log message with some value
98
-----------------------------------------------------------
99
100
EOT;
101
    }
102
103
    public function testMessageWithError(): void
104
    {
105
        if (\PHP_VERSION < '8.0.0') {
106
            $this->expectExceptionMessage(
107
                'Incorrect message type. Must be string, array or object with __toString method.'
108
            );
109
            $this->expectException(\Psr\Log\InvalidArgumentException::class);
110
        } else {
111
            $this->expectExceptionMessage(
112
                'method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given'
113
            );
114
            $this->expectException(\TypeError::class);
115
        }
116
117
        (new DefaultMessage())->createMessage(32432, [])->getMessage();
118
    }
119
}
120