TYPO3 CMS  TYPO3_8-7
LogRecordTest.php
Go to the documentation of this file.
1 <?php
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 
18 
22 class LogRecordTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
30  protected function getRecord(array $parameters = [])
31  {
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 
41  {
42  $component = 'test.core.log';
43  $record = $this->getRecord(['component' => $component]);
44  $this->assertEquals($component, $record->getComponent());
45  }
46 
51  {
53  $record = $this->getRecord(['level' => $logLevel]);
54  $this->assertEquals($logLevel, $record->getLevel());
55  }
56 
61  {
62  $logMessage = 'test message';
63  $record = $this->getRecord(['message' => $logMessage]);
64  $this->assertEquals($logMessage, $record->getMessage());
65  }
66 
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 
82  public function setComponentSetsComponent()
83  {
84  $record = $this->getRecord();
85  $component = 'testcomponent';
86  $this->assertEquals($component, $record->setComponent($component)->getComponent());
87  }
88 
92  public function setLevelSetsLevel()
93  {
94  $record = $this->getRecord();
96  $this->assertEquals($level, $record->setLevel($level)->getLevel());
97  }
98 
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 
114  public function setMessageSetsMessage()
115  {
116  $record = $this->getRecord();
117  $message = 'testmessage';
118  $this->assertEquals($message, $record->setMessage($message)->getMessage());
119  }
120 
124  public function setCreatedSetsCreated()
125  {
126  $record = $this->getRecord();
127  $created = 123.45;
128  $this->assertEquals($created, $record->setCreated($created)->getCreated());
129  }
130 
134  public function setRequestIdSetsRequestId()
135  {
136  $record = $this->getRecord();
137  $requestId = 'testrequestid';
138  $this->assertEquals($requestId, $record->setRequestId($requestId)->getRequestId());
139  }
140 
144  public function toArrayReturnsCorrectValues()
145  {
146  $component = 'test.core.log';
148  $message = 'test message';
149  $data = ['foo' => 'bar'];
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 
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 
173  {
174  $dataArray = ['exception' => new \Exception('foo', 1476049451)];
175  $record = $this->getRecord(['data' => $dataArray]);
176  $this->assertContains('Exception: foo', (string)$record);
177  }
178 }