[TASK] Replace @expectedException with code wise test 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 * 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->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', false);
30 $subject->setLogTable($logTable);
31 $this->assertSame($logTable, $subject->getLogTable());
32 }
33
34 /**
35 * @test
36 */
37 public function writeLogThrowsExceptionIfDatabaseInsertFailed()
38 {
39 $this->expectException(\RuntimeException::class);
40 $this->expectExceptionCode(1345036334);
41
42 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', false);
43 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->will($this->returnValue(false));
44 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
45 $logRecordMock = $this->getMock(\TYPO3\CMS\Core\Log\LogRecord::class, array(), array(), '', false);
46 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
47 $subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', false);
48 $subject->writeLog($logRecordMock);
49 }
50
51 /**
52 * @test
53 */
54 public function writeLogInsertsToSpecifiedTable()
55 {
56 $logTable = $this->getUniqueId('logtable_');
57 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', false);
58 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($logTable, $this->anything());
59 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
60 $logRecordMock = $this->getMock(\TYPO3\CMS\Core\Log\LogRecord::class, array(), array(), '', false);
61 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
62 $subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', false);
63 $subject->setLogTable($logTable);
64 $subject->writeLog($logRecordMock);
65 }
66
67 /**
68 * @test
69 */
70 public function writeLogInsertsLogRecordWithGivenProperties()
71 {
72 $logRecordData = array(
73 'request_id' => $this->getUniqueId('request_id'),
74 'time_micro' => $this->getUniqueId('time_micro'),
75 'component' => $this->getUniqueId('component'),
76 'level' => $this->getUniqueId('level'),
77 'message' => $this->getUniqueId('message'),
78 'data' => '',
79 );
80 /** @var \TYPO3\CMS\Core\Log\LogRecord|\PHPUnit_Framework_MockObject_MockObject $subject */
81 $logRecordFixture = $this->getMock(\TYPO3\CMS\Core\Log\LogRecord::class, array(), array(), '', false);
82 $logRecordFixture->expects($this->any())->method('getRequestId')->will($this->returnValue($logRecordData['request_id']));
83 $logRecordFixture->expects($this->any())->method('getCreated')->will($this->returnValue($logRecordData['time_micro']));
84 $logRecordFixture->expects($this->any())->method('getComponent')->will($this->returnValue($logRecordData['component']));
85 $logRecordFixture->expects($this->any())->method('getLevel')->will($this->returnValue($logRecordData['level']));
86 $logRecordFixture->expects($this->any())->method('getMessage')->will($this->returnValue($logRecordData['message']));
87 $logRecordFixture->expects($this->any())->method('getData')->will($this->returnValue(array()));
88
89 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
90 #$subject = $this->getMock(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class, array('dummy'), array(), '', FALSE);
91 $subject = new \TYPO3\CMS\Core\Log\Writer\DatabaseWriter();
92
93 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array(), array(), '', false);
94 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with($this->anything(), $logRecordData);
95
96 $subject->writeLog($logRecordFixture);
97 }
98 }