[FOLLOWUP][BUGFIX] Handle exceptions in Logging API
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / Writer / DatabaseWriterTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Log\Writer;
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 use TYPO3\CMS\Core\Log\LogRecord;
17
18 /**
19 * Test case
20 *
21 * @author Steffen Gebert <steffen.gebert@typo3.org>
22 */
23 class DatabaseWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * @test
27 */
28 public function getTableReturnsPreviouslySetTable() {
29 $logTable = $this->getUniqueId('logtable_');
30 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
31 $subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', FALSE);
32 $subject->setLogTable($logTable);
33 $this->assertSame($logTable, $subject->getLogTable());
34 }
35
36 /**
37 * @test
38 * @expectedException \RuntimeException
39 */
40 public function writeLogThrowsExceptionIfDatabaseInsertFailed() {
41 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', FALSE);
42 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->will($this->returnValue(FALSE));
43 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
44 $logRecordMock = $this->getMock(\TYPO3\CMS\Core\Log\LogRecord::class, array(), array(), '', FALSE);
45 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
46 $subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', FALSE);
47 $subject->writeLog($logRecordMock);
48 }
49
50 /**
51 * @test
52 */
53 public function writeLogInsertsToSpecifiedTable() {
54 $logTable = $this->getUniqueId('logtable_');
55 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', FALSE);
56 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($logTable, $this->anything());
57 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
58 $logRecordMock = $this->getMock(\TYPO3\CMS\Core\Log\LogRecord::class, array(), array(), '', FALSE);
59 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
60 $subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', FALSE);
61 $subject->setLogTable($logTable);
62 $subject->writeLog($logRecordMock);
63 }
64
65 /**
66 * @test
67 */
68 public function writeLogInsertsLogRecordWithGivenProperties() {
69 $logRecordData = array(
70 'request_id' => $this->getUniqueId('request_id'),
71 'time_micro' => $this->getUniqueId('time_micro'),
72 'component' => $this->getUniqueId('component'),
73 'level' => $this->getUniqueId('level'),
74 'message' => $this->getUniqueId('message'),
75 'data' => '',
76 );
77 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
78 $logRecordFixture = $this->getMock(\TYPO3\CMS\Core\Log\LogRecord::class, array(), array(), '', FALSE);
79 $logRecordFixture->expects($this->any())->method('getRequestId')->will($this->returnValue($logRecordData['request_id']));
80 $logRecordFixture->expects($this->any())->method('getCreated')->will($this->returnValue($logRecordData['time_micro']));
81 $logRecordFixture->expects($this->any())->method('getComponent')->will($this->returnValue($logRecordData['component']));
82 $logRecordFixture->expects($this->any())->method('getLevel')->will($this->returnValue($logRecordData['level']));
83 $logRecordFixture->expects($this->any())->method('getMessage')->will($this->returnValue($logRecordData['message']));
84 $logRecordFixture->expects($this->any())->method('getData')->will($this->returnValue(array()));
85
86 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
87 #$subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', FALSE);
88 $subject = new \TYPO3\CMS\Core\Log\Writer\DatabaseWriter();
89
90 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', FALSE);
91 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($this->anything(), $logRecordData);
92
93 $subject->writeLog($logRecordFixture);
94 }
95
96 }