[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / Tests / Unit / Database / AbstractTestCase.php
1 <?php
2 namespace TYPO3\CMS\Dbal\Tests\Unit\Database;
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 TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 require_once __DIR__ . '/../../../../adodb/adodb/adodb.inc.php';
20 require_once __DIR__ . '/../../../../adodb/adodb/drivers/adodb-mssql.inc.php';
21 require_once __DIR__ . '/../../../../adodb/adodb/drivers/adodb-oci8.inc.php';
22 require_once __DIR__ . '/../../../../adodb/adodb/drivers/adodb-postgres7.inc.php';
23
24 /**
25 * Base test case for dbal database tests.
26 */
27 abstract class AbstractTestCase extends \TYPO3\CMS\Core\Tests\UnitTestCase
28 {
29 /**
30 * Prepare a DatabaseConnection subject.
31 * Used by driver specific test cases.
32 *
33 * @param string $driver Driver to use like "mssql", "oci8" and "postgres7"
34 * @param array $configuration Dbal configuration array
35 * @return \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
36 */
37 protected function prepareSubject($driver, array $configuration)
38 {
39 /** @var \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
40 $subject = $this->getAccessibleMock(\TYPO3\CMS\Dbal\Database\DatabaseConnection::class, array('getFieldInfoCache'), array(), '', false);
41
42 $subject->conf = $configuration;
43
44 // Disable caching
45 $mockCacheFrontend = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\PhpFrontend::class, array(), array(), '', false);
46 $subject->expects($this->any())->method('getFieldInfoCache')->will($this->returnValue($mockCacheFrontend));
47
48 // Inject SqlParser - Its logic is tested with the tests, too.
49 $sqlParser = $this->getAccessibleMock(\TYPO3\CMS\Dbal\Database\SqlParser::class, array('dummy'), array(), '', false);
50 $sqlParser->_set('databaseConnection', $subject);
51 $sqlParser->_set('sqlCompiler', GeneralUtility::makeInstance(\TYPO3\CMS\Dbal\Database\SqlCompilers\Adodb::class, $subject));
52 $sqlParser->_set('nativeSqlCompiler', GeneralUtility::makeInstance(\TYPO3\CMS\Dbal\Database\SqlCompilers\Mysql::class, $subject));
53 $subject->SQLparser = $sqlParser;
54
55 // Mock away schema migration service from install tool
56 $installerSqlMock = $this->getMock(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService::class, array('getFieldDefinitions_fileContent'), array(), '', false);
57 $installerSqlMock->expects($this->any())->method('getFieldDefinitions_fileContent')->will($this->returnValue(array()));
58 $subject->_set('installerSql', $installerSqlMock);
59
60 $subject->initialize();
61
62 // Fake a working connection
63 $handlerKey = '_DEFAULT';
64 $subject->lastHandlerKey = $handlerKey;
65 $adodbDriverClass = '\ADODB_' . $driver;
66 $subject->handlerInstance[$handlerKey] = new $adodbDriverClass();
67 $subject->handlerInstance[$handlerKey]->DataDictionary = NewDataDictionary($subject->handlerInstance[$handlerKey]);
68 $subject->handlerInstance[$handlerKey]->_connectionID = rand(1, 1000);
69
70 return $subject;
71 }
72
73 /**
74 * Clean a parsed SQL query for easier comparison with expected SQL.
75 *
76 * @param mixed $sql
77 * @return mixed (string or array)
78 */
79 protected function cleanSql($sql)
80 {
81 if (!is_string($sql)) {
82 return $sql;
83 }
84 $sql = str_replace("\n", ' ', $sql);
85 $sql = preg_replace('/\\s+/', ' ', $sql);
86 return trim($sql);
87 }
88 }