TYPO3 CMS  TYPO3_7-6
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 
21 {
28  protected function getRecord(array $parameters = [])
29  {
31  $record = new \TYPO3\CMS\Core\Log\LogRecord($parameters['component'] ?: 'test.core.log', $parameters['level'] ?: \TYPO3\CMS\Core\Log\LogLevel::DEBUG, $parameters['message'] ?: 'test message', $parameters['data'] ?: []);
32  return $record;
33  }
34 
39  {
40  $component = 'test.core.log';
41  $record = $this->getRecord(['component' => $component]);
42  $this->assertEquals($component, $record->getComponent());
43  }
44 
49  {
51  $record = $this->getRecord(['level' => $logLevel]);
52  $this->assertEquals($logLevel, $record->getLevel());
53  }
54 
59  {
60  $logMessage = 'test message';
61  $record = $this->getRecord(['message' => $logMessage]);
62  $this->assertEquals($logMessage, $record->getMessage());
63  }
64 
68  public function constructorSetsCorrectData()
69  {
70  $dataArray = [
71  'foo' => 'bar'
72  ];
73  $record = $this->getRecord(['data' => $dataArray]);
74  $this->assertEquals($dataArray, $record->getData());
75  }
76 
80  public function setComponentSetsComponent()
81  {
82  $record = $this->getRecord();
83  $component = 'testcomponent';
84  $this->assertEquals($component, $record->setComponent($component)->getComponent());
85  }
86 
90  public function setLevelSetsLevel()
91  {
92  $record = $this->getRecord();
94  $this->assertEquals($level, $record->setLevel($level)->getLevel());
95  }
96 
101  public function setLevelValidatesLevel()
102  {
103  $record = $this->getRecord();
104  $record->setLevel(100);
105  }
106 
110  public function setMessageSetsMessage()
111  {
112  $record = $this->getRecord();
113  $message = 'testmessage';
114  $this->assertEquals($message, $record->setMessage($message)->getMessage());
115  }
116 
120  public function setCreatedSetsCreated()
121  {
122  $record = $this->getRecord();
123  $created = 123.45;
124  $this->assertEquals($created, $record->setCreated($created)->getCreated());
125  }
126 
130  public function setRequestIdSetsRequestId()
131  {
132  $record = $this->getRecord();
133  $requestId = 'testrequestid';
134  $this->assertEquals($requestId, $record->setRequestId($requestId)->getRequestId());
135  }
136 
140  public function toArrayReturnsCorrectValues()
141  {
142  $component = 'test.core.log';
144  $message = 'test message';
145  $data = ['foo' => 'bar'];
147  $record = new \TYPO3\CMS\Core\Log\LogRecord($component, $level, $message, $data);
148  $recordArray = $record->toArray();
149  $this->assertEquals($component, $recordArray['component']);
150  $this->assertEquals($level, $recordArray['level']);
151  $this->assertEquals($message, $recordArray['message']);
152  $this->assertEquals($data, $recordArray['data']);
153  }
154 
158  public function toStringIncludesDataAsJson()
159  {
160  $dataArray = ['foo' => 'bar'];
161  $record = $this->getRecord(['data' => $dataArray]);
162  $this->assertContains(json_encode($dataArray), (string)$record);
163  }
164 
169  {
170  $dataArray = ['exception' => new \Exception('foo')];
171  $record = $this->getRecord(['data' => $dataArray]);
172  // Since 7.0.0-dev 17.05.2015 the output of Exception is changed.
173  // https://github.com/php/php-src/commit/3ae995f03c8f60c4a4c9718262545cf5a6a08da3
174  // To check for dev version we need to compare with a version before 7.0.0
175  if (version_compare(PHP_VERSION, '6.99.00') >= 0) {
176  $this->assertContains('Exception: foo', (string)$record);
177  } else {
178  $this->assertContains('\'Exception\' with message \'foo\'', (string)$record);
179  }
180  }
181 }