‪TYPO3CMS  10.4
LogRecordTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Psr\Log\InvalidArgumentException;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 class ‪LogRecordTest extends UnitTestCase
29 {
36  protected function ‪getRecord(array $parameters = []): ‪LogRecord
37  {
38  $record = new ‪LogRecord(
39  $parameters['component'] ?? 'test.core.log',
40  $parameters['level'] ?? LogLevel::DEBUG,
41  $parameters['message'] ?? 'test message',
42  $parameters['data'] ?? []
43  );
44  return $record;
45  }
46 
51  {
52  $component = 'test.core.log';
53  $record = $this->‪getRecord(['component' => $component]);
54  self::assertEquals($component, $record->getComponent());
55  }
56 
61  {
62  $logLevel = LogLevel::CRITICAL;
63  $record = $this->‪getRecord(['level' => $logLevel]);
64  self::assertEquals($logLevel, $record->getLevel());
65  }
66 
71  {
72  $logMessage = 'test message';
73  $record = $this->‪getRecord(['message' => $logMessage]);
74  self::assertEquals($logMessage, $record->getMessage());
75  }
76 
81  {
82  $dataArray = [
83  'foo' => 'bar'
84  ];
85  $record = $this->‪getRecord(['data' => $dataArray]);
86  self::assertEquals($dataArray, $record->getData());
87  }
88 
92  public function ‪setComponentSetsComponent()
93  {
94  $record = $this->‪getRecord();
95  $component = 'testcomponent';
96  self::assertEquals($component, $record->setComponent($component)->getComponent());
97  }
98 
102  public function ‪setLevelSetsLevel()
103  {
104  $record = $this->‪getRecord();
105  $level = LogLevel::EMERGENCY;
106  self::assertEquals($level, $record->setLevel($level)->getLevel());
107  }
108 
112  public function ‪setLevelValidatesLevel()
113  {
114  $this->expectException(InvalidArgumentException::class);
115  $this->expectExceptionCode(1550247164);
116 
117  $record = $this->‪getRecord();
118  $record->setLevel('foo');
119  }
120 
124  public function ‪setMessageSetsMessage()
125  {
126  $record = $this->‪getRecord();
127  $message = 'testmessage';
128  self::assertEquals($message, $record->setMessage($message)->getMessage());
129  }
130 
134  public function ‪setCreatedSetsCreated()
135  {
136  $record = $this->‪getRecord();
137  $created = 123.45;
138  self::assertEquals($created, $record->setCreated($created)->getCreated());
139  }
140 
145  {
146  $record = $this->‪getRecord();
147  $requestId = 'testrequestid';
148  self::assertEquals($requestId, $record->setRequestId($requestId)->getRequestId());
149  }
150 
155  {
156  $component = 'test.core.log';
157  $level = LogLevel::DEBUG;
158  $message = 'test message';
159  $data = ['foo' => 'bar'];
161  $record = new ‪LogRecord($component, $level, $message, $data);
162  $recordArray = $record->toArray();
163  self::assertEquals($component, $recordArray['component']);
164  self::assertEquals($level, $recordArray['level']);
165  self::assertEquals($message, $recordArray['message']);
166  self::assertEquals($data, $recordArray['data']);
167  }
168 
173  {
174  $dataArray = ['foo' => 'bar'];
175  $record = $this->‪getRecord(['data' => $dataArray]);
176  self::assertStringContainsString(json_encode($dataArray), (string)$record);
177  }
178 
183  {
184  $dataArray = ['exception' => new \Exception('foo', 1476049451)];
185  $record = $this->‪getRecord(['data' => $dataArray]);
186  self::assertStringContainsString('Exception: foo', (string)$record);
187  }
188 }
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest
Definition: LogRecordTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setLevelSetsLevel
‪setLevelSetsLevel()
Definition: LogRecordTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectMessage
‪constructorSetsCorrectMessage()
Definition: LogRecordTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\toArrayReturnsCorrectValues
‪toArrayReturnsCorrectValues()
Definition: LogRecordTest.php:154
‪TYPO3\CMS\Core\Tests\Unit\Log
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setLevelValidatesLevel
‪setLevelValidatesLevel()
Definition: LogRecordTest.php:112
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setComponentSetsComponent
‪setComponentSetsComponent()
Definition: LogRecordTest.php:92
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectLogLevel
‪constructorSetsCorrectLogLevel()
Definition: LogRecordTest.php:60
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectComponent
‪constructorSetsCorrectComponent()
Definition: LogRecordTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\constructorSetsCorrectData
‪constructorSetsCorrectData()
Definition: LogRecordTest.php:80
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\toStringIncludesExceptionDataAsJson
‪toStringIncludesExceptionDataAsJson()
Definition: LogRecordTest.php:182
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setRequestIdSetsRequestId
‪setRequestIdSetsRequestId()
Definition: LogRecordTest.php:144
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:22
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setCreatedSetsCreated
‪setCreatedSetsCreated()
Definition: LogRecordTest.php:134
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\toStringIncludesDataAsJson
‪toStringIncludesDataAsJson()
Definition: LogRecordTest.php:172
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\getRecord
‪LogRecord getRecord(array $parameters=[])
Definition: LogRecordTest.php:36
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24
‪TYPO3\CMS\Core\Tests\Unit\Log\LogRecordTest\setMessageSetsMessage
‪setMessageSetsMessage()
Definition: LogRecordTest.php:124