Completed
Push — master ( 8a6725...9de629 )
by Michał
02:24
created

DefaultMessageTest::getArrayMessageContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
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()
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()
18
    {
19
        return <<<EOT
20
Some log message
21
-----------------------------------------------------------
22
23
EOT;
24
    }
25
26
    public function testSimpleMessageWithArray()
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()
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()
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()
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()
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()
95
    {
96
        return <<<EOT
97
Some log message with some value
98
-----------------------------------------------------------
99
100
EOT;
101
    }
102
103
    /**
104
     * @expectedException \InvalidArgumentException
105
     * @expectedExceptionMessage Incorrect message type. Must be string, array or object with __toString method.
106
     */
107
    public function testMessageWithError()
108
    {
109
        (new DefaultMessage)->createMessage(32432, [])->getMessage();
110
    }
111
}
112