MessageTest::testSetTimestamp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the php-gelf package.
6
 *
7
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Gelf\Test;
14
15
use DateTime;
16
use Gelf\Message;
17
use PHPUnit\Framework\TestCase;
18
use Psr\Log\LogLevel;
19
use RuntimeException;
20
21
class MessageTest extends TestCase
22
{
23
    private Message $message;
24
25
    public function setUp(): void
26
    {
27
        $this->message = new Message();
28
    }
29
30
    public function testTimestamp(): void
31
    {
32
        self::assertLessThanOrEqual(microtime(true), $this->message->getTimestamp());
33
        self::assertGreaterThan(0, $this->message->getTimestamp());
34
35
        $this->message->setTimestamp(123);
36
        self::assertEquals(123, $this->message->getTimestamp());
37
    }
38
39
    public function testVersion(): void
40
    {
41
        self::assertEquals("1.1", $this->message->getVersion());
42
    }
43
44
    public function testHost(): void
45
    {
46
        // default is current hostname
47
        self::assertEquals(gethostname(), $this->message->getHost());
48
49
        $this->message->setHost("example.local");
50
        self::assertEquals("example.local", $this->message->getHost());
51
    }
52
53
    public function testLevel(): void
54
    {
55
        self::assertEquals(1, $this->message->getSyslogLevel());
56
        self::assertEquals(LogLevel::ALERT, $this->message->getLevel());
57
58
        $this->message->setLevel(0);
59
        self::assertEquals(0, $this->message->getSyslogLevel());
60
        self::assertEquals(LogLevel::EMERGENCY, $this->message->getLevel());
61
62
        $this->message->setLevel(LogLevel::EMERGENCY);
63
        self::assertEquals(0, $this->message->getSyslogLevel());
64
        self::assertEquals(LogLevel::EMERGENCY, $this->message->getLevel());
65
    }
66
67
    public function testLevelInvalidString(): void
68
    {
69
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

69
        self::/** @scrutinizer ignore-call */ 
70
              expectException(RuntimeException::class);
Loading history...
70
        $this->message->setLevel("invalid");
71
    }
72
73
    public function testLevelInvalidInteger()
74
    {
75
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

75
        self::/** @scrutinizer ignore-call */ 
76
              expectException(RuntimeException::class);
Loading history...
76
        $this->message->setLevel(8);
77
    }
78
79
    public function testLogLevelToPsr(): void
80
    {
81
        self::assertEquals(LogLevel::ALERT, Message::logLevelToPsr("alert"));
82
        self::assertEquals(LogLevel::ALERT, Message::logLevelToPsr("ALERT"));
83
        self::assertEquals(LogLevel::ALERT, Message::logLevelToPsr(1));
84
    }
85
86
    public function testLogLevelToPsrInvalidString(): void
87
    {
88
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

88
        self::/** @scrutinizer ignore-call */ 
89
              expectException(RuntimeException::class);
Loading history...
89
        Message::logLevelToPsr("invalid");
90
    }
91
92
    public function testLogLevelToPsrInvalidInt(): void
93
    {
94
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

94
        self::/** @scrutinizer ignore-call */ 
95
              expectException(RuntimeException::class);
Loading history...
95
        Message::logLevelToPsr(-1);
96
    }
97
98
    public function testOptionalMessageFields(): void
99
    {
100
        $fields = [
101
            "FullMessage" => 'full message',
102
            "ShortMessage" => 'short message'
103
        ];
104
105
        foreach ($fields as $field => $value) {
106
            $g = "get$field";
107
            $s = "set$field";
108
            self::assertEmpty($this->message->$g());
109
110
            $this->message->$s($value);
111
            self::assertEquals($value, $this->message->$g());
112
        }
113
    }
114
115
    public function testAdditionals(): void
116
    {
117
        self::assertCount(0, $this->message->getAllAdditionals());
118
119
        self::assertFalse($this->message->hasAdditional("foo"));
120
        $this->message->setAdditional("foo", "bar");
121
        self::assertEquals("bar", $this->message->getAdditional("foo"));
122
        self::assertTrue($this->message->hasAdditional("foo"));
123
        self::assertCount(1, $this->message->getAllAdditionals());
124
125
        $this->message->setAdditional("foo", "buk");
126
        self::assertEquals("buk", $this->message->getAdditional("foo"));
127
        self::assertCount(1, $this->message->getAllAdditionals());
128
129
        self::assertEquals(
130
            ["foo" => "buk"],
131
            $this->message->getAllAdditionals()
132
        );
133
    }
134
    
135
    public function testSetAdditionalEmptyKey(): void
136
    {
137
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

137
        self::/** @scrutinizer ignore-call */ 
138
              expectException(RuntimeException::class);
Loading history...
138
        $this->message->setAdditional("", "test");
139
    }
140
141
    public function testGetAdditionalInvalidKey(): void
142
    {
143
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

143
        self::/** @scrutinizer ignore-call */ 
144
              expectException(RuntimeException::class);
Loading history...
144
        $this->message->getAdditional("invalid");
145
    }
146
147
    public function testSetTimestamp(): void
148
    {
149
        $dt = new DateTime('@1393661544.3012');
150
        $this->message->setTimestamp($dt);
151
152
        self::assertEquals($dt->format("U.u"), $this->message->getTimestamp());
153
    }
154
155
    public function testMethodChaining(): void
156
    {
157
        $message = $this->message
158
            ->setTimestamp(new DateTime())
159
            ->setAdditional("test", "value")
160
            ->setHost("test")
161
            ->setFullMessage("testtest")
162
            ->setShortMessage("test")
163
            ->setLevel("ERROR")
164
            ->setVersion("1.1")
165
        ;
166
167
        self::assertEquals($this->message, $message);
168
    }
169
170
    public function testToArrayWithArrayData(): void
171
    {
172
        $this->message->setAdditional("foo", ["foo" => "bar"]);
173
        $data = $this->message->toArray();
174
175
        $map = [
176
            "version"       => "getVersion",
177
            "host"          => "getHost",
178
            "timestamp"     => "getTimestamp",
179
            "full_message"  => "getFullMessage",
180
            "short_message" => "getShortMessage",
181
            "level"         => "getSyslogLevel"
182
        ];
183
184
        foreach ($map as $k => $method) {
185
            $r = $this->message->$method();
186
            if (empty($r)) {
187
                $error = sprintf(
188
                    "When method %s returns an empty value, " .
189
                    "%s should not be in array",
190
                    $method,
191
                    $k
192
                );
193
                self::assertArrayNotHasKey($k, $data, $error);
194
            } else {
195
                self::assertEquals($data[$k], $this->message->$method());
196
            }
197
        }
198
    }
199
200
    public function testToArrayV11(): void
201
    {
202
        $this->message->setVersion("1.1");
203
        $this->message->setShortMessage("lorem ipsum");
204
        $this->message->setAdditional("foo", "bar");
205
        $this->message->setAdditional("bool-true", true);
206
        $this->message->setAdditional("bool-false", false);
207
        $this->message->setAdditional("int-zero", 0);
208
209
        $data = $this->message->toArray();
210
211
        self::assertSame('1.1', $data['version']);
212
        self::assertSame('lorem ipsum', $data['short_message']);
213
214
        self::assertArrayHasKey('_foo', $data);
215
        self::assertSame('bar', $data['_foo']);
216
        self::assertArrayHasKey("_bool-true", $data);
217
        self::assertTrue($data["_bool-true"]);
218
        self::assertArrayHasKey("_bool-false", $data);
219
        self::assertFalse($data["_bool-false"]);
220
        self::assertArrayHasKey("_int-zero", $data);
221
        self::assertEquals(0, $data["_int-zero"]);
222
    }
223
}
224