d38dbf7ac547d933e825d4509297bdf9873cd4b8
[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 use Prophecy\Argument;
18 use TYPO3\CMS\Core\Database\Connection;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22
23 /**
24 * Test case
25 */
26 class DatabaseWriterTest extends UnitTestCase
27 {
28 /**
29 * @test
30 */
31 public function getTableReturnsPreviouslySetTable()
32 {
33 $logTable = $this->getUniqueId('logtable_');
34 /** @var \TYPO3\CMS\Core\Log\Writer\DatabaseWriter|\PHPUnit_Framework_MockObject_MockObject $subject */
35 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
36 ->setMethods(['dummy'])
37 ->disableOriginalConstructor()
38 ->getMock();
39 $subject->setLogTable($logTable);
40 $this->assertSame($logTable, $subject->getLogTable());
41 }
42
43 /**
44 * @test
45 */
46 public function writeLogInsertsToSpecifiedTable()
47 {
48 $logTable = $this->getUniqueId('logtable_');
49
50 $connectionProphecy = $this->prophesize(Connection::class);
51 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
52 $connectionPoolProphecy->getConnectionForTable(Argument::cetera())->willReturn($connectionProphecy->reveal());
53
54 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
55 $logRecordMock = $this->createMock(\TYPO3\CMS\Core\Log\LogRecord::class);
56 $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class)
57 ->setMethods(['dummy'])
58 ->disableOriginalConstructor()
59 ->getMock();
60 $subject->setLogTable($logTable);
61
62 // $logTable should end up as first insert argument
63 $connectionProphecy->insert($logTable, Argument::cetera())->willReturn(1);
64
65 $subject->writeLog($logRecordMock);
66 }
67 }