‪TYPO3CMS  9.5
LogRecordTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
25 class ‪LogRecordTest extends UnitTestCase
26 {
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 
48  {
49  $component = 'test.core.log';
50  $record = $this->‪getRecord(['component' => $component]);
51  $this->assertEquals($component, $record->getComponent());
52  }
53 
58  {
59  $logLevel = ‪LogLevel::CRITICAL;
60  $record = $this->‪getRecord(['level' => $logLevel]);
61  $this->assertEquals($logLevel, $record->getLevel());
62  }
63 
68  {
69  $logMessage = 'test message';
70  $record = $this->‪getRecord(['message' => $logMessage]);
71  $this->assertEquals($logMessage, $record->getMessage());
72  }
73 
78  {
79  $dataArray = [
80  'foo' => 'bar'
81  ];
82  $record = $this->‪getRecord(['data' => $dataArray]);
83  $this->assertEquals($dataArray, $record->getData());
84  }
85 
89  public function ‪setComponentSetsComponent()
90  {
91  $record = $this->‪getRecord();
92  $component = 'testcomponent';
93  $this->assertEquals($component, $record->setComponent($component)->getComponent());
94  }
95 
99  public function ‪setLevelSetsLevel()
100  {
101  $record = $this->‪getRecord();
102  $level = ‪LogLevel::EMERGENCY;
103  $this->assertEquals($level, $record->setLevel($level)->getLevel());
104  }
105 
109  public function ‪setLevelValidatesLevel()
110  {
111  $this->expectException(\Psr\Log\InvalidArgumentException::class);
112  $this->expectExceptionCode(1321637121);
113 
114  $record = $this->‪getRecord();
115  $record->setLevel(100);
116  }
117 
121  public function ‪setMessageSetsMessage()
122  {
123  $record = $this->‪getRecord();
124  $message = 'testmessage';
125  $this->assertEquals($message, $record->setMessage($message)->getMessage());
126  }
127 
131  public function ‪setCreatedSetsCreated()
132  {
133  $record = $this->‪getRecord();
134  $created = 123.45;
135  $this->assertEquals($created, $record->setCreated($created)->getCreated());
136  }
137 
142  {
143  $record = $this->‪getRecord();
144  $requestId = 'testrequestid';
145  $this->assertEquals($requestId, $record->setRequestId($requestId)->getRequestId());
146  }
147 
152  {
153  $component = 'test.core.log';
154  $level = ‪LogLevel::DEBUG;
155  $message = 'test message';
156  $data = ['foo' => 'bar'];
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 
170  {
171  $dataArray = ['foo' => 'bar'];
172  $record = $this->‪getRecord(['data' => $dataArray]);
173  $this->assertContains(json_encode($dataArray), (string)$record);
174  }
175 
180  {
181  $dataArray = ['exception' => new \Exception('foo', 1476049451)];
182  $record = $this->‪getRecord(['data' => $dataArray]);
183  $this->assertContains('Exception: foo', (string)$record);
184  }
185 }
‪TYPO3\CMS\Core\Log\LogLevel\CRITICAL
‪const CRITICAL
Definition: LogLevel.php:48
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest
Definition: LogRecordTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setLevelSetsLevel
‪setLevelSetsLevel()
Definition: LogRecordTest.php:99
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectMessage
‪constructorSetsCorrectMessage()
Definition: LogRecordTest.php:67
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\toArrayReturnsCorrectValues
‪toArrayReturnsCorrectValues()
Definition: LogRecordTest.php:151
‪TYPO3\CMS\Core\Tests\Unit\Log
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setLevelValidatesLevel
‪setLevelValidatesLevel()
Definition: LogRecordTest.php:109
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setComponentSetsComponent
‪setComponentSetsComponent()
Definition: LogRecordTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectLogLevel
‪constructorSetsCorrectLogLevel()
Definition: LogRecordTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectComponent
‪constructorSetsCorrectComponent()
Definition: LogRecordTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectData
‪constructorSetsCorrectData()
Definition: LogRecordTest.php:77
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\toStringIncludesExceptionDataAsJson
‪toStringIncludesExceptionDataAsJson()
Definition: LogRecordTest.php:179
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setRequestIdSetsRequestId
‪setRequestIdSetsRequestId()
Definition: LogRecordTest.php:141
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:21
‪TYPO3\CMS\Core\Log\LogLevel\EMERGENCY
‪const EMERGENCY
Definition: LogLevel.php:30
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setCreatedSetsCreated
‪setCreatedSetsCreated()
Definition: LogRecordTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\toStringIncludesDataAsJson
‪toStringIncludesDataAsJson()
Definition: LogRecordTest.php:169
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\getRecord
‪LogRecord getRecord(array $parameters=[])
Definition: LogRecordTest.php:33
‪TYPO3\CMS\Core\Log\LogLevel\DEBUG
‪const DEBUG
Definition: LogLevel.php:94
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:21
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setMessageSetsMessage
‪setMessageSetsMessage()
Definition: LogRecordTest.php:121