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