[BUGFIX] Tests: Refactor log writer tests in ext:core
[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 * Copyright notice
6 *
7 * (c) 2011-2013 Steffen Gebert (steffen.gebert@typo3.org)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case
29 *
30 * @author Steffen Gebert <steffen.gebert@typo3.org>
31 */
32 class DatabaseWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @test
36 */
37 public function getTableReturnsPreviouslySetTable() {
38 $logTable = uniqid('logtable_');
39 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
40 $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
41 $subject->setLogTable($logTable);
42 $this->assertSame($logTable, $subject->getLogTable());
43 }
44
45 /**
46 * @test
47 * @expectedException \RuntimeException
48 */
49 public function writeLogThrowsExceptionIfDatabaseInsertFailed() {
50 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
51 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->will($this->returnValue(FALSE));
52 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
53 $logRecordMock = $this->getMock('TYPO3\\CMS\\Core\\Log\\LogRecord', array(), array(), '', FALSE);
54 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
55 $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
56 $subject->writeLog($logRecordMock);
57 }
58
59 /**
60 * @test
61 */
62 public function writeLogInsertsToSpecifiedTable() {
63 $logTable = uniqid('logtable_');
64 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
65 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($logTable, $this->anything());
66 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
67 $logRecordMock = $this->getMock('TYPO3\\CMS\\Core\\Log\\LogRecord', array(), array(), '', FALSE);
68 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
69 $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
70 $subject->setLogTable($logTable);
71 $subject->writeLog($logRecordMock);
72 }
73
74 /**
75 * @test
76 */
77 public function writeLogInsertsLogRecordWithGivenProperties() {
78 $logRecordData = array(
79 'request_id' => uniqid('request_id'),
80 'time_micro' => uniqid('time_micro'),
81 'component' => uniqid('component'),
82 'level' => uniqid('level'),
83 'message' => uniqid('message'),
84 'data' => '',
85 );
86 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
87 $logRecordMock = $this->getMock('TYPO3\\CMS\\Core\\Log\\LogRecord', array(), array(), '', FALSE);
88 $logRecordMock->expects($this->at(0))->method('offsetGet')->with('requestId')->will($this->returnValue($logRecordData['request_id']));
89 $logRecordMock->expects($this->at(1))->method('offsetGet')->with('created')->will($this->returnValue($logRecordData['time_micro']));
90 $logRecordMock->expects($this->at(2))->method('offsetGet')->with('component')->will($this->returnValue($logRecordData['component']));
91 $logRecordMock->expects($this->at(3))->method('offsetGet')->with('level')->will($this->returnValue($logRecordData['level']));
92 $logRecordMock->expects($this->at(4))->method('offsetGet')->with('message')->will($this->returnValue($logRecordData['message']));
93
94 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
95 $subject = $this->getMock('TYPO3\\CMS\Core\Log\\Writer\\DatabaseWriter', array('dummy'), array(), '', FALSE);
96
97 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
98 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($this->anything(), $logRecordData);
99
100 $subject->writeLog($logRecordMock);
101 }
102 }