|
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 Gelf\MessageInterface; |
|
16
|
|
|
use Gelf\MessageValidator; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use RuntimeException; |
|
19
|
|
|
|
|
20
|
|
|
class MessageValidatorTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
private MessageValidator $messageValidator; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->messageValidator = new MessageValidator(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @dataProvider versions |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testValid(string $version): void |
|
33
|
|
|
{ |
|
34
|
|
|
$msg = $this->getMessage("lorem", "example.local", $version); |
|
35
|
|
|
self::assertTrue($this->messageValidator->validate($msg, $reason)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @dataProvider versions |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testMissingShortMessage(string $version): void |
|
42
|
|
|
{ |
|
43
|
|
|
$msg = $this->getMessage(null, "example.local", $version); |
|
44
|
|
|
self::assertFalse($this->messageValidator->validate($msg, $reason)); |
|
45
|
|
|
self::assertStringContainsString('short-message', $reason); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @dataProvider versions |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testInvalidAddtionalFieldID(string $version): void |
|
52
|
|
|
{ |
|
53
|
|
|
$msg = $this->getMessage( |
|
54
|
|
|
"lorem ipsum", |
|
55
|
|
|
"example.local", |
|
56
|
|
|
$version, |
|
57
|
|
|
['id' => 1] |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
self::assertFalse($this->messageValidator->validate($msg, $reason)); |
|
61
|
|
|
self::assertStringContainsString('id', $reason); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testInvalidAddtionalKeyV11(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$msg = $this->getMessage( |
|
67
|
|
|
"lorem", |
|
68
|
|
|
"example.local", |
|
69
|
|
|
"1.1", |
|
70
|
|
|
['foo?' => 1] |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
self::assertFalse($this->messageValidator->validate($msg, $reason)); |
|
74
|
|
|
self::assertStringContainsString('additional', $reason); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function getMessage( |
|
78
|
|
|
?string $shortMessage = "lorem ipsum", |
|
79
|
|
|
?string $host = "example.local", |
|
80
|
|
|
?string $version = "1.0", |
|
81
|
|
|
array $additionals = [] |
|
82
|
|
|
): MessageInterface { |
|
83
|
|
|
$msg = $this->createMock(MessageInterface::class); |
|
84
|
|
|
$msg->expects($this->any())->method('getHost') |
|
85
|
|
|
->will($this->returnValue($host)); |
|
86
|
|
|
$msg->expects($this->any())->method('getVersion') |
|
87
|
|
|
->will($this->returnValue($version)); |
|
88
|
|
|
$msg->expects($this->any())->method('getShortMessage') |
|
89
|
|
|
->will($this->returnValue($shortMessage)); |
|
90
|
|
|
|
|
91
|
|
|
$msg->expects($this->any())->method('getAllAdditionals') |
|
92
|
|
|
->will($this->returnValue($additionals)); |
|
93
|
|
|
|
|
94
|
|
|
$msg->expects($this->any())->method('hasAdditional') |
|
95
|
|
|
->will($this->returnCallback( |
|
96
|
|
|
function ($key) use ($additionals) { |
|
97
|
|
|
return isset($additionals[$key]); |
|
98
|
|
|
} |
|
99
|
|
|
)); |
|
100
|
|
|
|
|
101
|
|
|
return $msg; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function versions(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return [['1.0'], ['1.1']]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|