[!!!][FEATURE] Introduce PSR-3 Logging
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / LogRecordTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Log;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Log\LogLevel;
19 use TYPO3\CMS\Core\Log\LogRecord;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class LogRecordTest extends UnitTestCase
26 {
27 /**
28 * Returns a LogRecord
29 *
30 * @param array $parameters Parameters to set in LogRecord constructor.
31 * @return LogRecord
32 */
33 protected function getRecord(array $parameters = []): LogRecord
34 {
35 $record = new LogRecord(
36 $parameters['component'] ?? 'test.core.log',
37 $parameters['level'] ?? LogLevel::DEBUG,
38 $parameters['message'] ?? 'test message',
39 $parameters['data'] ?? []
40 );
41 return $record;
42 }
43
44 /**
45 * @test
46 */
47 public function constructorSetsCorrectComponent()
48 {
49 $component = 'test.core.log';
50 $record = $this->getRecord(['component' => $component]);
51 $this->assertEquals($component, $record->getComponent());
52 }
53
54 /**
55 * @test
56 */
57 public function constructorSetsCorrectLogLevel()
58 {
59 $logLevel = LogLevel::CRITICAL;
60 $record = $this->getRecord(['level' => $logLevel]);
61 $this->assertEquals($logLevel, $record->getLevel());
62 }
63
64 /**
65 * @test
66 */
67 public function constructorSetsCorrectMessage()
68 {
69 $logMessage = 'test message';
70 $record = $this->getRecord(['message' => $logMessage]);
71 $this->assertEquals($logMessage, $record->getMessage());
72 }
73
74 /**
75 * @test
76 */
77 public function constructorSetsCorrectData()
78 {
79 $dataArray = [
80 'foo' => 'bar'
81 ];
82 $record = $this->getRecord(['data' => $dataArray]);
83 $this->assertEquals($dataArray, $record->getData());
84 }
85
86 /**
87 * @test
88 */
89 public function setComponentSetsComponent()
90 {
91 $record = $this->getRecord();
92 $component = 'testcomponent';
93 $this->assertEquals($component, $record->setComponent($component)->getComponent());
94 }
95
96 /**
97 * @test
98 */
99 public function setLevelSetsLevel()
100 {
101 $record = $this->getRecord();
102 $level = LogLevel::EMERGENCY;
103 $this->assertEquals($level, $record->setLevel($level)->getLevel());
104 }
105
106 /**
107 * @test
108 */
109 public function setLevelValidatesLevel()
110 {
111 $this->expectException(\Psr\Log\InvalidArgumentException::class);
112 $this->expectExceptionCode(1550247164);
113
114 $record = $this->getRecord();
115 $record->setLevel('foo');
116 }
117
118 /**
119 * @test
120 */
121 public function setMessageSetsMessage()
122 {
123 $record = $this->getRecord();
124 $message = 'testmessage';
125 $this->assertEquals($message, $record->setMessage($message)->getMessage());
126 }
127
128 /**
129 * @test
130 */
131 public function setCreatedSetsCreated()
132 {
133 $record = $this->getRecord();
134 $created = 123.45;
135 $this->assertEquals($created, $record->setCreated($created)->getCreated());
136 }
137
138 /**
139 * @test
140 */
141 public function setRequestIdSetsRequestId()
142 {
143 $record = $this->getRecord();
144 $requestId = 'testrequestid';
145 $this->assertEquals($requestId, $record->setRequestId($requestId)->getRequestId());
146 }
147
148 /**
149 * @test
150 */
151 public function toArrayReturnsCorrectValues()
152 {
153 $component = 'test.core.log';
154 $level = LogLevel::DEBUG;
155 $message = 'test message';
156 $data = ['foo' => 'bar'];
157 /** @var $record LogRecord */
158 $record = new LogRecord($component, $level, $message, $data);
159 $recordArray = $record->toArray();
160 $this->assertEquals($component, $recordArray['component']);
161 $this->assertEquals($level, $recordArray['level']);
162 $this->assertEquals($message, $recordArray['message']);
163 $this->assertEquals($data, $recordArray['data']);
164 }
165
166 /**
167 * @test
168 */
169 public function toStringIncludesDataAsJson()
170 {
171 $dataArray = ['foo' => 'bar'];
172 $record = $this->getRecord(['data' => $dataArray]);
173 $this->assertStringContainsString(json_encode($dataArray), (string)$record);
174 }
175
176 /**
177 * @test
178 */
179 public function toStringIncludesExceptionDataAsJson()
180 {
181 $dataArray = ['exception' => new \Exception('foo', 1476049451)];
182 $record = $this->getRecord(['data' => $dataArray]);
183 $this->assertStringContainsString('Exception: foo', (string)$record);
184 }
185 }