bd0184fd3e27fd5325b76bc6a7751494f5dafbb1
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Log / Writer / DatabaseTest.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 * Testcase for \TYPO3\CMS\Core\Log\Writer\DatabaseWriter
29 *
30 * @author Steffen Gebert <steffen.gebert@typo3.org>
31 */
32 class DatabaseTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * Backup and restore of the $GLOBALS array.
36 *
37 * @var boolean
38 */
39 protected $backupGlobalsArray = array();
40
41 /**
42 * Mock object of t3lib_db
43 *
44 * @var PHPUnit_Framework_MockObject_MockObject
45 */
46 private $databaseStub;
47
48 /**
49 * Create a new database mock object for every test
50 * and backup the original global database object.
51 *
52 * @return void
53 */
54 public function setUp() {
55 $this->backupGlobalsArray['TYPO3_DB'] = $GLOBALS['TYPO3_DB'];
56 $this->databaseStub = $this->setUpAndReturnDatabaseStub();
57 }
58
59 /**
60 * Restore global database object.
61 *
62 * @return void
63 */
64 protected function tearDown() {
65 $GLOBALS['TYPO3_DB'] = $this->backupGlobalsArray['TYPO3_DB'];
66 }
67
68 //////////////////////
69 // Utility functions
70 //////////////////////
71 /**
72 * Set up the stub to be able to get the result of the prepared statement.
73 *
74 * @return PHPUnit_Framework_MockObject_MockObject
75 */
76 private function setUpAndReturnDatabaseStub() {
77 $databaseLink = $GLOBALS['TYPO3_DB']->link;
78 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('exec_INSERTquery'), array(), '', FALSE, FALSE);
79 $GLOBALS['TYPO3_DB']->link = $databaseLink;
80 return $GLOBALS['TYPO3_DB'];
81 }
82
83 /**
84 * Creates a test logger
85 *
86 * @return \TYPO3\CMS\Core\Log\Logger
87 */
88 protected function createLogger() {
89 $loggerName = uniqid('test.core.datbaseWriter');
90 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
91 $logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger($loggerName);
92 return $logger;
93 }
94
95 /**
96 * Creates a database writer
97 *
98 * @return \TYPO3\CMS\Core\Log\Writer\DatabaseWriter
99 */
100 protected function createWriter() {
101 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter $writer */
102 $writer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\Writer\\DatabaseWriter');
103 return $writer;
104 }
105
106 /**
107 * @test
108 */
109 public function setLogTableSetsLogTable() {
110 $logTable = uniqid('logtable_');
111 $this->assertSame($logTable, $this->createWriter()->setLogTable($logTable)->getLogTable());
112 }
113
114 /**
115 * @return array
116 */
117 public function writerLogsToDatabaseDataProvider() {
118 $simpleRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', 'test.core.databaseWriter.simpleRecord', \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test entry');
119 $recordWithData = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', 'test.core.databaseWriter.recordWithData', \TYPO3\CMS\Core\Log\LogLevel::ALERT, 'test entry with data', array('foo' => array('bar' => 'baz')));
120 return array(
121 'simple record' => array($simpleRecord),
122 'record with data' => array($recordWithData)
123 );
124 }
125
126 /**
127 * @test
128 * @param \TYPO3\CMS\Core\Log\LogRecord $record Record Test Data
129 * @dataProvider writerLogsToDatabaseDataProvider
130 */
131 public function writerLogsToDatabase(\TYPO3\CMS\Core\Log\LogRecord $record) {
132 $logger = $this->createLogger();
133 $databaseWriter = $this->createWriter();
134 $logger->addWriter(\TYPO3\CMS\Core\Log\LogLevel::NOTICE, $databaseWriter);
135 $this->databaseStub->expects($this->once())->method('exec_INSERTquery');
136 $logger->log($record->getLevel(), $record->getMessage());
137 }
138
139 }
140
141 ?>