[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 /**
18 * Testcase for \TYPO3\CMS\Core\Log\LogRecord.
19 *
20 * @author Steffen Gebert <steffen.gebert@typo3.org>
21 */
22 class RecordTest extends \TYPO3\CMS\Core\Tests\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 \TYPO3\CMS\Core\Log\LogRecord
29 */
30 protected function getRecord(array $parameters = array()) {
31 /** @var $record \TYPO3\CMS\Core\Log\LogRecord */
32 $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'] ?: array());
33 return $record;
34 }
35
36 /**
37 * @test
38 */
39 public function constructorSetsCorrectComponent() {
40 $component = 'test.core.log';
41 $record = $this->getRecord(array('component' => $component));
42 $this->assertEquals($component, $record->getComponent());
43 }
44
45 /**
46 * @test
47 */
48 public function constructorSetsCorrectLogLevel() {
49 $logLevel = \TYPO3\CMS\Core\Log\LogLevel::CRITICAL;
50 $record = $this->getRecord(array('level' => $logLevel));
51 $this->assertEquals($logLevel, $record->getLevel());
52 }
53
54 /**
55 * @test
56 */
57 public function constructorSetsCorrectMessage() {
58 $logMessage = 'test message';
59 $record = $this->getRecord(array('message' => $logMessage));
60 $this->assertEquals($logMessage, $record->getMessage());
61 }
62
63 /**
64 * @test
65 */
66 public function constructorSetsCorrectData() {
67 $dataArray = array(
68 'foo' => 'bar'
69 );
70 $record = $this->getRecord(array('data' => $dataArray));
71 $this->assertEquals($dataArray, $record->getData());
72 }
73
74 /**
75 * @test
76 */
77 public function setComponentSetsComponent() {
78 $record = $this->getRecord();
79 $component = 'testcomponent';
80 $this->assertEquals($component, $record->setComponent($component)->getComponent());
81 }
82
83 /**
84 * @test
85 */
86 public function setLevelSetsLevel() {
87 $record = $this->getRecord();
88 $level = \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY;
89 $this->assertEquals($level, $record->setLevel($level)->getLevel());
90 }
91
92 /**
93 * @test
94 * @expectedException Psr\Log\InvalidArgumentException
95 */
96 public function setLevelValidatesLevel() {
97 $record = $this->getRecord();
98 $record->setLevel(100);
99 }
100
101 /**
102 * @test
103 */
104 public function setMessageSetsMessage() {
105 $record = $this->getRecord();
106 $message = 'testmessage';
107 $this->assertEquals($message, $record->setMessage($message)->getMessage());
108 }
109
110 /**
111 * @test
112 */
113 public function setCreatedSetsCreated() {
114 $record = $this->getRecord();
115 $created = 123.45;
116 $this->assertEquals($created, $record->setCreated($created)->getCreated());
117 }
118
119 /**
120 * @test
121 */
122 public function setRequestIdSetsRequestId() {
123 $record = $this->getRecord();
124 $requestId = 'testrequestid';
125 $this->assertEquals($requestId, $record->setRequestId($requestId)->getRequestId());
126 }
127
128 /**
129 * @test
130 */
131 public function toArrayReturnsCorrectValues() {
132 $component = 'test.core.log';
133 $level = \TYPO3\CMS\Core\Log\LogLevel::DEBUG;
134 $message = 'test message';
135 $data = array('foo' => 'bar');
136 /** @var $record \TYPO3\CMS\Core\Log\LogRecord */
137 $record = new \TYPO3\CMS\Core\Log\LogRecord($component, $level, $message, $data);
138 $recordArray = $record->toArray();
139 $this->assertEquals($component, $recordArray['component']);
140 $this->assertEquals($level, $recordArray['level']);
141 $this->assertEquals($message, $recordArray['message']);
142 $this->assertEquals($data, $recordArray['data']);
143 }
144
145 /**
146 * @test
147 */
148 public function toStringIncludesDataAsJson() {
149 $dataArray = array('foo' => 'bar');
150 $record = $this->getRecord(array('data' => $dataArray));
151 $this->assertContains(json_encode($dataArray), (string) $record);
152 }
153
154 }