[TASK] Replace deprecated getMock method in Unit tests
[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
17 /**
18 * Test case
19 */
20 class DatabaseWriterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @test
24 */
25 public function getTableReturnsPreviouslySetTable()
26 {
27 $logTable = $this->getUniqueId('logtable_');
28 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
29 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
30 ->setMethods(array('dummy'))
31 ->disableOriginalConstructor()
32 ->getMock();
33 $subject->setLogTable($logTable);
34 $this->assertSame($logTable, $subject->getLogTable());
35 }
36
37 /**
38 * @test
39 */
40 public function writeLogThrowsExceptionIfDatabaseInsertFailed()
41 {
42 $this->expectException(\RuntimeException::class);
43 $this->expectExceptionCode(1345036334);
44
45 $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
46 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->will($this->returnValue(false));
47 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
48 $logRecordMock = $this->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
49 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
50 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
51 ->setMethods(array('dummy'))
52 ->disableOriginalConstructor()
53 ->getMock();
54 $subject->writeLog($logRecordMock);
55 }
56
57 /**
58 * @test
59 */
60 public function writeLogInsertsToSpecifiedTable()
61 {
62 $logTable = $this->getUniqueId('logtable_');
63 $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
64 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($logTable, $this->anything());
65 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
66 $logRecordMock = $this->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
67 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
68 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
69 ->setMethods(array('dummy'))
70 ->disableOriginalConstructor()
71 ->getMock();
72 $subject->setLogTable($logTable);
73 $subject->writeLog($logRecordMock);
74 }
75
76 /**
77 * @test
78 */
79 public function writeLogInsertsLogRecordWithGivenProperties()
80 {
81 $logRecordData = array(
82 'request_id' => $this->getUniqueId('request_id'),
83 'time_micro' => $this->getUniqueId('time_micro'),
84 'component' => $this->getUniqueId('component'),
85 'level' => $this->getUniqueId('level'),
86 'message' => $this->getUniqueId('message'),
87 'data' => '',
88 );
89 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
90 $logRecordFixture = $this->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
91 $logRecordFixture->expects($this->any())->method('getRequestId')->will($this->returnValue($logRecordData['request_id']));
92 $logRecordFixture->expects($this->any())->method('getCreated')->will($this->returnValue($logRecordData['time_micro']));
93 $logRecordFixture->expects($this->any())->method('getComponent')->will($this->returnValue($logRecordData['component']));
94 $logRecordFixture->expects($this->any())->method('getLevel')->will($this->returnValue($logRecordData['level']));
95 $logRecordFixture->expects($this->any())->method('getMessage')->will($this->returnValue($logRecordData['message']));
96 $logRecordFixture->expects($this->any())->method('getData')->will($this->returnValue(array()));
97
98 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
99 #$subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', FALSE);
100 $subject = new \TYPO3\CMS\Core\Log\Writer\DatabaseWriter();
101
102 $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
103 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($this->anything(), $logRecordData);
104
105 $subject->writeLog($logRecordFixture);
106 }
107 }